From 1e19de957960c4c3401b43c4ba76d7a5d24ecc00 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 5 Sep 2021 18:31:43 +0200 Subject: [PATCH] state-res: Return borrows from Event::{prev_events, auth_events} --- .../ruma-state-res/benches/state_res_bench.rs | 18 +++++++++--------- crates/ruma-state-res/src/event_auth.rs | 4 ++-- crates/ruma-state-res/src/lib.rs | 16 +++++++--------- crates/ruma-state-res/src/state_event.rs | 6 ++++-- crates/ruma-state-res/src/test_utils.rs | 18 +++++++++--------- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/crates/ruma-state-res/benches/state_res_bench.rs b/crates/ruma-state-res/benches/state_res_bench.rs index 52a9154e..f278637c 100644 --- a/crates/ruma-state-res/benches/state_res_bench.rs +++ b/crates/ruma-state-res/benches/state_res_bench.rs @@ -202,7 +202,7 @@ impl TestStore { let event = self.get_event(room_id, &ev_id)?; - stack.extend(event.auth_events().clone()); + stack.extend(event.auth_events().cloned()); } Ok(result) @@ -578,7 +578,7 @@ pub mod event { self.state_key() } - fn prev_events(&self) -> Vec { + fn prev_events(&self) -> Box + '_> { self.prev_event_ids() } @@ -586,7 +586,7 @@ pub mod event { self.depth() } - fn auth_events(&self) -> Vec { + fn auth_events(&self) -> Box + '_> { self.auth_events() } @@ -732,19 +732,19 @@ pub mod event { } } - pub fn prev_event_ids(&self) -> Vec { + pub fn prev_event_ids(&self) -> Box + '_> { match &self.rest { - Pdu::RoomV1Pdu(ev) => ev.prev_events.iter().map(|(id, _)| id).cloned().collect(), - Pdu::RoomV3Pdu(ev) => ev.prev_events.clone(), + Pdu::RoomV1Pdu(ev) => Box::new(ev.prev_events.iter().map(|(id, _)| id)), + Pdu::RoomV3Pdu(ev) => Box::new(ev.prev_events.iter()), #[cfg(not(feature = "unstable-exhaustive-types"))] _ => unreachable!("new PDU version"), } } - pub fn auth_events(&self) -> Vec { + pub fn auth_events(&self) -> Box + '_> { match &self.rest { - Pdu::RoomV1Pdu(ev) => ev.auth_events.iter().map(|(id, _)| id).cloned().collect(), - Pdu::RoomV3Pdu(ev) => ev.auth_events.to_vec(), + Pdu::RoomV1Pdu(ev) => Box::new(ev.auth_events.iter().map(|(id, _)| id)), + Pdu::RoomV3Pdu(ev) => Box::new(ev.auth_events.iter()), #[cfg(not(feature = "unstable-exhaustive-types"))] _ => unreachable!("new PDU version"), } diff --git a/crates/ruma-state-res/src/event_auth.rs b/crates/ruma-state-res/src/event_auth.rs index e6382b8b..8234a86c 100644 --- a/crates/ruma-state-res/src/event_auth.rs +++ b/crates/ruma-state-res/src/event_auth.rs @@ -118,7 +118,7 @@ where info!("start m.room.create check"); // If it has any previous events, reject - if !incoming_event.prev_events().is_empty() { + if incoming_event.prev_events().next().is_some() { warn!("the room creation event had previous events"); return Ok(false); } @@ -431,7 +431,7 @@ fn valid_membership_change( } if let Some(prev) = prev_event { - if *prev.event_type() == EventType::RoomCreate && prev.prev_events().is_empty() { + if *prev.event_type() == EventType::RoomCreate && prev.prev_events().next().is_none() { return Ok(true); } } diff --git a/crates/ruma-state-res/src/lib.rs b/crates/ruma-state-res/src/lib.rs index b4e28655..69cdb553 100644 --- a/crates/ruma-state-res/src/lib.rs +++ b/crates/ruma-state-res/src/lib.rs @@ -341,8 +341,8 @@ where let event = fetch_event(event_id); let mut pl = None; - for aid in event.as_ref().map(|pdu| pdu.auth_events()).unwrap_or_default() { - if let Some(aev) = fetch_event(&aid) { + for aid in event.as_ref().map(|pdu| pdu.auth_events()).into_iter().flatten() { + if let Some(aev) = fetch_event(aid) { if is_type_and_key(&aev, &EventType::RoomPowerLevels, "") { pl = Some(aev); break; @@ -402,7 +402,7 @@ where .ok_or_else(|| Error::InvalidPdu("State event had no state key".to_owned()))?; let mut auth_events = HashMap::new(); - for aid in &event.auth_events() { + for aid in event.auth_events() { if let Some(ev) = fetch_event(aid) { // TODO synapse check "rejected_reason" which is most likely // related to soft-failing @@ -439,7 +439,7 @@ where debug!("event to check {:?}", event.event_id()); let most_recent_prev_event = - event.prev_events().iter().filter_map(|id| fetch_event(id)).next_back(); + event.prev_events().filter_map(|id| fetch_event(id)).next_back(); // The key for this is (eventType + a state_key of the signed token not sender) so // search for it @@ -502,9 +502,8 @@ where let event = fetch_event(&p).ok_or_else(|| Error::NotFound(format!("Failed to find {}", p)))?; - let auth_events = &event.auth_events(); pl = None; - for aid in auth_events { + for aid in event.auth_events() { let ev = fetch_event(aid) .ok_or_else(|| Error::NotFound(format!("Failed to find {}", aid)))?; if is_type_and_key(&ev, &EventType::RoomPowerLevels, "") { @@ -566,9 +565,8 @@ where return Ok(*depth); } - let auth_events = &sort_ev.auth_events(); event = None; - for aid in auth_events { + for aid in sort_ev.auth_events() { let aev = fetch_event(aid) .ok_or_else(|| Error::NotFound(format!("Failed to find {}", aid)))?; if is_type_and_key(&aev, &EventType::RoomPowerLevels, "") { @@ -594,7 +592,7 @@ fn add_event_and_auth_chain_to_graph( while let Some(eid) = state.pop() { graph.entry(eid.clone()).or_default(); // Prefer the store to event as the store filters dedups the events - for aid in &fetch_event(&eid).map(|ev| ev.auth_events()).unwrap_or_default() { + for aid in fetch_event(&eid).as_ref().map(|ev| ev.auth_events()).into_iter().flatten() { if auth_diff.contains(aid) { if !graph.contains_key(aid) { state.push(aid.clone()); diff --git a/crates/ruma-state-res/src/state_event.rs b/crates/ruma-state-res/src/state_event.rs index 4b632dc7..f9059c17 100644 --- a/crates/ruma-state-res/src/state_event.rs +++ b/crates/ruma-state-res/src/state_event.rs @@ -30,7 +30,8 @@ pub trait Event { fn state_key(&self) -> Option<&str>; /// The events before this event. - fn prev_events(&self) -> Vec; + // Requires GATs to avoid boxing (and TAIT for making it convenient). + fn prev_events(&self) -> Box + '_>; /// The maximum number of `prev_events` plus 1. /// @@ -38,7 +39,8 @@ pub trait Event { fn depth(&self) -> &UInt; /// All the authenticating events for this event. - fn auth_events(&self) -> Vec; + // Requires GATs to avoid boxing (and TAIT for making it convenient). + fn auth_events(&self) -> Box + '_>; /// If this event is a redaction event this is the event it redacts. fn redacts(&self) -> Option<&EventId>; diff --git a/crates/ruma-state-res/src/test_utils.rs b/crates/ruma-state-res/src/test_utils.rs index 06d86b4d..6322a545 100644 --- a/crates/ruma-state-res/src/test_utils.rs +++ b/crates/ruma-state-res/src/test_utils.rs @@ -244,7 +244,7 @@ impl TestStore { let event = self.get_event(room_id, &ev_id)?; - stack.extend(event.auth_events().clone()); + stack.extend(event.auth_events().cloned()); } Ok(result) @@ -599,13 +599,13 @@ pub mod event { fn state_key(&self) -> Option<&str> { Some(self.state_key()) } - fn prev_events(&self) -> Vec { + fn prev_events(&self) -> Box + '_> { self.prev_event_ids() } fn depth(&self) -> &UInt { self.depth() } - fn auth_events(&self) -> Vec { + fn auth_events(&self) -> Box + '_> { self.auth_events() } fn redacts(&self) -> Option<&EventId> { @@ -748,19 +748,19 @@ pub mod event { } } - pub fn prev_event_ids(&self) -> Vec { + pub fn prev_event_ids(&self) -> Box + '_> { match &self.rest { - Pdu::RoomV1Pdu(ev) => ev.prev_events.iter().map(|(id, _)| id).cloned().collect(), - Pdu::RoomV3Pdu(ev) => ev.prev_events.clone(), + Pdu::RoomV1Pdu(ev) => Box::new(ev.prev_events.iter().map(|(id, _)| id)), + Pdu::RoomV3Pdu(ev) => Box::new(ev.prev_events.iter()), #[cfg(not(feature = "unstable-exhaustive-types"))] _ => unreachable!("new PDU version"), } } - pub fn auth_events(&self) -> Vec { + pub fn auth_events(&self) -> Box + '_> { match &self.rest { - Pdu::RoomV1Pdu(ev) => ev.auth_events.iter().map(|(id, _)| id).cloned().collect(), - Pdu::RoomV3Pdu(ev) => ev.auth_events.to_vec(), + Pdu::RoomV1Pdu(ev) => Box::new(ev.auth_events.iter().map(|(id, _)| id)), + Pdu::RoomV3Pdu(ev) => Box::new(ev.auth_events.iter()), #[cfg(not(feature = "unstable-exhaustive-types"))] _ => unreachable!("new PDU version"), }