identifiers: Remove runtime checks for server_name extraction

This commit is contained in:
Jonas Platte 2021-11-26 17:43:02 +01:00
parent c3382f085c
commit 4132c43cbb
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
7 changed files with 8 additions and 16 deletions

View File

@ -1,7 +1,5 @@
//! Matrix event identifiers. //! Matrix event identifiers.
use std::convert::TryInto;
use crate::ServerName; use crate::ServerName;
/// A Matrix event ID. /// A Matrix event ID.
@ -68,7 +66,7 @@ impl EventId {
/// ///
/// Only applicable to events in the original format as used by Matrix room versions 1 and 2. /// Only applicable to events in the original format as used by Matrix room versions 1 and 2.
pub fn server_name(&self) -> Option<&ServerName> { pub fn server_name(&self) -> Option<&ServerName> {
self.colon_idx().map(|idx| self.as_str()[idx as usize + 1..].try_into().unwrap()) self.colon_idx().map(|idx| ServerName::from_borrowed(&self.as_str()[idx + 1..]))
} }
fn colon_idx(&self) -> Option<usize> { fn colon_idx(&self) -> Option<usize> {

View File

@ -27,7 +27,7 @@ macro_rules! partial_eq_string {
macro_rules! opaque_identifier_common_impls { macro_rules! opaque_identifier_common_impls {
($id:ty) => { ($id:ty) => {
impl $id { impl $id {
fn from_borrowed(s: &str) -> &Self { pub(super) fn from_borrowed(s: &str) -> &Self {
unsafe { std::mem::transmute(s) } unsafe { std::mem::transmute(s) }
} }

View File

@ -2,7 +2,7 @@
//! //!
//! [MXC URI]: https://matrix.org/docs/spec/client_server/r0.6.1#mxc-uri //! [MXC URI]: https://matrix.org/docs/spec/client_server/r0.6.1#mxc-uri
use std::{convert::TryInto, num::NonZeroU8}; use std::num::NonZeroU8;
use ruma_identifiers_validation::{error::MxcUriError, mxc_uri::validate}; use ruma_identifiers_validation::{error::MxcUriError, mxc_uri::validate};
@ -36,7 +36,7 @@ impl MxcUri {
pub fn parts(&self) -> Result<(&ServerName, &str)> { pub fn parts(&self) -> Result<(&ServerName, &str)> {
self.extract_slash_idx().map(|idx| { self.extract_slash_idx().map(|idx| {
( (
self.as_str()[6..idx.get() as usize].try_into().unwrap(), ServerName::from_borrowed(&self.as_str()[6..idx.get() as usize]),
&self.as_str()[idx.get() as usize + 1..], &self.as_str()[idx.get() as usize + 1..],
) )
}) })

View File

@ -1,7 +1,5 @@
//! Matrix room alias identifiers. //! Matrix room alias identifiers.
use std::convert::TryInto;
use crate::{server_name::ServerName, EventId, MatrixToRef}; use crate::{server_name::ServerName, EventId, MatrixToRef};
/// A Matrix room alias ID. /// A Matrix room alias ID.
@ -31,7 +29,7 @@ impl RoomAliasId {
/// Returns the server name of the room alias ID. /// Returns the server name of the room alias ID.
pub fn server_name(&self) -> &ServerName { pub fn server_name(&self) -> &ServerName {
self.as_str()[self.colon_idx() + 1..].try_into().unwrap() ServerName::from_borrowed(&self.as_str()[self.colon_idx() + 1..])
} }
/// Create a `matrix.to` reference for this room alias ID. /// Create a `matrix.to` reference for this room alias ID.

View File

@ -1,7 +1,5 @@
//! Matrix room identifiers. //! Matrix room identifiers.
use std::convert::TryInto;
use crate::{EventId, MatrixToRef, ServerName}; use crate::{EventId, MatrixToRef, ServerName};
/// A Matrix room ID. /// A Matrix room ID.
@ -40,7 +38,7 @@ impl RoomId {
/// Returns the server name of the room ID. /// Returns the server name of the room ID.
pub fn server_name(&self) -> &ServerName { pub fn server_name(&self) -> &ServerName {
self.as_str()[self.colon_idx() + 1..].try_into().unwrap() ServerName::from_borrowed(&self.as_str()[self.colon_idx() + 1..])
} }
/// Create a `matrix.to` reference for this room ID. /// Create a `matrix.to` reference for this room ID.

View File

@ -43,7 +43,7 @@ impl RoomOrAliasId {
/// Returns the server name of the room (alias) ID. /// Returns the server name of the room (alias) ID.
pub fn server_name(&self) -> &ServerName { pub fn server_name(&self) -> &ServerName {
self.as_str()[self.colon_idx() + 1..].try_into().unwrap() ServerName::from_borrowed(&self.as_str()[self.colon_idx() + 1..])
} }
/// Whether this is a room id (starts with `'!'`) /// Whether this is a room id (starts with `'!'`)

View File

@ -1,7 +1,5 @@
//! Matrix user identifiers. //! Matrix user identifiers.
use std::convert::TryInto;
use crate::{MatrixToRef, ServerName}; use crate::{MatrixToRef, ServerName};
/// A Matrix user ID. /// A Matrix user ID.
@ -60,7 +58,7 @@ impl UserId {
/// Returns the server name of the user ID. /// Returns the server name of the user ID.
pub fn server_name(&self) -> &ServerName { pub fn server_name(&self) -> &ServerName {
self.as_str()[self.colon_idx() as usize + 1..].try_into().unwrap() ServerName::from_borrowed(&self.as_str()[self.colon_idx() + 1..])
} }
/// Whether this user ID is a historical one. /// Whether this user ID is a historical one.