identifiers: Make matrix URI ctors generic over owned parameters

Avoid unnecessary cloning
This commit is contained in:
Kévin Commaille 2022-06-29 13:59:53 +02:00 committed by Kévin Commaille
parent cbcf9b0a7b
commit 4883a3154c
4 changed files with 40 additions and 29 deletions

View File

@ -22,6 +22,7 @@ Breaking changes:
* Remove `RoomMessageFeedbackEvent` and associated types and variants according to MSC3582 * Remove `RoomMessageFeedbackEvent` and associated types and variants according to MSC3582
* Move `CanonicalJson`, `CanonicalJsonObject` and `CanonicalJsonError` out of * Move `CanonicalJson`, `CanonicalJsonObject` and `CanonicalJsonError` out of
the `serde` module and behind the cargo feature flag `canonical-json` the `serde` module and behind the cargo feature flag `canonical-json`
* Make identifiers matrix URI constructors generic over owned parameters
[spec]: https://github.com/matrix-org/matrix-spec-proposals/pull/3669 [spec]: https://github.com/matrix-org/matrix-spec-proposals/pull/3669

View File

@ -11,7 +11,7 @@ use url::Url;
use super::{ use super::{
EventId, OwnedEventId, OwnedRoomAliasId, OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, EventId, OwnedEventId, OwnedRoomAliasId, OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName,
OwnedUserId, RoomAliasId, RoomId, RoomOrAliasId, ServerName, UserId, OwnedUserId, RoomAliasId, RoomId, RoomOrAliasId, UserId,
}; };
use crate::PrivOwnedStr; use crate::PrivOwnedStr;
@ -282,8 +282,8 @@ pub struct MatrixToUri {
} }
impl MatrixToUri { impl MatrixToUri {
pub(crate) fn new(id: MatrixId, via: Vec<&ServerName>) -> Self { pub(crate) fn new(id: MatrixId, via: Vec<OwnedServerName>) -> Self {
Self { id, via: via.into_iter().map(ToOwned::to_owned).collect() } Self { id, via }
} }
/// The identifier represented by this `matrix.to` URI. /// The identifier represented by this `matrix.to` URI.
@ -446,8 +446,8 @@ pub struct MatrixUri {
} }
impl MatrixUri { impl MatrixUri {
pub(crate) fn new(id: MatrixId, via: Vec<&ServerName>, action: Option<UriAction>) -> Self { pub(crate) fn new(id: MatrixId, via: Vec<OwnedServerName>, action: Option<UriAction>) -> Self {
Self { id, via: via.into_iter().map(ToOwned::to_owned).collect(), action } Self { id, via, action }
} }
/// The identifier represented by this `matrix:` URI. /// The identifier represented by this `matrix:` URI.
@ -544,7 +544,7 @@ mod tests {
use super::{MatrixId, MatrixToUri, MatrixUri}; use super::{MatrixId, MatrixToUri, MatrixUri};
use crate::{ use crate::{
event_id, matrix_uri::UriAction, room_alias_id, room_id, server_name, user_id, event_id, matrix_uri::UriAction, room_alias_id, room_id, server_name, user_id,
RoomOrAliasId, OwnedServerName, RoomOrAliasId,
}; };
#[test] #[test]
@ -818,7 +818,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
room_id!("!ruma:notareal.hs") room_id!("!ruma:notareal.hs")
.matrix_event_uri(event_id!("$event:notareal.hs"), vec![]) .matrix_event_uri(event_id!("$event:notareal.hs"), Vec::<OwnedServerName>::new())
.to_string(), .to_string(),
"matrix:roomid/ruma:notareal.hs/e/event:notareal.hs" "matrix:roomid/ruma:notareal.hs/e/event:notareal.hs"
); );

View File

@ -2,7 +2,7 @@
use ruma_macros::IdZst; use ruma_macros::IdZst;
use super::{matrix_uri::UriAction, server_name::ServerName, EventId, MatrixToUri, MatrixUri}; use super::{matrix_uri::UriAction, server_name::ServerName, MatrixToUri, MatrixUri, OwnedEventId};
/// A Matrix [room alias ID]. /// A Matrix [room alias ID].
/// ///
@ -37,8 +37,8 @@ impl RoomAliasId {
} }
/// Create a `matrix.to` URI for an event scoped under this room alias ID. /// Create a `matrix.to` URI for an event scoped under this room alias ID.
pub fn matrix_to_event_uri(&self, ev_id: &EventId) -> MatrixToUri { pub fn matrix_to_event_uri(&self, ev_id: impl Into<OwnedEventId>) -> MatrixToUri {
MatrixToUri::new((self, ev_id).into(), Vec::new()) MatrixToUri::new((self.to_owned(), ev_id.into()).into(), Vec::new())
} }
/// Create a `matrix:` URI for this room alias ID. /// Create a `matrix:` URI for this room alias ID.
@ -49,8 +49,8 @@ impl RoomAliasId {
} }
/// Create a `matrix:` URI for an event scoped under this room alias ID. /// Create a `matrix:` URI for an event scoped under this room alias ID.
pub fn matrix_event_uri(&self, ev_id: &EventId) -> MatrixUri { pub fn matrix_event_uri(&self, ev_id: impl Into<OwnedEventId>) -> MatrixUri {
MatrixUri::new((self, ev_id).into(), Vec::new(), None) MatrixUri::new((self.to_owned(), ev_id.into()).into(), Vec::new(), None)
} }
fn colon_idx(&self) -> usize { fn colon_idx(&self) -> usize {

View File

@ -2,7 +2,9 @@
use ruma_macros::IdZst; use ruma_macros::IdZst;
use super::{matrix_uri::UriAction, EventId, MatrixToUri, MatrixUri, ServerName}; use super::{
matrix_uri::UriAction, MatrixToUri, MatrixUri, OwnedEventId, OwnedServerName, ServerName,
};
/// A Matrix [room ID]. /// A Matrix [room ID].
/// ///
@ -56,13 +58,17 @@ impl RoomId {
/// "https://matrix.to/#/%21somewhere%3Aexample.org?via=example.org&via=alt.example.org" /// "https://matrix.to/#/%21somewhere%3Aexample.org?via=example.org&via=alt.example.org"
/// ); /// );
/// ``` /// ```
pub fn matrix_to_uri<'a>(&self, via: impl IntoIterator<Item = &'a ServerName>) -> MatrixToUri { pub fn matrix_to_uri<T>(&self, via: T) -> MatrixToUri
MatrixToUri::new(self.into(), via.into_iter().collect()) where
T: IntoIterator,
T::Item: Into<OwnedServerName>,
{
MatrixToUri::new(self.into(), via.into_iter().map(Into::into).collect())
} }
/// Create a `matrix.to` URI for an event scoped under this room ID. /// Create a `matrix.to` URI for an event scoped under this room ID.
pub fn matrix_to_event_uri(&self, ev_id: &EventId) -> MatrixToUri { pub fn matrix_to_event_uri(&self, ev_id: impl Into<OwnedEventId>) -> MatrixToUri {
MatrixToUri::new((self, ev_id).into(), Vec::new()) MatrixToUri::new((self.to_owned(), ev_id.into()).into(), Vec::new())
} }
/// Create a `matrix:` URI for this room ID. /// Create a `matrix:` URI for this room ID.
@ -81,25 +87,29 @@ impl RoomId {
/// "matrix:roomid/somewhere:example.org?via=example.org&via=alt.example.org&action=join" /// "matrix:roomid/somewhere:example.org?via=example.org&via=alt.example.org&action=join"
/// ); /// );
/// ``` /// ```
pub fn matrix_uri<'a>( pub fn matrix_uri<T>(&self, via: T, join: bool) -> MatrixUri
&self, where
via: impl IntoIterator<Item = &'a ServerName>, T: IntoIterator,
join: bool, T::Item: Into<OwnedServerName>,
) -> MatrixUri { {
MatrixUri::new( MatrixUri::new(
self.into(), self.into(),
via.into_iter().collect(), via.into_iter().map(Into::into).collect(),
Some(UriAction::Join).filter(|_| join), Some(UriAction::Join).filter(|_| join),
) )
} }
/// Create a `matrix:` URI for an event scoped under this room ID. /// Create a `matrix:` URI for an event scoped under this room ID.
pub fn matrix_event_uri<'a>( pub fn matrix_event_uri<T>(&self, ev_id: impl Into<OwnedEventId>, via: T) -> MatrixUri
&self, where
ev_id: &EventId, T: IntoIterator,
via: impl IntoIterator<Item = &'a ServerName>, T::Item: Into<OwnedServerName>,
) -> MatrixUri { {
MatrixUri::new((self, ev_id).into(), via.into_iter().collect(), None) MatrixUri::new(
(self.to_owned(), ev_id.into()).into(),
via.into_iter().map(Into::into).collect(),
None,
)
} }
fn colon_idx(&self) -> usize { fn colon_idx(&self) -> usize {