diff --git a/src/core/matrix/pdu/id.rs b/src/core/matrix/pdu/id.rs index 896d677b..d024ad07 100644 --- a/src/core/matrix/pdu/id.rs +++ b/src/core/matrix/pdu/id.rs @@ -1,5 +1,4 @@ use super::{Count, RawId}; -use crate::utils::u64_from_u8x8; pub type ShortRoomId = ShortId; pub type ShortEventId = ShortId; @@ -16,8 +15,8 @@ impl From for Id { #[inline] fn from(raw: RawId) -> Self { Self { - shortroomid: u64_from_u8x8(raw.shortroomid()), - shorteventid: Count::from_unsigned(u64_from_u8x8(raw.shorteventid())), + shortroomid: u64::from_be_bytes(raw.shortroomid()), + shorteventid: Count::from_unsigned(u64::from_be_bytes(raw.shorteventid())), } } } diff --git a/src/core/utils/bytes.rs b/src/core/utils/bytes.rs index 93afa734..79723b76 100644 --- a/src/core/utils/bytes.rs +++ b/src/core/utils/bytes.rs @@ -31,7 +31,7 @@ pub fn pretty(bytes: usize) -> String { #[inline] #[must_use] pub fn increment(old: Option<&[u8]>) -> [u8; 8] { - old.map_or(0_u64, u64_from_bytes_or_zero) + old.map_or(0_u64, |bytes| u64_from_bytes(bytes).unwrap_or(0)) .wrapping_add(1) .to_be_bytes() } @@ -45,16 +45,4 @@ pub fn u64_from_u8(bytes: &[u8]) -> u64 { /// Parses the big-endian bytes into an u64. #[inline] -#[must_use] -pub fn u64_from_bytes_or_zero(bytes: &[u8]) -> u64 { u64_from_bytes(bytes).unwrap_or(0) } - -/// Parses the big-endian bytes into an u64. -#[inline] -pub fn u64_from_bytes(bytes: &[u8]) -> Result { Ok(u64_from_u8x8(*u8x8_from_bytes(bytes)?)) } - -#[inline] -#[must_use] -pub fn u64_from_u8x8(bytes: [u8; 8]) -> u64 { u64::from_be_bytes(bytes) } - -#[inline] -pub fn u8x8_from_bytes(bytes: &[u8]) -> Result<&[u8; 8]> { Ok(bytes.try_into()?) } +pub fn u64_from_bytes(bytes: &[u8]) -> Result { Ok(u64::from_be_bytes(bytes.try_into()?)) } diff --git a/src/core/utils/html.rs b/src/core/utils/html.rs deleted file mode 100644 index eac4c47f..00000000 --- a/src/core/utils/html.rs +++ /dev/null @@ -1,40 +0,0 @@ -use std::fmt; - -/// Wrapper struct which will emit the HTML-escaped version of the contained -/// string when passed to a format string. -pub struct Escape<'a>(pub &'a str); - -/// Copied from librustdoc: -/// * -#[allow(clippy::string_slice)] -impl fmt::Display for Escape<'_> { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - // Because the internet is always right, turns out there's not that many - // characters to escape: http://stackoverflow.com/questions/7381974 - let Escape(s) = *self; - let pile_o_bits = s; - let mut last = 0; - for (i, ch) in s.char_indices() { - let s = match ch { - | '>' => ">", - | '<' => "<", - | '&' => "&", - | '\'' => "'", - | '"' => """, - | _ => continue, - }; - - fmt.write_str(&pile_o_bits[last..i])?; - fmt.write_str(s)?; - - // NOTE: we only expect single byte characters here - which is fine as long as - // we only match single byte characters - last = i.saturating_add(1); - } - - if last < s.len() { - fmt.write_str(&pile_o_bits[last..])?; - } - Ok(()) - } -} diff --git a/src/core/utils/math.rs b/src/core/utils/math.rs index 9316731c..0b8f4633 100644 --- a/src/core/utils/math.rs +++ b/src/core/utils/math.rs @@ -1,7 +1,7 @@ mod expected; mod tried; -use std::{cmp, convert::TryFrom}; +use std::convert::TryFrom; pub use checked_ops::checked_ops; @@ -97,17 +97,11 @@ pub fn usize_from_u64_truncated(val: u64) -> usize { val as usize } #[inline] pub fn try_into, Src>(src: Src) -> Result { - Dst::try_from(src).map_err(try_into_err::) + Dst::try_from(src).map_err(|_| { + err!(Arithmetic( + "failed to convert from {} to {}", + type_name::(), + type_name::() + )) + }) } - -fn try_into_err, Src>(e: >::Error) -> Error { - drop(e); - err!(Arithmetic( - "failed to convert from {} to {}", - type_name::(), - type_name::() - )) -} - -#[inline] -pub fn clamp(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) } diff --git a/src/core/utils/mod.rs b/src/core/utils/mod.rs index f9724534..157d573b 100644 --- a/src/core/utils/mod.rs +++ b/src/core/utils/mod.rs @@ -6,7 +6,6 @@ pub mod debug; pub mod defer; pub mod future; pub mod hash; -pub mod html; pub mod json; pub mod math; pub mod mutex_map; @@ -27,13 +26,11 @@ pub use ::tuwunel_macros::implement; pub use self::{ arrayvec::ArrayVecExt, bool::BoolExt, - bytes::{increment, u64_from_bytes, u64_from_u8, u64_from_u8x8}, + bytes::{increment, u64_from_bytes, u64_from_u8}, debug::slice_truncated as debug_slice_truncated, future::{BoolExt as FutureBoolExt, OptionStream, TryExtExt as TryFutureExtExt}, hash::sha256::delimited as calculate_hash, - html::Escape as HtmlEscape, json::{deserialize_from_str, to_canonical_object}, - math::clamp, mutex_map::{Guard as MutexMapGuard, MutexMap}, rand::{shuffle, string as random_string}, stream::{IterStream, ReadyExt, Tools as StreamTools, TryReadyExt},