From 96ca63847416b2de1b752eae9a5520122b82c8f2 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 30 May 2020 18:31:08 +0200 Subject: [PATCH] Change most usages of String to Box --- src/event_id.rs | 8 ++++---- src/macros.rs | 17 +++++++---------- src/room_alias_id.rs | 4 ++-- src/room_id.rs | 6 +++--- src/room_id_or_room_alias_id.rs | 4 ++-- src/room_version_id.rs | 10 ++++++---- src/user_id.rs | 8 ++++---- 7 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/event_id.rs b/src/event_id.rs index e0af1492..d4b9811e 100644 --- a/src/event_id.rs +++ b/src/event_id.rs @@ -44,7 +44,7 @@ use crate::{error::Error, parse_id, validate_id}; #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))] #[cfg_attr(feature = "diesel", sql_type = "Text")] pub struct EventId { - full_id: String, + full_id: Box, colon_idx: Option, } @@ -63,7 +63,7 @@ impl EventId { if !is_valid_server_name(server_name) { return Err(Error::InvalidServerName); } - let full_id = format!("${}:{}", generate_localpart(18), server_name); + let full_id = format!("${}:{}", generate_localpart(18), server_name).into(); Ok(Self { full_id, @@ -105,14 +105,14 @@ impl TryFrom> for EventId { let colon_idx = parse_id(&event_id, &['$'])?; Ok(Self { - full_id: event_id.into_owned(), + full_id: event_id.into_owned().into(), colon_idx: Some(colon_idx), }) } else { validate_id(&event_id, &['$'])?; Ok(Self { - full_id: event_id.into_owned(), + full_id: event_id.into_owned().into(), colon_idx: None, }) } diff --git a/src/macros.rs b/src/macros.rs index 881f5292..09cb7e8a 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -2,7 +2,7 @@ macro_rules! common_impls { ($id:ident, $desc:literal) => { impl ::std::convert::From<$id> for ::std::string::String { fn from(id: $id) -> Self { - id.full_id + id.full_id.into() } } @@ -44,16 +44,13 @@ macro_rules! common_impls { impl ::std::cmp::PartialOrd for $id { fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> { - <::std::string::String as ::std::cmp::PartialOrd>::partial_cmp( - &self.full_id, - &other.full_id, - ) + ::std::cmp::PartialOrd::partial_cmp(&self.full_id, &other.full_id) } } impl ::std::cmp::Ord for $id { fn cmp(&self, other: &Self) -> ::std::cmp::Ordering { - <::std::string::String as ::std::cmp::Ord>::cmp(&self.full_id, &other.full_id) + ::std::cmp::Ord::cmp(&self.full_id, &other.full_id) } } @@ -85,25 +82,25 @@ macro_rules! common_impls { impl ::std::cmp::PartialEq<&str> for $id { fn eq(&self, other: &&str) -> bool { - self.full_id == *other + &self.full_id[..] == *other } } impl ::std::cmp::PartialEq<$id> for &str { fn eq(&self, other: &$id) -> bool { - *self == other.full_id + *self == &other.full_id[..] } } impl ::std::cmp::PartialEq<::std::string::String> for $id { fn eq(&self, other: &::std::string::String) -> bool { - &self.full_id == other + &self.full_id[..] == &other[..] } } impl ::std::cmp::PartialEq<$id> for ::std::string::String { fn eq(&self, other: &$id) -> bool { - self == &other.full_id + &self[..] == &other.full_id[..] } } }; diff --git a/src/room_alias_id.rs b/src/room_alias_id.rs index 0293bfb5..dacce28e 100644 --- a/src/room_alias_id.rs +++ b/src/room_alias_id.rs @@ -24,7 +24,7 @@ use crate::{error::Error, parse_id}; #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))] #[cfg_attr(feature = "diesel", sql_type = "Text")] pub struct RoomAliasId { - pub(crate) full_id: String, + pub(crate) full_id: Box, pub(crate) colon_idx: NonZeroU8, } @@ -50,7 +50,7 @@ impl TryFrom> for RoomAliasId { let colon_idx = parse_id(&room_id, &['#'])?; Ok(Self { - full_id: room_id.into_owned(), + full_id: room_id.into_owned().into(), colon_idx, }) } diff --git a/src/room_id.rs b/src/room_id.rs index fa5e8841..c349ee84 100644 --- a/src/room_id.rs +++ b/src/room_id.rs @@ -24,7 +24,7 @@ use crate::{error::Error, parse_id}; #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))] #[cfg_attr(feature = "diesel", sql_type = "Text")] pub struct RoomId { - pub(crate) full_id: String, + pub(crate) full_id: Box, pub(crate) colon_idx: NonZeroU8, } @@ -41,7 +41,7 @@ impl RoomId { if !is_valid_server_name(server_name) { return Err(Error::InvalidServerName); } - let full_id = format!("!{}:{}", generate_localpart(18), server_name); + let full_id = format!("!{}:{}", generate_localpart(18), server_name).into(); Ok(Self { full_id, @@ -71,7 +71,7 @@ impl TryFrom> for RoomId { let colon_idx = parse_id(&room_id, &['!'])?; Ok(Self { - full_id: room_id.into_owned(), + full_id: room_id.into_owned().into(), colon_idx, }) } diff --git a/src/room_id_or_room_alias_id.rs b/src/room_id_or_room_alias_id.rs index f0be719e..7b356d4a 100644 --- a/src/room_id_or_room_alias_id.rs +++ b/src/room_id_or_room_alias_id.rs @@ -30,7 +30,7 @@ use crate::{error::Error, parse_id, RoomAliasId, RoomId}; #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))] #[cfg_attr(feature = "diesel", sql_type = "Text")] pub struct RoomIdOrAliasId { - full_id: String, + full_id: Box, colon_idx: NonZeroU8, } @@ -97,7 +97,7 @@ impl TryFrom> for RoomIdOrAliasId { fn try_from(room_id_or_alias_id: Cow<'_, str>) -> Result { let colon_idx = parse_id(&room_id_or_alias_id, &['#', '!'])?; Ok(Self { - full_id: room_id_or_alias_id.into_owned(), + full_id: room_id_or_alias_id.into_owned().into(), colon_idx, }) } diff --git a/src/room_version_id.rs b/src/room_version_id.rs index 02cd7bf4..216b59f0 100644 --- a/src/room_version_id.rs +++ b/src/room_version_id.rs @@ -51,7 +51,7 @@ enum InnerRoomVersionId { Version5, /// A custom room version. - Custom(String), + Custom(Box), } impl RoomVersionId { @@ -82,7 +82,7 @@ impl RoomVersionId { /// Creates a custom room version ID from the given string slice. pub fn custom(id: String) -> Self { - Self(InnerRoomVersionId::Custom(id)) + Self(InnerRoomVersionId::Custom(id.into())) } /// Whether or not this room version is an official one specified by the Matrix protocol. @@ -132,7 +132,7 @@ impl From for String { InnerRoomVersionId::Version3 => "3".to_owned(), InnerRoomVersionId::Version4 => "4".to_owned(), InnerRoomVersionId::Version5 => "5".to_owned(), - InnerRoomVersionId::Custom(version) => version, + InnerRoomVersionId::Custom(version) => version.into(), } } } @@ -193,7 +193,9 @@ impl TryFrom> for RoomVersionId { } else if custom.chars().count() > MAX_CODE_POINTS { return Err(Error::MaximumLengthExceeded); } else { - Self(InnerRoomVersionId::Custom(room_version_id.into_owned())) + Self(InnerRoomVersionId::Custom( + room_version_id.into_owned().into(), + )) } } }; diff --git a/src/user_id.rs b/src/user_id.rs index f020dd46..43e499bd 100644 --- a/src/user_id.rs +++ b/src/user_id.rs @@ -24,7 +24,7 @@ use crate::{error::Error, is_valid_server_name, parse_id}; #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))] #[cfg_attr(feature = "diesel", sql_type = "Text")] pub struct UserId { - full_id: String, + full_id: Box, colon_idx: NonZeroU8, /// Whether this user id is a historical one. /// @@ -47,7 +47,7 @@ impl UserId { if !is_valid_server_name(server_name) { return Err(Error::InvalidServerName); } - let full_id = format!("@{}:{}", generate_localpart(12).to_lowercase(), server_name); + let full_id = format!("@{}:{}", generate_localpart(12).to_lowercase(), server_name).into(); Ok(Self { full_id, @@ -78,7 +78,7 @@ impl UserId { } Ok(Self { - full_id: format!("@{}:{}", id_str, server_name), + full_id: format!("@{}:{}", id_str, server_name).into(), colon_idx: NonZeroU8::new(id_str.len() as u8 + 1).unwrap(), is_historical: !is_fully_conforming, }) @@ -117,7 +117,7 @@ impl TryFrom> for UserId { let is_historical = localpart_is_fully_comforming(localpart)?; Ok(Self { - full_id: user_id.into_owned(), + full_id: user_id.into_owned().into(), colon_idx, is_historical: !is_historical, })