Change most usages of String to Box<str>

This commit is contained in:
Jonas Platte 2020-05-30 18:31:08 +02:00
parent 89f0594a68
commit 96ca638474
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
7 changed files with 28 additions and 29 deletions

View File

@ -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<str>,
colon_idx: Option<NonZeroU8>,
}
@ -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<Cow<'_, str>> 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,
})
}

View File

@ -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[..]
}
}
};

View File

@ -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<str>,
pub(crate) colon_idx: NonZeroU8,
}
@ -50,7 +50,7 @@ impl TryFrom<Cow<'_, str>> 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,
})
}

View File

@ -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<str>,
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<Cow<'_, str>> 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,
})
}

View File

@ -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<str>,
colon_idx: NonZeroU8,
}
@ -97,7 +97,7 @@ impl TryFrom<Cow<'_, str>> for RoomIdOrAliasId {
fn try_from(room_id_or_alias_id: Cow<'_, str>) -> Result<Self, Error> {
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,
})
}

View File

@ -51,7 +51,7 @@ enum InnerRoomVersionId {
Version5,
/// A custom room version.
Custom(String),
Custom(Box<str>),
}
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<RoomVersionId> 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<Cow<'_, str>> 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(),
))
}
}
};

View File

@ -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<str>,
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<Cow<'_, str>> 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,
})