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
* Move `CanonicalJson`, `CanonicalJsonObject` and `CanonicalJsonError` out of
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

View File

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

View File

@ -2,7 +2,7 @@
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].
///
@ -37,8 +37,8 @@ impl RoomAliasId {
}
/// 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 {
MatrixToUri::new((self, ev_id).into(), Vec::new())
pub fn matrix_to_event_uri(&self, ev_id: impl Into<OwnedEventId>) -> MatrixToUri {
MatrixToUri::new((self.to_owned(), ev_id.into()).into(), Vec::new())
}
/// 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.
pub fn matrix_event_uri(&self, ev_id: &EventId) -> MatrixUri {
MatrixUri::new((self, ev_id).into(), Vec::new(), None)
pub fn matrix_event_uri(&self, ev_id: impl Into<OwnedEventId>) -> MatrixUri {
MatrixUri::new((self.to_owned(), ev_id.into()).into(), Vec::new(), None)
}
fn colon_idx(&self) -> usize {

View File

@ -2,7 +2,9 @@
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].
///
@ -56,13 +58,17 @@ impl RoomId {
/// "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 {
MatrixToUri::new(self.into(), via.into_iter().collect())
pub fn matrix_to_uri<T>(&self, via: T) -> MatrixToUri
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.
pub fn matrix_to_event_uri(&self, ev_id: &EventId) -> MatrixToUri {
MatrixToUri::new((self, ev_id).into(), Vec::new())
pub fn matrix_to_event_uri(&self, ev_id: impl Into<OwnedEventId>) -> MatrixToUri {
MatrixToUri::new((self.to_owned(), ev_id.into()).into(), Vec::new())
}
/// 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"
/// );
/// ```
pub fn matrix_uri<'a>(
&self,
via: impl IntoIterator<Item = &'a ServerName>,
join: bool,
) -> MatrixUri {
pub fn matrix_uri<T>(&self, via: T, join: bool) -> MatrixUri
where
T: IntoIterator,
T::Item: Into<OwnedServerName>,
{
MatrixUri::new(
self.into(),
via.into_iter().collect(),
via.into_iter().map(Into::into).collect(),
Some(UriAction::Join).filter(|_| join),
)
}
/// Create a `matrix:` URI for an event scoped under this room ID.
pub fn matrix_event_uri<'a>(
&self,
ev_id: &EventId,
via: impl IntoIterator<Item = &'a ServerName>,
) -> MatrixUri {
MatrixUri::new((self, ev_id).into(), via.into_iter().collect(), None)
pub fn matrix_event_uri<T>(&self, ev_id: impl Into<OwnedEventId>, via: T) -> MatrixUri
where
T: IntoIterator,
T::Item: Into<OwnedServerName>,
{
MatrixUri::new(
(self.to_owned(), ev_id.into()).into(),
via.into_iter().map(Into::into).collect(),
None,
)
}
fn colon_idx(&self) -> usize {