From efe48af20747699440567b9a087302b3979294b5 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 14 Apr 2022 20:22:52 +0200 Subject: [PATCH] Replace remaining uses of boxed IDs with dedicated owned ones --- crates/ruma-client-api/src/membership.rs | 6 +-- crates/ruma-common/src/events.rs | 8 ++-- crates/ruma-common/src/events/direct.rs | 6 +-- .../src/events/room/pinned_events.rs | 12 ++--- .../src/identifiers/client_secret.rs | 5 ++- .../ruma-common/src/identifiers/device_id.rs | 15 ++++--- .../src/identifiers/device_key_id.rs | 8 ++-- .../ruma-common/src/identifiers/event_id.rs | 14 +++--- crates/ruma-common/src/identifiers/key_id.rs | 4 +- .../ruma-common/src/identifiers/matrix_uri.rs | 13 +++--- .../src/identifiers/room_alias_id.rs | 4 +- crates/ruma-common/src/identifiers/room_id.rs | 10 +++-- .../src/identifiers/room_or_room_alias_id.rs | 44 ++++++++++--------- .../ruma-common/src/identifiers/signatures.rs | 12 ++--- .../src/identifiers/transaction_id.rs | 5 ++- crates/ruma-common/src/identifiers/user_id.rs | 22 ++++++---- .../tests/api/manual_endpoint_impl.rs | 8 ++-- .../ruma-common/tests/api/ruma_api_macros.rs | 4 +- .../ruma-common/tests/api/ui/03-move-value.rs | 12 ++--- crates/ruma-federation-api/src/discovery.rs | 10 +++-- .../discovery/get_remote_server_keys_batch.rs | 7 +-- .../src/keys/claim_keys.rs | 8 ++-- .../src/thirdparty/bind_callback.rs | 8 ++-- 23 files changed, 134 insertions(+), 111 deletions(-) diff --git a/crates/ruma-client-api/src/membership.rs b/crates/ruma-client-api/src/membership.rs index 9e079394..ef290d0e 100644 --- a/crates/ruma-client-api/src/membership.rs +++ b/crates/ruma-client-api/src/membership.rs @@ -15,7 +15,7 @@ pub mod unban_user; use std::collections::BTreeMap; use ruma_common::{ - serde::Incoming, thirdparty::Medium, OwnedServerName, ServerSigningKeyId, UserId, + serde::Incoming, thirdparty::Medium, OwnedServerName, OwnedServerSigningKeyId, UserId, }; use serde::Serialize; @@ -34,7 +34,7 @@ pub struct ThirdPartySigned<'a> { pub token: &'a str, /// A signatures object containing a signature of the entire signed object. - pub signatures: BTreeMap, String>>, + pub signatures: BTreeMap>, } impl<'a> ThirdPartySigned<'a> { @@ -44,7 +44,7 @@ impl<'a> ThirdPartySigned<'a> { sender: &'a UserId, mxid: &'a UserId, token: &'a str, - signatures: BTreeMap, String>>, + signatures: BTreeMap>, ) -> Self { Self { sender, mxid, token, signatures } } diff --git a/crates/ruma-common/src/events.rs b/crates/ruma-common/src/events.rs index 35e5f460..1520b7f7 100644 --- a/crates/ruma-common/src/events.rs +++ b/crates/ruma-common/src/events.rs @@ -41,8 +41,10 @@ //! [`OriginalSyncMessageLikeEvent`] struct but any [`OriginalMessageLikeEvent`] struct would work. //! //! ```rust -//! use ruma_common::events::{macros::EventContent, OriginalSyncMessageLikeEvent}; -//! use ruma_common::EventId; +//! use ruma_common::{ +//! events::{macros::EventContent, OriginalSyncMessageLikeEvent}, +//! OwnedEventId, +//! }; //! use serde::{Deserialize, Serialize}; //! //! #[derive(Clone, Debug, Deserialize, Serialize)] @@ -51,7 +53,7 @@ //! #[serde(rename = "m.annotation")] //! Annotation { //! /// The event this reaction relates to. -//! event_id: Box, +//! event_id: OwnedEventId, //! /// The displayable content of the reaction. //! key: String, //! }, diff --git a/crates/ruma-common/src/events/direct.rs b/crates/ruma-common/src/events/direct.rs index 82318432..d0ebe4b6 100644 --- a/crates/ruma-common/src/events/direct.rs +++ b/crates/ruma-common/src/events/direct.rs @@ -10,7 +10,7 @@ use std::{ use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; -use crate::{RoomId, UserId}; +use crate::{OwnedRoomId, OwnedUserId}; /// The content of an `m.direct` event. /// @@ -21,10 +21,10 @@ use crate::{RoomId, UserId}; #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] #[allow(clippy::exhaustive_structs)] #[ruma_event(type = "m.direct", kind = GlobalAccountData)] -pub struct DirectEventContent(pub BTreeMap, Vec>>); +pub struct DirectEventContent(pub BTreeMap>); impl Deref for DirectEventContent { - type Target = BTreeMap, Vec>>; + type Target = BTreeMap>; fn deref(&self) -> &Self::Target { &self.0 diff --git a/crates/ruma-common/src/events/room/pinned_events.rs b/crates/ruma-common/src/events/room/pinned_events.rs index ed00bed3..c531c0de 100644 --- a/crates/ruma-common/src/events/room/pinned_events.rs +++ b/crates/ruma-common/src/events/room/pinned_events.rs @@ -5,7 +5,7 @@ use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; -use crate::EventId; +use crate::OwnedEventId; /// The content of an `m.room.pinned_events` event. /// @@ -15,12 +15,12 @@ use crate::EventId; #[ruma_event(type = "m.room.pinned_events", kind = State)] pub struct RoomPinnedEventsEventContent { /// An ordered list of event IDs to pin. - pub pinned: Vec>, + pub pinned: Vec, } impl RoomPinnedEventsEventContent { /// Creates a new `RoomPinnedEventsEventContent` with the given events. - pub fn new(pinned: Vec>) -> Self { + pub fn new(pinned: Vec) -> Self { Self { pinned } } } @@ -45,10 +45,10 @@ mod tests { let event = OriginalStateEvent { content: content.clone(), - event_id: EventId::new(server_name).into(), + event_id: EventId::new(server_name), origin_server_ts: MilliSecondsSinceUnixEpoch(1_432_804_485_886_u64.try_into().unwrap()), - room_id: RoomId::new(server_name).into(), - sender: UserId::new(server_name).into(), + room_id: RoomId::new(server_name), + sender: UserId::new(server_name), state_key: "".into(), unsigned: StateUnsigned::default(), }; diff --git a/crates/ruma-common/src/identifiers/client_secret.rs b/crates/ruma-common/src/identifiers/client_secret.rs index 7b68bd1c..87e6fe22 100644 --- a/crates/ruma-common/src/identifiers/client_secret.rs +++ b/crates/ruma-common/src/identifiers/client_secret.rs @@ -21,9 +21,10 @@ impl ClientSecret { /// This will currently be a UUID without hyphens, but no guarantees are made about the /// structure of client secrets generated from this function. #[cfg(feature = "rand")] - pub fn new() -> Box { + #[allow(clippy::new_ret_no_self)] + pub fn new() -> OwnedClientSecret { let id = uuid::Uuid::new_v4(); - Self::from_box(id.to_simple().to_string().into_boxed_str()) + ClientSecret::from_borrowed(&id.to_simple().to_string()).to_owned() } } diff --git a/crates/ruma-common/src/identifiers/device_id.rs b/crates/ruma-common/src/identifiers/device_id.rs index 4c275e68..11a7e514 100644 --- a/crates/ruma-common/src/identifiers/device_id.rs +++ b/crates/ruma-common/src/identifiers/device_id.rs @@ -11,7 +11,7 @@ use super::generate_localpart; /// # Example /// /// ``` -/// use ruma_common::{device_id, DeviceId}; +/// use ruma_common::{device_id, DeviceId, OwnedDeviceId}; /// /// # #[cfg(feature = "rand")] { /// let random_id = DeviceId::new(); @@ -24,7 +24,7 @@ use super::generate_localpart; /// let ref_id: &DeviceId = "abcdefghi".into(); /// assert_eq!(ref_id.as_str(), "abcdefghi"); /// -/// let owned_id: Box = "ijklmnop".into(); +/// let owned_id: OwnedDeviceId = "ijklmnop".into(); /// assert_eq!(owned_id.as_str(), "ijklmnop"); /// ``` #[repr(transparent)] @@ -34,14 +34,15 @@ pub struct DeviceId(str); impl DeviceId { /// Generates a random `DeviceId`, suitable for assignment to a new device. #[cfg(feature = "rand")] - pub fn new() -> Box { - Self::from_box(generate_localpart(8)) + #[allow(clippy::new_ret_no_self)] + pub fn new() -> OwnedDeviceId { + Self::from_borrowed(&generate_localpart(8)).to_owned() } } #[cfg(all(test, feature = "rand"))] mod tests { - use super::DeviceId; + use super::{DeviceId, OwnedDeviceId}; #[test] fn generate_device_id() { @@ -56,14 +57,14 @@ mod tests { #[test] fn create_boxed_device_id_from_str() { - let box_id: Box = "12345678".into(); + let box_id: OwnedDeviceId = "12345678".into(); assert_eq!(box_id.as_str(), "12345678"); } #[test] fn create_device_id_from_box() { let box_str: Box = "ijklmnop".into(); - let device_id: Box = box_str.into(); + let device_id: OwnedDeviceId = box_str.into(); assert_eq!(device_id.as_str(), "ijklmnop"); } } diff --git a/crates/ruma-common/src/identifiers/device_key_id.rs b/crates/ruma-common/src/identifiers/device_key_id.rs index d13a9ca5..42ab93d8 100644 --- a/crates/ruma-common/src/identifiers/device_key_id.rs +++ b/crates/ruma-common/src/identifiers/device_key_id.rs @@ -12,7 +12,7 @@ pub struct DeviceKeyId(str); impl DeviceKeyId { /// Create a `DeviceKeyId` from a `DeviceKeyAlgorithm` and a `DeviceId`. - pub fn from_parts(algorithm: DeviceKeyAlgorithm, device_id: &DeviceId) -> Box { + pub fn from_parts(algorithm: DeviceKeyAlgorithm, device_id: &DeviceId) -> OwnedDeviceKeyId { let algorithm: &str = algorithm.as_ref(); let device_id: &str = device_id.as_ref(); @@ -21,7 +21,7 @@ impl DeviceKeyId { res.push(':'); res.push_str(device_id); - Self::from_box(res.into()) + Self::from_borrowed(&res).to_owned() } /// Returns key algorithm of the device key ID. @@ -43,7 +43,7 @@ impl DeviceKeyId { mod tests { use std::convert::TryFrom; - use super::DeviceKeyId; + use super::{DeviceKeyId, OwnedDeviceKeyId}; use crate::identifiers::{crypto_algorithms::DeviceKeyAlgorithm, IdParseError}; #[test] @@ -65,7 +65,7 @@ mod tests { #[test] fn deserialize_device_key_id() { - let deserialized: Box = + let deserialized: OwnedDeviceKeyId = serde_json::from_value(serde_json::json!("ed25519:JLAFKJWSCS")).unwrap(); let expected = <&DeviceKeyId>::try_from("ed25519:JLAFKJWSCS").unwrap(); diff --git a/crates/ruma-common/src/identifiers/event_id.rs b/crates/ruma-common/src/identifiers/event_id.rs index 69698ae4..917133e1 100644 --- a/crates/ruma-common/src/identifiers/event_id.rs +++ b/crates/ruma-common/src/identifiers/event_id.rs @@ -48,8 +48,10 @@ impl EventId { /// This should only be used for events in the original format as used by Matrix room versions /// 1 and 2. #[cfg(feature = "rand")] - pub fn new(server_name: &ServerName) -> Box { - Self::from_box(format!("${}:{}", super::generate_localpart(18), server_name).into()) + #[allow(clippy::new_ret_no_self)] + pub fn new(server_name: &ServerName) -> OwnedEventId { + Self::from_borrowed(&format!("${}:{}", super::generate_localpart(18), server_name)) + .to_owned() } /// Returns the event's unique ID. @@ -78,7 +80,7 @@ impl EventId { mod tests { use std::convert::TryFrom; - use super::EventId; + use super::{EventId, OwnedEventId}; use crate::IdParseError; #[test] @@ -158,7 +160,7 @@ mod tests { #[test] fn deserialize_valid_original_event_id() { assert_eq!( - serde_json::from_str::>(r#""$39hvsi03hlne:example.com""#) + serde_json::from_str::(r#""$39hvsi03hlne:example.com""#) .expect("Failed to convert JSON to EventId"), <&EventId>::try_from("$39hvsi03hlne:example.com").expect("Failed to create EventId.") ); @@ -167,7 +169,7 @@ mod tests { #[test] fn deserialize_valid_base64_event_id() { assert_eq!( - serde_json::from_str::>( + serde_json::from_str::( r#""$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk""# ) .expect("Failed to convert JSON to EventId"), @@ -179,7 +181,7 @@ mod tests { #[test] fn deserialize_valid_url_safe_base64_event_id() { assert_eq!( - serde_json::from_str::>( + serde_json::from_str::( r#""$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg""# ) .expect("Failed to convert JSON to EventId"), diff --git a/crates/ruma-common/src/identifiers/key_id.rs b/crates/ruma-common/src/identifiers/key_id.rs index 359ea735..04b3b004 100644 --- a/crates/ruma-common/src/identifiers/key_id.rs +++ b/crates/ruma-common/src/identifiers/key_id.rs @@ -17,7 +17,7 @@ pub struct KeyId(PhantomData<(A, K)>, str); impl KeyId { /// Creates a new `KeyId` from an algorithm and key name. - pub fn from_parts(algorithm: A, key_name: &K) -> Box + pub fn from_parts(algorithm: A, key_name: &K) -> OwnedKeyId where A: AsRef, K: AsRef, @@ -30,7 +30,7 @@ impl KeyId { res.push(':'); res.push_str(key_name); - Self::from_box(res.into()) + Self::from_borrowed(&res).to_owned() } /// Returns key algorithm of the key ID. diff --git a/crates/ruma-common/src/identifiers/matrix_uri.rs b/crates/ruma-common/src/identifiers/matrix_uri.rs index b18a21f9..988a50b0 100644 --- a/crates/ruma-common/src/identifiers/matrix_uri.rs +++ b/crates/ruma-common/src/identifiers/matrix_uri.rs @@ -9,7 +9,10 @@ use ruma_identifiers_validation::{ }; use url::Url; -use super::{EventId, OwnedServerName, RoomAliasId, RoomId, RoomOrAliasId, ServerName, UserId}; +use super::{ + EventId, OwnedEventId, OwnedRoomAliasId, OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, + OwnedUserId, RoomAliasId, RoomId, RoomOrAliasId, ServerName, UserId, +}; use crate::PrivOwnedStr; const MATRIX_TO_BASE_URL: &str = "https://matrix.to/#/"; @@ -46,16 +49,16 @@ const RESERVED: &AsciiSet = &CONTROLS #[non_exhaustive] pub enum MatrixId { /// A room ID. - Room(Box), + Room(OwnedRoomId), /// A room alias. - RoomAlias(Box), + RoomAlias(OwnedRoomAliasId), /// A user ID. - User(Box), + User(OwnedUserId), /// An event ID. - Event(Box, Box), + Event(OwnedRoomOrAliasId, OwnedEventId), } impl MatrixId { diff --git a/crates/ruma-common/src/identifiers/room_alias_id.rs b/crates/ruma-common/src/identifiers/room_alias_id.rs index cf5101a1..3a02f413 100644 --- a/crates/ruma-common/src/identifiers/room_alias_id.rs +++ b/crates/ruma-common/src/identifiers/room_alias_id.rs @@ -63,7 +63,7 @@ impl RoomAliasId { mod tests { use std::convert::TryFrom; - use super::RoomAliasId; + use super::{OwnedRoomAliasId, RoomAliasId}; use crate::IdParseError; #[test] @@ -97,7 +97,7 @@ mod tests { #[test] fn deserialize_valid_room_alias_id() { assert_eq!( - serde_json::from_str::>(r##""#ruma:example.com""##) + serde_json::from_str::(r##""#ruma:example.com""##) .expect("Failed to convert JSON to RoomAliasId"), <&RoomAliasId>::try_from("#ruma:example.com").expect("Failed to create RoomAliasId.") ); diff --git a/crates/ruma-common/src/identifiers/room_id.rs b/crates/ruma-common/src/identifiers/room_id.rs index 5cb638fa..02c38e22 100644 --- a/crates/ruma-common/src/identifiers/room_id.rs +++ b/crates/ruma-common/src/identifiers/room_id.rs @@ -27,8 +27,10 @@ impl RoomId { /// /// Fails if the given homeserver cannot be parsed as a valid host. #[cfg(feature = "rand")] - pub fn new(server_name: &ServerName) -> Box { - Self::from_box(format!("!{}:{}", super::generate_localpart(18), server_name).into()) + #[allow(clippy::new_ret_no_self)] + pub fn new(server_name: &ServerName) -> OwnedRoomId { + Self::from_borrowed(&format!("!{}:{}", super::generate_localpart(18), server_name)) + .to_owned() } /// Returns the rooms's unique ID. @@ -110,7 +112,7 @@ impl RoomId { mod tests { use std::convert::TryFrom; - use super::RoomId; + use super::{OwnedRoomId, RoomId}; use crate::IdParseError; #[test] @@ -157,7 +159,7 @@ mod tests { #[test] fn deserialize_valid_room_id() { assert_eq!( - serde_json::from_str::>(r#""!29fhd83h92h0:example.com""#) + serde_json::from_str::(r#""!29fhd83h92h0:example.com""#) .expect("Failed to convert JSON to RoomId"), <&RoomId>::try_from("!29fhd83h92h0:example.com").expect("Failed to create RoomId.") ); diff --git a/crates/ruma-common/src/identifiers/room_or_room_alias_id.rs b/crates/ruma-common/src/identifiers/room_or_room_alias_id.rs index 3a1e60ae..cbbdb1b2 100644 --- a/crates/ruma-common/src/identifiers/room_or_room_alias_id.rs +++ b/crates/ruma-common/src/identifiers/room_or_room_alias_id.rs @@ -4,7 +4,7 @@ use std::{convert::TryFrom, hint::unreachable_unchecked}; use ruma_macros::IdZst; -use super::{server_name::ServerName, RoomAliasId, RoomId}; +use super::{server_name::ServerName, OwnedRoomAliasId, OwnedRoomId, RoomAliasId, RoomId}; /// A Matrix [room ID] or a Matrix [room alias ID]. /// @@ -82,15 +82,17 @@ impl<'a> From<&'a RoomAliasId> for &'a RoomOrAliasId { } } -impl From> for Box { - fn from(room_id: Box) -> Self { - RoomOrAliasId::from_box(room_id.into_owned()) +impl From for OwnedRoomOrAliasId { + fn from(room_id: OwnedRoomId) -> Self { + // FIXME: Don't allocate + RoomOrAliasId::from_borrowed(room_id.as_str()).to_owned() } } -impl From> for Box { - fn from(room_alias_id: Box) -> Self { - RoomOrAliasId::from_box(room_alias_id.into_owned()) +impl From for OwnedRoomOrAliasId { + fn from(room_alias_id: OwnedRoomAliasId) -> Self { + // FIXME: Don't allocate + RoomOrAliasId::from_borrowed(room_alias_id.as_str()).to_owned() } } @@ -116,24 +118,26 @@ impl<'a> TryFrom<&'a RoomOrAliasId> for &'a RoomAliasId { } } -impl TryFrom> for Box { - type Error = Box; +impl TryFrom for OwnedRoomId { + type Error = OwnedRoomAliasId; - fn try_from(id: Box) -> Result, Box> { + fn try_from(id: OwnedRoomOrAliasId) -> Result { + // FIXME: Don't allocate match id.variant() { - Variant::RoomId => Ok(RoomId::from_box(id.into_owned())), - Variant::RoomAliasId => Err(RoomAliasId::from_box(id.into_owned())), + Variant::RoomId => Ok(RoomId::from_borrowed(id.as_str()).to_owned()), + Variant::RoomAliasId => Err(RoomAliasId::from_borrowed(id.as_str()).to_owned()), } } } -impl TryFrom> for Box { - type Error = Box; +impl TryFrom for OwnedRoomAliasId { + type Error = OwnedRoomId; - fn try_from(id: Box) -> Result, Box> { + fn try_from(id: OwnedRoomOrAliasId) -> Result { + // FIXME: Don't allocate match id.variant() { - Variant::RoomAliasId => Ok(RoomAliasId::from_box(id.into_owned())), - Variant::RoomId => Err(RoomId::from_box(id.into_owned())), + Variant::RoomAliasId => Ok(RoomAliasId::from_borrowed(id.as_str()).to_owned()), + Variant::RoomId => Err(RoomId::from_borrowed(id.as_str()).to_owned()), } } } @@ -142,7 +146,7 @@ impl TryFrom> for Box { mod tests { use std::convert::TryFrom; - use super::RoomOrAliasId; + use super::{OwnedRoomOrAliasId, RoomOrAliasId}; use crate::IdParseError; #[test] @@ -200,7 +204,7 @@ mod tests { #[test] fn deserialize_valid_room_id_or_alias_id_with_a_room_alias_id() { assert_eq!( - serde_json::from_str::>(r##""#ruma:example.com""##) + serde_json::from_str::(r##""#ruma:example.com""##) .expect("Failed to convert JSON to RoomAliasId"), <&RoomOrAliasId>::try_from("#ruma:example.com").expect("Failed to create RoomAliasId.") ); @@ -209,7 +213,7 @@ mod tests { #[test] fn deserialize_valid_room_id_or_alias_id_with_a_room_id() { assert_eq!( - serde_json::from_str::>(r##""!29fhd83h92h0:example.com""##) + serde_json::from_str::(r##""!29fhd83h92h0:example.com""##) .expect("Failed to convert JSON to RoomId"), <&RoomOrAliasId>::try_from("!29fhd83h92h0:example.com") .expect("Failed to create RoomAliasId.") diff --git a/crates/ruma-common/src/identifiers/signatures.rs b/crates/ruma-common/src/identifiers/signatures.rs index fb343dbc..03a11ed1 100644 --- a/crates/ruma-common/src/identifiers/signatures.rs +++ b/crates/ruma-common/src/identifiers/signatures.rs @@ -2,15 +2,15 @@ use std::{borrow::Borrow, collections::BTreeMap}; use serde::{Deserialize, Serialize}; -use super::{DeviceId, KeyName, ServerName, SigningKeyId, UserId}; +use super::{OwnedDeviceId, OwnedKeyName, OwnedServerName, OwnedSigningKeyId, OwnedUserId}; /// Map of key identifier to signature values. -pub type EntitySignatures = BTreeMap>, String>; +pub type EntitySignatures = BTreeMap, String>; /// Map of all signatures, grouped by entity /// /// ``` -/// # use ruma_common::{server_name, {KeyId, Signatures, SigningKeyAlgorithm}}; +/// # use ruma_common::{server_name, KeyId, Signatures, SigningKeyAlgorithm}; /// let key_identifier = KeyId::from_parts(SigningKeyAlgorithm::Ed25519, "1"); /// let mut signatures = Signatures::new(); /// let server_name = server_name!("example.org"); @@ -34,7 +34,7 @@ impl Signatures { pub fn insert( &mut self, entity: E, - key_identifier: Box>, + key_identifier: OwnedSigningKeyId, value: String, ) -> Option { self.0.entry(entity).or_insert_with(Default::default).insert(key_identifier, value) @@ -51,7 +51,7 @@ impl Signatures { } /// Map of server signatures for an event, grouped by server. -pub type ServerSignatures = Signatures, Box>; +pub type ServerSignatures = Signatures; /// Map of device signatures for an event, grouped by user. -pub type DeviceSignatures = Signatures>; +pub type DeviceSignatures = Signatures; diff --git a/crates/ruma-common/src/identifiers/transaction_id.rs b/crates/ruma-common/src/identifiers/transaction_id.rs index 763f3e20..b3a71fa2 100644 --- a/crates/ruma-common/src/identifiers/transaction_id.rs +++ b/crates/ruma-common/src/identifiers/transaction_id.rs @@ -18,8 +18,9 @@ impl TransactionId { /// This will currently be a UUID without hyphens, but no guarantees are made about the /// structure of transaction IDs generated from this function. #[cfg(feature = "rand")] - pub fn new() -> Box { + #[allow(clippy::new_ret_no_self)] + pub fn new() -> OwnedTransactionId { let id = uuid::Uuid::new_v4(); - Self::from_box(id.to_simple().to_string().into_boxed_str()) + Self::from_borrowed(&id.to_simple().to_string()).to_owned() } } diff --git a/crates/ruma-common/src/identifiers/user_id.rs b/crates/ruma-common/src/identifiers/user_id.rs index 8cc82fea..abafbd98 100644 --- a/crates/ruma-common/src/identifiers/user_id.rs +++ b/crates/ruma-common/src/identifiers/user_id.rs @@ -25,10 +25,14 @@ impl UserId { /// Attempts to generate a `UserId` for the given origin server with a localpart consisting of /// 12 random ASCII characters. #[cfg(feature = "rand")] - pub fn new(server_name: &ServerName) -> Box { - Self::from_box( - format!("@{}:{}", super::generate_localpart(12).to_lowercase(), server_name).into(), - ) + #[allow(clippy::new_ret_no_self)] + pub fn new(server_name: &ServerName) -> OwnedUserId { + Self::from_borrowed(&format!( + "@{}:{}", + super::generate_localpart(12).to_lowercase(), + server_name + )) + .to_owned() } /// Attempts to complete a user ID, by adding the colon + server name and `@` prefix, if not @@ -41,14 +45,14 @@ impl UserId { pub fn parse_with_server_name( id: impl AsRef + Into>, server_name: &ServerName, - ) -> Result, IdParseError> { + ) -> Result { let id_str = id.as_ref(); if id_str.starts_with('@') { - Self::parse(id) + Self::parse(id).map(Into::into) } else { let _ = localpart_is_fully_conforming(id_str)?; - Ok(Self::from_box(format!("@{}:{}", id_str, server_name).into())) + Ok(Self::from_borrowed(&format!("@{}:{}", id_str, server_name)).to_owned()) } } @@ -153,7 +157,7 @@ use ruma_macros::IdZst; mod tests { use std::convert::TryFrom; - use super::UserId; + use super::{OwnedUserId, UserId}; use crate::{server_name, IdParseError}; #[test] @@ -272,7 +276,7 @@ mod tests { #[test] fn deserialize_valid_user_id() { assert_eq!( - serde_json::from_str::>(r#""@carl:example.com""#) + serde_json::from_str::(r#""@carl:example.com""#) .expect("Failed to convert JSON to UserId"), <&UserId>::try_from("@carl:example.com").expect("Failed to create UserId.") ); diff --git a/crates/ruma-common/tests/api/manual_endpoint_impl.rs b/crates/ruma-common/tests/api/manual_endpoint_impl.rs index 0bead785..888c9a84 100644 --- a/crates/ruma-common/tests/api/manual_endpoint_impl.rs +++ b/crates/ruma-common/tests/api/manual_endpoint_impl.rs @@ -12,15 +12,15 @@ use ruma_common::{ AuthScheme, EndpointError, IncomingRequest, IncomingResponse, MatrixVersion, Metadata, OutgoingRequest, OutgoingResponse, SendAccessToken, }, - RoomAliasId, RoomId, + OwnedRoomAliasId, OwnedRoomId, }; use serde::{Deserialize, Serialize}; /// A request to create a new room alias. #[derive(Debug)] pub struct Request { - pub room_id: Box, // body - pub room_alias: Box, // path + pub room_id: OwnedRoomId, // body + pub room_alias: OwnedRoomAliasId, // path } const METADATA: Metadata = Metadata { @@ -104,7 +104,7 @@ impl IncomingRequest for Request { #[derive(Debug, Serialize, Deserialize)] struct RequestBody { - room_id: Box, + room_id: OwnedRoomId, } /// The response to a request to create a new room alias. diff --git a/crates/ruma-common/tests/api/ruma_api_macros.rs b/crates/ruma-common/tests/api/ruma_api_macros.rs index 65df0a53..ea0516ac 100644 --- a/crates/ruma-common/tests/api/ruma_api_macros.rs +++ b/crates/ruma-common/tests/api/ruma_api_macros.rs @@ -5,7 +5,7 @@ pub mod some_endpoint { api::ruma_api, events::{tag::TagEvent, AnyRoomEvent}, serde::Raw, - UserId, + OwnedUserId, }; ruma_api! { @@ -33,7 +33,7 @@ pub mod some_endpoint { // This value will be inserted into the request's URL in place of the // ":user" path component. #[ruma_api(path)] - pub user: Box, + pub user: OwnedUserId, } response: { diff --git a/crates/ruma-common/tests/api/ui/03-move-value.rs b/crates/ruma-common/tests/api/ui/03-move-value.rs index 614e8ba6..ca7bf40c 100644 --- a/crates/ruma-common/tests/api/ui/03-move-value.rs +++ b/crates/ruma-common/tests/api/ui/03-move-value.rs @@ -2,7 +2,7 @@ // consume the request/response. mod newtype_body { - use ruma_common::{api::ruma_api, UserId}; + use ruma_common::{api::ruma_api, OwnedUserId}; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] pub struct Foo; @@ -25,7 +25,7 @@ mod newtype_body { pub bar: String, #[ruma_api(query)] - pub baz: Box, + pub baz: OwnedUserId, #[ruma_api(header = CONTENT_TYPE)] pub world: String, @@ -42,7 +42,7 @@ mod newtype_body { } mod raw_body { - use ruma_common::{api::ruma_api, UserId}; + use ruma_common::{api::ruma_api, OwnedUserId}; ruma_api! { metadata: { @@ -62,7 +62,7 @@ mod raw_body { pub bar: String, #[ruma_api(query)] - pub baz: Box, + pub baz: OwnedUserId, #[ruma_api(header = CONTENT_TYPE)] pub world: String, @@ -79,7 +79,7 @@ mod raw_body { } mod plain { - use ruma_common::{api::ruma_api, UserId}; + use ruma_common::{api::ruma_api, OwnedUserId}; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] pub struct Foo; @@ -101,7 +101,7 @@ mod plain { pub bar: String, #[ruma_api(query)] - pub baz: Box, + pub baz: OwnedUserId, #[ruma_api(header = CONTENT_TYPE)] pub world: String, diff --git a/crates/ruma-federation-api/src/discovery.rs b/crates/ruma-federation-api/src/discovery.rs index 3877a2de..9ebb2368 100644 --- a/crates/ruma-federation-api/src/discovery.rs +++ b/crates/ruma-federation-api/src/discovery.rs @@ -2,7 +2,9 @@ use std::collections::BTreeMap; -use ruma_common::{serde::Base64, MilliSecondsSinceUnixEpoch, OwnedServerName, ServerSigningKeyId}; +use ruma_common::{ + serde::Base64, MilliSecondsSinceUnixEpoch, OwnedServerName, OwnedServerSigningKeyId, +}; use serde::{Deserialize, Serialize}; pub mod discover_homeserver; @@ -55,15 +57,15 @@ pub struct ServerSigningKeys { pub server_name: OwnedServerName, /// Public keys of the homeserver for verifying digital signatures. - pub verify_keys: BTreeMap, VerifyKey>, + pub verify_keys: BTreeMap, /// Public keys that the homeserver used to use and when it stopped using them. - pub old_verify_keys: BTreeMap, OldVerifyKey>, + pub old_verify_keys: BTreeMap, /// Digital signatures of this object signed using the verify_keys. /// /// Map of server name to keys by key ID. - pub signatures: BTreeMap, String>>, + pub signatures: BTreeMap>, /// Timestamp when the keys should be refreshed. /// diff --git a/crates/ruma-federation-api/src/discovery/get_remote_server_keys_batch.rs b/crates/ruma-federation-api/src/discovery/get_remote_server_keys_batch.rs index fdca6aa7..17a72f4a 100644 --- a/crates/ruma-federation-api/src/discovery/get_remote_server_keys_batch.rs +++ b/crates/ruma-federation-api/src/discovery/get_remote_server_keys_batch.rs @@ -11,7 +11,8 @@ pub mod v2 { use std::collections::BTreeMap; use ruma_common::{ - api::ruma_api, serde::Raw, MilliSecondsSinceUnixEpoch, OwnedServerName, ServerSigningKeyId, + api::ruma_api, serde::Raw, MilliSecondsSinceUnixEpoch, OwnedServerName, + OwnedServerSigningKeyId, }; use serde::{Deserialize, Serialize}; @@ -37,7 +38,7 @@ pub mod v2 { /// notary server must return an empty server_keys array in the response. /// /// The notary server may return multiple keys regardless of the Key IDs given. - pub server_keys: BTreeMap, QueryCriteria>>, + pub server_keys: BTreeMap>, } @@ -52,7 +53,7 @@ pub mod v2 { pub fn new( server_keys: BTreeMap< OwnedServerName, - BTreeMap, QueryCriteria>, + BTreeMap, >, ) -> Self { Self { server_keys } diff --git a/crates/ruma-federation-api/src/keys/claim_keys.rs b/crates/ruma-federation-api/src/keys/claim_keys.rs index 2b425ca9..ffba66b7 100644 --- a/crates/ruma-federation-api/src/keys/claim_keys.rs +++ b/crates/ruma-federation-api/src/keys/claim_keys.rs @@ -13,7 +13,7 @@ pub mod v1 { api::ruma_api, encryption::OneTimeKey, serde::{Base64, Raw}, - DeviceKeyAlgorithm, DeviceKeyId, OwnedDeviceId, OwnedUserId, + DeviceKeyAlgorithm, OwnedDeviceId, OwnedDeviceKeyId, OwnedUserId, }; use serde::{Deserialize, Serialize}; @@ -58,7 +58,7 @@ pub mod v1 { /// One time keys for use in pre-key messages pub type OneTimeKeys = - BTreeMap, Raw>>>; + BTreeMap>>>; /// A key and its signature #[derive(Debug, Clone, Serialize, Deserialize)] @@ -68,14 +68,14 @@ pub mod v1 { pub key: Base64, /// Signature of the key object. - pub signatures: BTreeMap, String>>, + pub signatures: BTreeMap>, } impl KeyObject { /// Creates a new `KeyObject` with the given key and signatures. pub fn new( key: Base64, - signatures: BTreeMap, String>>, + signatures: BTreeMap>, ) -> Self { Self { key, signatures } } diff --git a/crates/ruma-federation-api/src/thirdparty/bind_callback.rs b/crates/ruma-federation-api/src/thirdparty/bind_callback.rs index f5c9c7f7..f700f0b7 100644 --- a/crates/ruma-federation-api/src/thirdparty/bind_callback.rs +++ b/crates/ruma-federation-api/src/thirdparty/bind_callback.rs @@ -12,8 +12,8 @@ pub mod v1 { use std::collections::BTreeMap; use ruma_common::{ - api::ruma_api, thirdparty::Medium, OwnedRoomId, OwnedServerName, OwnedUserId, - ServerSigningKeyId, UserId, + api::ruma_api, thirdparty::Medium, OwnedRoomId, OwnedServerName, OwnedServerSigningKeyId, + OwnedUserId, UserId, }; use serde::{Deserialize, Serialize}; @@ -88,7 +88,7 @@ pub mod v1 { pub sender: OwnedUserId, /// Signature from the identity server using a long-term private key. - pub signed: BTreeMap, String>>, + pub signed: BTreeMap>, } impl ThirdPartyInvite { @@ -98,7 +98,7 @@ pub mod v1 { mxid: OwnedUserId, room_id: OwnedRoomId, sender: OwnedUserId, - signed: BTreeMap, String>>, + signed: BTreeMap>, ) -> Self { Self { medium: Medium::Email, address, mxid, room_id, sender, signed } }