identifiers: Update ToOwned implementations to use new types

… and a lot of changes that cascaded from that.
This commit is contained in:
Jonas Platte
2022-04-04 19:58:08 +02:00
committed by Jonas Platte
parent ab94bed1dc
commit d855ec33d6
50 changed files with 336 additions and 307 deletions

View File

@@ -30,7 +30,8 @@ use ruma_common::{
},
RoomEventType, StateEventType,
},
room_id, user_id, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, UserId,
room_id, user_id, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, RoomVersionId,
UserId,
};
use ruma_state_res::{self as state_res, Error, Event, Result, StateMap};
use serde_json::{
@@ -157,7 +158,7 @@ criterion_main!(benches);
// IMPLEMENTATION DETAILS AHEAD
//
/////////////////////////////////////////////////////////////////////*/
struct TestStore<E: Event>(HashMap<Box<EventId>, Arc<E>>);
struct TestStore<E: Event>(HashMap<OwnedEventId, Arc<E>>);
#[allow(unused)]
impl<E: Event> TestStore<E> {
@@ -230,7 +231,7 @@ impl TestStore<PduEvent> {
#[allow(clippy::type_complexity)]
fn set_up(
&mut self,
) -> (StateMap<Box<EventId>>, StateMap<Box<EventId>>, StateMap<Box<EventId>>) {
) -> (StateMap<OwnedEventId>, StateMap<OwnedEventId>, StateMap<OwnedEventId>) {
let create_event = to_pdu_event::<&EventId>(
"CREATE",
alice(),
@@ -314,27 +315,27 @@ impl TestStore<PduEvent> {
}
}
fn event_id(id: &str) -> Box<EventId> {
fn event_id(id: &str) -> OwnedEventId {
if id.contains('$') {
return id.try_into().unwrap();
}
format!("${}:foo", id).try_into().unwrap()
}
fn alice() -> Box<UserId> {
user_id!("@alice:foo").to_owned()
fn alice() -> &'static UserId {
user_id!("@alice:foo")
}
fn bob() -> Box<UserId> {
user_id!("@bob:foo").to_owned()
fn bob() -> &'static UserId {
user_id!("@bob:foo")
}
fn charlie() -> Box<UserId> {
user_id!("@charlie:foo").to_owned()
fn charlie() -> &'static UserId {
user_id!("@charlie:foo")
}
fn ella() -> Box<UserId> {
user_id!("@ella:foo").to_owned()
fn ella() -> &'static UserId {
user_id!("@ella:foo")
}
fn room_id() -> &'static RoomId {
@@ -351,7 +352,7 @@ fn member_content_join() -> Box<RawJsonValue> {
fn to_pdu_event<S>(
id: &str,
sender: Box<UserId>,
sender: &UserId,
ev_type: RoomEventType,
state_key: Option<&str>,
content: Box<RawJsonValue>,
@@ -373,7 +374,7 @@ where
event_id: id.try_into().unwrap(),
rest: Pdu::RoomV3Pdu(RoomV3Pdu {
room_id: room_id().to_owned(),
sender,
sender: sender.to_owned(),
origin_server_ts: MilliSecondsSinceUnixEpoch(ts.try_into().unwrap()),
state_key,
kind: ev_type,
@@ -391,7 +392,7 @@ where
// all graphs start with these input events
#[allow(non_snake_case)]
fn INITIAL_EVENTS() -> HashMap<Box<EventId>, Arc<PduEvent>> {
fn INITIAL_EVENTS() -> HashMap<OwnedEventId, Arc<PduEvent>> {
vec![
to_pdu_event::<&EventId>(
"CREATE",
@@ -406,7 +407,7 @@ fn INITIAL_EVENTS() -> HashMap<Box<EventId>, Arc<PduEvent>> {
"IMA",
alice(),
RoomEventType::RoomMember,
Some(alice().to_string().as_str()),
Some(alice().as_str()),
member_content_join(),
&["CREATE"],
&["CREATE"],
@@ -416,7 +417,7 @@ fn INITIAL_EVENTS() -> HashMap<Box<EventId>, Arc<PduEvent>> {
alice(),
RoomEventType::RoomPowerLevels,
Some(""),
to_raw_json_value(&json!({ "users": { alice().to_string(): 100 } })).unwrap(),
to_raw_json_value(&json!({ "users": { alice(): 100 } })).unwrap(),
&["CREATE", "IMA"],
&["IMA"],
),
@@ -473,7 +474,7 @@ fn INITIAL_EVENTS() -> HashMap<Box<EventId>, Arc<PduEvent>> {
// all graphs start with these input events
#[allow(non_snake_case)]
fn BAN_STATE_SET() -> HashMap<Box<EventId>, Arc<PduEvent>> {
fn BAN_STATE_SET() -> HashMap<OwnedEventId, Arc<PduEvent>> {
vec![
to_pdu_event(
"PA",
@@ -531,14 +532,14 @@ impl EventTypeExt for &RoomEventType {
mod event {
use ruma_common::{
events::{pdu::Pdu, RoomEventType},
EventId, MilliSecondsSinceUnixEpoch, RoomId, UserId,
MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, UserId,
};
use ruma_state_res::Event;
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue as RawJsonValue;
impl Event for PduEvent {
type Id = Box<EventId>;
type Id = OwnedEventId;
fn event_id(&self) -> &Self::Id {
&self.event_id
@@ -628,7 +629,7 @@ mod event {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PduEvent {
pub event_id: Box<EventId>,
pub event_id: OwnedEventId,
#[serde(flatten)]
pub rest: Pdu,
}

View File

@@ -1014,9 +1014,9 @@ mod tests {
assert!(valid_membership_change(
&RoomVersion::V6,
&target_user,
target_user,
fetch_state(StateEventType::RoomMember, target_user.to_string()),
&sender,
sender,
fetch_state(StateEventType::RoomMember, sender.to_string()),
&requester,
None::<PduEvent>,
@@ -1056,9 +1056,9 @@ mod tests {
assert!(!valid_membership_change(
&RoomVersion::V6,
&target_user,
target_user,
fetch_state(StateEventType::RoomMember, target_user.to_string()),
&sender,
sender,
fetch_state(StateEventType::RoomMember, sender.to_string()),
&requester,
None::<PduEvent>,
@@ -1098,9 +1098,9 @@ mod tests {
assert!(valid_membership_change(
&RoomVersion::V6,
&target_user,
target_user,
fetch_state(StateEventType::RoomMember, target_user.to_string()),
&sender,
sender,
fetch_state(StateEventType::RoomMember, sender.to_string()),
&requester,
None::<PduEvent>,
@@ -1140,9 +1140,9 @@ mod tests {
assert!(!valid_membership_change(
&RoomVersion::V6,
&target_user,
target_user,
fetch_state(StateEventType::RoomMember, target_user.to_string()),
&sender,
sender,
fetch_state(StateEventType::RoomMember, sender.to_string()),
&requester,
None::<PduEvent>,
@@ -1176,7 +1176,7 @@ mod tests {
);
let mut member = RoomMemberEventContent::new(MembershipState::Join);
member.join_authorized_via_users_server = Some(alice());
member.join_authorized_via_users_server = Some(alice().to_owned());
let auth_events = events
.values()
@@ -1199,15 +1199,15 @@ mod tests {
assert!(valid_membership_change(
&RoomVersion::V9,
&target_user,
target_user,
fetch_state(StateEventType::RoomMember, target_user.to_string()),
&sender,
sender,
fetch_state(StateEventType::RoomMember, sender.to_string()),
&requester,
None::<PduEvent>,
fetch_state(StateEventType::RoomPowerLevels, "".to_owned()),
fetch_state(StateEventType::RoomJoinRules, "".to_owned()),
Some(&alice()),
Some(alice()),
&MembershipState::Join,
fetch_state(StateEventType::RoomCreate, "".to_owned()).unwrap(),
)
@@ -1215,15 +1215,15 @@ mod tests {
assert!(!valid_membership_change(
&RoomVersion::V9,
&target_user,
target_user,
fetch_state(StateEventType::RoomMember, target_user.to_string()),
&sender,
sender,
fetch_state(StateEventType::RoomMember, sender.to_string()),
&requester,
None::<PduEvent>,
fetch_state(StateEventType::RoomPowerLevels, "".to_owned()),
fetch_state(StateEventType::RoomJoinRules, "".to_owned()),
Some(&ella()),
Some(ella()),
&MembershipState::Leave,
fetch_state(StateEventType::RoomCreate, "".to_owned()).unwrap(),
)
@@ -1266,9 +1266,9 @@ mod tests {
assert!(valid_membership_change(
&RoomVersion::V7,
&target_user,
target_user,
fetch_state(StateEventType::RoomMember, target_user.to_string()),
&sender,
sender,
fetch_state(StateEventType::RoomMember, sender.to_string()),
&requester,
None::<PduEvent>,

View File

@@ -667,7 +667,7 @@ mod tests {
room::join_rules::{JoinRule, RoomJoinRulesEventContent},
RoomEventType, StateEventType,
},
EventId, MilliSecondsSinceUnixEpoch, RoomVersionId,
MilliSecondsSinceUnixEpoch, OwnedEventId, RoomVersionId,
};
use serde_json::{json, value::to_raw_value as to_raw_json_value};
use tracing::debug;
@@ -692,7 +692,7 @@ mod tests {
.map(|ev| (ev.event_type().with_state_key(ev.state_key().unwrap()), ev.clone()))
.collect::<StateMap<_>>();
let auth_chain: HashSet<Box<EventId>> = HashSet::new();
let auth_chain: HashSet<OwnedEventId> = HashSet::new();
let power_events = event_map
.values()
@@ -1222,7 +1222,7 @@ mod tests {
}
#[allow(non_snake_case)]
fn BAN_STATE_SET() -> HashMap<Box<EventId>, Arc<PduEvent>> {
fn BAN_STATE_SET() -> HashMap<OwnedEventId, Arc<PduEvent>> {
vec![
to_pdu_event(
"PA",
@@ -1267,7 +1267,7 @@ mod tests {
}
#[allow(non_snake_case)]
fn JOIN_RULE() -> HashMap<Box<EventId>, Arc<PduEvent>> {
fn JOIN_RULE() -> HashMap<OwnedEventId, Arc<PduEvent>> {
vec![
to_pdu_event(
"JR",

View File

@@ -19,7 +19,8 @@ use ruma_common::{
},
RoomEventType,
},
room_id, user_id, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, UserId,
room_id, user_id, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, RoomVersionId,
UserId,
};
use serde_json::{
json,
@@ -35,8 +36,8 @@ static SERVER_TIMESTAMP: AtomicU64 = AtomicU64::new(0);
pub fn do_check(
events: &[Arc<PduEvent>],
edges: Vec<Vec<Box<EventId>>>,
expected_state_ids: Vec<Box<EventId>>,
edges: Vec<Vec<OwnedEventId>>,
expected_state_ids: Vec<OwnedEventId>,
) {
// To activate logging use `RUST_LOG=debug cargo t`
@@ -76,10 +77,10 @@ pub fn do_check(
}
}
// event_id -> OriginalStateEvent
let mut event_map: HashMap<Box<EventId>, Arc<PduEvent>> = HashMap::new();
// event_id -> StateMap<Box<EventId>>
let mut state_at_event: HashMap<Box<EventId>, StateMap<Box<EventId>>> = HashMap::new();
// event_id -> PduEvent
let mut event_map: HashMap<OwnedEventId, Arc<PduEvent>> = HashMap::new();
// event_id -> StateMap<OwnedEventId>
let mut state_at_event: HashMap<OwnedEventId, StateMap<OwnedEventId>> = HashMap::new();
// Resolve the current state and add it to the state_at_event map then continue
// on in "time"
@@ -93,7 +94,7 @@ pub fn do_check(
let prev_events = graph.get(&node).unwrap();
let state_before: StateMap<Box<EventId>> = if prev_events.is_empty() {
let state_before: StateMap<OwnedEventId> = if prev_events.is_empty() {
HashMap::new()
} else if prev_events.len() == 1 {
state_at_event.get(prev_events.iter().next().unwrap()).unwrap().clone()
@@ -155,7 +156,7 @@ pub fn do_check(
let ev_id = e.event_id();
let event = to_pdu_event(
e.event_id().as_str(),
e.sender().to_owned(),
e.sender(),
e.event_type().clone(),
e.state_key(),
e.content().to_owned(),
@@ -201,13 +202,13 @@ pub fn do_check(
&& **k != ("m.room.message".into(), "dummy".to_owned())
})
.map(|(k, v)| (k.clone(), v.clone()))
.collect::<StateMap<Box<EventId>>>();
.collect::<StateMap<OwnedEventId>>();
assert_eq!(expected_state, end_state);
}
#[allow(clippy::exhaustive_structs)]
pub struct TestStore<E: Event>(pub HashMap<Box<EventId>, Arc<E>>);
pub struct TestStore<E: Event>(pub HashMap<OwnedEventId, Arc<E>>);
impl<E: Event> TestStore<E> {
pub fn get_event(&self, _: &RoomId, event_id: &EventId) -> Result<Arc<E>> {
@@ -248,7 +249,7 @@ impl<E: Event> TestStore<E> {
impl TestStore<PduEvent> {
pub fn set_up(
&mut self,
) -> (StateMap<Box<EventId>>, StateMap<Box<EventId>>, StateMap<Box<EventId>>) {
) -> (StateMap<OwnedEventId>, StateMap<OwnedEventId>, StateMap<OwnedEventId>) {
let create_event = to_pdu_event::<&EventId>(
"CREATE",
alice(),
@@ -265,7 +266,7 @@ impl TestStore<PduEvent> {
"IMA",
alice(),
RoomEventType::RoomMember,
Some(alice().to_string().as_str()),
Some(alice().as_str()),
member_content_join(),
&[cre.clone()],
&[cre.clone()],
@@ -289,7 +290,7 @@ impl TestStore<PduEvent> {
"IMB",
bob(),
RoomEventType::RoomMember,
Some(bob().to_string().as_str()),
Some(bob().as_str()),
member_content_join(),
&[cre.clone(), join_rules.event_id().to_owned()],
&[join_rules.event_id().to_owned()],
@@ -300,7 +301,7 @@ impl TestStore<PduEvent> {
"IMC",
charlie(),
RoomEventType::RoomMember,
Some(charlie().to_string().as_str()),
Some(charlie().as_str()),
member_content_join(),
&[cre, join_rules.event_id().to_owned()],
&[join_rules.event_id().to_owned()],
@@ -332,7 +333,7 @@ impl TestStore<PduEvent> {
}
}
pub fn event_id(id: &str) -> Box<EventId> {
pub fn event_id(id: &str) -> OwnedEventId {
if id.contains('$') {
return id.try_into().unwrap();
}
@@ -340,24 +341,24 @@ pub fn event_id(id: &str) -> Box<EventId> {
format!("${}:foo", id).try_into().unwrap()
}
pub fn alice() -> Box<UserId> {
user_id!("@alice:foo").to_owned()
pub fn alice() -> &'static UserId {
user_id!("@alice:foo")
}
pub fn bob() -> Box<UserId> {
user_id!("@bob:foo").to_owned()
pub fn bob() -> &'static UserId {
user_id!("@bob:foo")
}
pub fn charlie() -> Box<UserId> {
user_id!("@charlie:foo").to_owned()
pub fn charlie() -> &'static UserId {
user_id!("@charlie:foo")
}
pub fn ella() -> Box<UserId> {
user_id!("@ella:foo").to_owned()
pub fn ella() -> &'static UserId {
user_id!("@ella:foo")
}
pub fn zara() -> Box<UserId> {
user_id!("@zara:foo").to_owned()
pub fn zara() -> &'static UserId {
user_id!("@zara:foo")
}
pub fn room_id() -> &'static RoomId {
@@ -374,7 +375,7 @@ pub fn member_content_join() -> Box<RawJsonValue> {
pub fn to_init_pdu_event(
id: &str,
sender: Box<UserId>,
sender: &UserId,
ev_type: RoomEventType,
state_key: Option<&str>,
content: Box<RawJsonValue>,
@@ -387,7 +388,7 @@ pub fn to_init_pdu_event(
event_id: id.try_into().unwrap(),
rest: Pdu::RoomV3Pdu(RoomV3Pdu {
room_id: room_id().to_owned(),
sender,
sender: sender.to_owned(),
origin_server_ts: MilliSecondsSinceUnixEpoch(ts.try_into().unwrap()),
state_key,
kind: ev_type,
@@ -405,7 +406,7 @@ pub fn to_init_pdu_event(
pub fn to_pdu_event<S>(
id: &str,
sender: Box<UserId>,
sender: &UserId,
ev_type: RoomEventType,
state_key: Option<&str>,
content: Box<RawJsonValue>,
@@ -425,7 +426,7 @@ where
event_id: id.try_into().unwrap(),
rest: Pdu::RoomV3Pdu(RoomV3Pdu {
room_id: room_id().to_owned(),
sender,
sender: sender.to_owned(),
origin_server_ts: MilliSecondsSinceUnixEpoch(ts.try_into().unwrap()),
state_key,
kind: ev_type,
@@ -443,7 +444,7 @@ where
// all graphs start with these input events
#[allow(non_snake_case)]
pub fn INITIAL_EVENTS() -> HashMap<Box<EventId>, Arc<PduEvent>> {
pub fn INITIAL_EVENTS() -> HashMap<OwnedEventId, Arc<PduEvent>> {
vec![
to_pdu_event::<&EventId>(
"CREATE",
@@ -458,7 +459,7 @@ pub fn INITIAL_EVENTS() -> HashMap<Box<EventId>, Arc<PduEvent>> {
"IMA",
alice(),
RoomEventType::RoomMember,
Some(alice().to_string().as_str()),
Some(alice().as_str()),
member_content_join(),
&["CREATE"],
&["CREATE"],
@@ -468,7 +469,7 @@ pub fn INITIAL_EVENTS() -> HashMap<Box<EventId>, Arc<PduEvent>> {
alice(),
RoomEventType::RoomPowerLevels,
Some(""),
to_raw_json_value(&json!({ "users": { alice().to_string(): 100 } })).unwrap(),
to_raw_json_value(&json!({ "users": { alice(): 100 } })).unwrap(),
&["CREATE", "IMA"],
&["IMA"],
),
@@ -485,7 +486,7 @@ pub fn INITIAL_EVENTS() -> HashMap<Box<EventId>, Arc<PduEvent>> {
"IMB",
bob(),
RoomEventType::RoomMember,
Some(bob().to_string().as_str()),
Some(bob().as_str()),
member_content_join(),
&["CREATE", "IJR", "IPOWER"],
&["IJR"],
@@ -494,7 +495,7 @@ pub fn INITIAL_EVENTS() -> HashMap<Box<EventId>, Arc<PduEvent>> {
"IMC",
charlie(),
RoomEventType::RoomMember,
Some(charlie().to_string().as_str()),
Some(charlie().as_str()),
member_content_join(),
&["CREATE", "IJR", "IPOWER"],
&["IMB"],
@@ -525,7 +526,7 @@ pub fn INITIAL_EVENTS() -> HashMap<Box<EventId>, Arc<PduEvent>> {
// all graphs start with these input events
#[allow(non_snake_case)]
pub fn INITIAL_EVENTS_CREATE_ROOM() -> HashMap<Box<EventId>, Arc<PduEvent>> {
pub fn INITIAL_EVENTS_CREATE_ROOM() -> HashMap<OwnedEventId, Arc<PduEvent>> {
vec![to_pdu_event::<&EventId>(
"CREATE",
alice(),
@@ -541,7 +542,7 @@ pub fn INITIAL_EVENTS_CREATE_ROOM() -> HashMap<Box<EventId>, Arc<PduEvent>> {
}
#[allow(non_snake_case)]
pub fn INITIAL_EDGES() -> Vec<Box<EventId>> {
pub fn INITIAL_EDGES() -> Vec<OwnedEventId> {
vec!["START", "IMC", "IMB", "IJR", "IPOWER", "IMA", "CREATE"]
.into_iter()
.map(event_id)
@@ -551,7 +552,7 @@ pub fn INITIAL_EDGES() -> Vec<Box<EventId>> {
pub mod event {
use ruma_common::{
events::{pdu::Pdu, RoomEventType},
EventId, MilliSecondsSinceUnixEpoch, RoomId, UserId,
MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, UserId,
};
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue as RawJsonValue;
@@ -559,7 +560,7 @@ pub mod event {
use crate::Event;
impl Event for PduEvent {
type Id = Box<EventId>;
type Id = OwnedEventId;
fn event_id(&self) -> &Self::Id {
&self.event_id
@@ -650,7 +651,7 @@ pub mod event {
#[derive(Clone, Debug, Deserialize, Serialize)]
#[allow(clippy::exhaustive_structs)]
pub struct PduEvent {
pub event_id: Box<EventId>,
pub event_id: OwnedEventId,
#[serde(flatten)]
pub rest: Pdu,
}