From fae5385753b10b26cd8fbb12500d8898f336643b Mon Sep 17 00:00:00 2001 From: Devin Ragotzy Date: Thu, 17 Jun 2021 17:20:43 -0400 Subject: [PATCH] state-res: Make all pub structs non_exhaustive --- crates/ruma-state-res/Cargo.toml | 1 + .../ruma-state-res/benches/state_res_bench.rs | 41 ++++++++++++- crates/ruma-state-res/src/lib.rs | 1 + crates/ruma-state-res/src/room_version.rs | 1 + crates/ruma-state-res/tests/utils.rs | 58 +++++++++++++++---- crates/ruma/Cargo.toml | 2 + 6 files changed, 93 insertions(+), 11 deletions(-) diff --git a/crates/ruma-state-res/Cargo.toml b/crates/ruma-state-res/Cargo.toml index 3c1584e8..b748c6ae 100644 --- a/crates/ruma-state-res/Cargo.toml +++ b/crates/ruma-state-res/Cargo.toml @@ -16,6 +16,7 @@ all-features = true [features] unstable-pre-spec = ["ruma-events/unstable-pre-spec"] +unstable-exhaustive-types = [] [dependencies] itertools = "0.10.0" diff --git a/crates/ruma-state-res/benches/state_res_bench.rs b/crates/ruma-state-res/benches/state_res_bench.rs index 68ae4158..d56e6a15 100644 --- a/crates/ruma-state-res/benches/state_res_bench.rs +++ b/crates/ruma-state-res/benches/state_res_bench.rs @@ -4,6 +4,9 @@ // `cargo bench unknown option --save-baseline`. // To pass args to criterion, use this form // `cargo bench --bench -- --save-baseline `. + +#![allow(clippy::exhaustive_structs)] + use std::{ collections::{BTreeMap, BTreeSet}, convert::{TryFrom, TryInto}, @@ -372,7 +375,7 @@ where auth_events, prev_events, depth: uint!(0), - hashes: EventHash { sha256: "".into() }, + hashes: EventHash::new(String::new()), signatures: btreemap! {}, }), }) @@ -627,6 +630,8 @@ pub mod event { _ => false, }, Pdu::RoomV3Pdu(event) => event.state_key == Some("".into()), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -634,6 +639,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => serde_json::from_value(ev.content.clone()), Pdu::RoomV3Pdu(ev) => serde_json::from_value(ev.content.clone()), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -641,6 +648,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.origin_server_ts, Pdu::RoomV3Pdu(ev) => &ev.origin_server_ts, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -652,6 +661,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.sender, Pdu::RoomV3Pdu(ev) => &ev.sender, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -659,6 +670,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.redacts.as_ref(), Pdu::RoomV3Pdu(ev) => ev.redacts.as_ref(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -666,18 +679,24 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.room_id, Pdu::RoomV3Pdu(ev) => &ev.room_id, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } pub fn kind(&self) -> EventType { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.kind.clone(), Pdu::RoomV3Pdu(ev) => ev.kind.clone(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } pub fn state_key(&self) -> Option { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.state_key.clone(), Pdu::RoomV3Pdu(ev) => ev.state_key.clone(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -686,6 +705,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.origin.clone(), Pdu::RoomV3Pdu(ev) => ev.origin.clone(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -693,6 +714,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.prev_events.iter().map(|(id, _)| id).cloned().collect(), Pdu::RoomV3Pdu(ev) => ev.prev_events.clone(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -700,6 +723,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.auth_events.iter().map(|(id, _)| id).cloned().collect(), Pdu::RoomV3Pdu(ev) => ev.auth_events.to_vec(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -707,6 +732,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.content.clone(), Pdu::RoomV3Pdu(ev) => ev.content.clone(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -714,6 +741,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.unsigned, Pdu::RoomV3Pdu(ev) => &ev.unsigned, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -723,6 +752,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(_) => maplit::btreemap! {}, Pdu::RoomV3Pdu(ev) => ev.signatures.clone(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -730,6 +761,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.hashes, Pdu::RoomV3Pdu(ev) => &ev.hashes, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -737,6 +770,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.depth, Pdu::RoomV3Pdu(ev) => &ev.depth, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -748,6 +783,8 @@ pub mod event { Pdu::RoomV3Pdu(ev) => { ev.kind == ev_type && ev.state_key.as_deref() == Some(state_key) } + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -760,6 +797,8 @@ pub mod event { match self.rest { Pdu::RoomV1Pdu(_) => RoomVersionId::Version1, Pdu::RoomV3Pdu(_) => RoomVersionId::Version6, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } } diff --git a/crates/ruma-state-res/src/lib.rs b/crates/ruma-state-res/src/lib.rs index d6388eb6..5464ce36 100644 --- a/crates/ruma-state-res/src/lib.rs +++ b/crates/ruma-state-res/src/lib.rs @@ -33,6 +33,7 @@ pub type StateMap = BTreeMap<(EventType, String), T>; pub type EventMap = BTreeMap; #[derive(Default)] +#[allow(clippy::exhaustive_structs)] pub struct StateResolution; impl StateResolution { diff --git a/crates/ruma-state-res/src/room_version.rs b/crates/ruma-state-res/src/room_version.rs index 9682207c..0c7e15bc 100644 --- a/crates/ruma-state-res/src/room_version.rs +++ b/crates/ruma-state-res/src/room_version.rs @@ -26,6 +26,7 @@ pub enum StateResolutionVersion { V2, } +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct RoomVersion { /// The version this room is set to. pub version: RoomVersionId, diff --git a/crates/ruma-state-res/tests/utils.rs b/crates/ruma-state-res/tests/utils.rs index 73e2b2d6..ed2a8163 100644 --- a/crates/ruma-state-res/tests/utils.rs +++ b/crates/ruma-state-res/tests/utils.rs @@ -1,4 +1,4 @@ -#![allow(clippy::or_fun_call, clippy::expect_fun_call, dead_code)] +#![allow(clippy::exhaustive_structs, dead_code)] use std::{ collections::{BTreeMap, BTreeSet}, @@ -60,14 +60,14 @@ pub fn do_check( for pair in INITIAL_EDGES().windows(2) { if let [a, b] = &pair { - graph.entry(a.clone()).or_insert(btreeset![]).insert(b.clone()); + graph.entry(a.clone()).or_insert_with(BTreeSet::new).insert(b.clone()); } } for edge_list in edges { for pair in edge_list.windows(2) { if let [a, b] = &pair { - graph.entry(a.clone()).or_insert(btreeset![]).insert(b.clone()); + graph.entry(a.clone()).or_insert_with(BTreeSet::new).insert(b.clone()); } } } @@ -173,11 +173,13 @@ pub fn do_check( let mut expected_state = StateMap::new(); for node in expected_state_ids { - let ev = event_map.get(&node).expect(&format!( - "{} not found in {:?}", - node.to_string(), - event_map.keys().map(ToString::to_string).collect::>(), - )); + let ev = event_map.get(&node).unwrap_or_else(|| { + panic!( + "{} not found in {:?}", + node.to_string(), + event_map.keys().map(ToString::to_string).collect::>() + ) + }); let key = (ev.kind(), ev.state_key()); @@ -334,7 +336,7 @@ pub fn to_init_pdu_event( auth_events: vec![], prev_events: vec![], depth: uint!(0), - hashes: EventHash { sha256: "".into() }, + hashes: EventHash::new("".to_owned()), signatures: btreemap! {}, }), }) @@ -374,7 +376,7 @@ where auth_events, prev_events, depth: uint!(0), - hashes: EventHash { sha256: "".into() }, + hashes: EventHash::new("".to_owned()), signatures: btreemap! {}, }), }) @@ -589,18 +591,24 @@ pub mod event { _ => false, }, Pdu::RoomV3Pdu(event) => event.state_key == Some("".into()), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } pub fn deserialize_content(&self) -> serde_json::Result { match &self.rest { Pdu::RoomV1Pdu(ev) => serde_json::from_value(ev.content.clone()), Pdu::RoomV3Pdu(ev) => serde_json::from_value(ev.content.clone()), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } pub fn origin_server_ts(&self) -> &MilliSecondsSinceUnixEpoch { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.origin_server_ts, Pdu::RoomV3Pdu(ev) => &ev.origin_server_ts, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } pub fn event_id(&self) -> &EventId { @@ -611,6 +619,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.sender, Pdu::RoomV3Pdu(ev) => &ev.sender, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -618,6 +628,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.redacts.as_ref(), Pdu::RoomV3Pdu(ev) => ev.redacts.as_ref(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -625,18 +637,24 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.room_id, Pdu::RoomV3Pdu(ev) => &ev.room_id, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } pub fn kind(&self) -> EventType { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.kind.clone(), Pdu::RoomV3Pdu(ev) => ev.kind.clone(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } pub fn state_key(&self) -> String { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.state_key.clone().unwrap(), Pdu::RoomV3Pdu(ev) => ev.state_key.clone().unwrap(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -645,6 +663,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.origin.clone(), Pdu::RoomV3Pdu(ev) => ev.origin.clone(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -652,6 +672,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.prev_events.iter().map(|(id, _)| id).cloned().collect(), Pdu::RoomV3Pdu(ev) => ev.prev_events.clone(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -659,6 +681,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.auth_events.iter().map(|(id, _)| id).cloned().collect(), Pdu::RoomV3Pdu(ev) => ev.auth_events.to_vec(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -666,6 +690,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.content.clone(), Pdu::RoomV3Pdu(ev) => ev.content.clone(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -673,6 +699,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.unsigned, Pdu::RoomV3Pdu(ev) => &ev.unsigned, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -682,6 +710,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(_) => maplit::btreemap! {}, Pdu::RoomV3Pdu(ev) => ev.signatures.clone(), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -689,6 +719,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.hashes, Pdu::RoomV3Pdu(ev) => &ev.hashes, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -696,6 +728,8 @@ pub mod event { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.depth, Pdu::RoomV3Pdu(ev) => &ev.depth, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -707,6 +741,8 @@ pub mod event { Pdu::RoomV3Pdu(ev) => { ev.kind == ev_type && ev.state_key.as_deref() == Some(state_key) } + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -719,6 +755,8 @@ pub mod event { match self.rest { Pdu::RoomV1Pdu(_) => RoomVersionId::Version1, Pdu::RoomV3Pdu(_) => RoomVersionId::Version6, + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } } diff --git a/crates/ruma/Cargo.toml b/crates/ruma/Cargo.toml index bbeaa22c..8d67f802 100644 --- a/crates/ruma/Cargo.toml +++ b/crates/ruma/Cargo.toml @@ -103,12 +103,14 @@ unstable-exhaustive-types = [ "ruma-federation-api/unstable-exhaustive-types", "ruma-identity-service-api/unstable-exhaustive-types", "ruma-push-gateway-api/unstable-exhaustive-types", + "ruma-state-res/unstable-exhaustive-types" ] unstable-pre-spec = [ "ruma-common/unstable-pre-spec", "ruma-client-api/unstable-pre-spec", "ruma-events/unstable-pre-spec", "ruma-federation-api/unstable-pre-spec", + "ruma-state-res/unstable-pre-spec" #"ruma-identity-service-api/unstable-pre-spec", #"ruma-push-gateway-api/unstable-pre-spec", ]