diff --git a/crates/ruma-common/src/serde.rs b/crates/ruma-common/src/serde.rs index 7c652efa..5af36f9c 100644 --- a/crates/ruma-common/src/serde.rs +++ b/crates/ruma-common/src/serde.rs @@ -37,11 +37,13 @@ pub use self::{ pub type JsonObject = serde_json::Map; /// Check whether a value is equal to its default value. +#[inline] pub fn is_default(val: &T) -> bool { *val == T::default() } /// Deserialize a `T` via `Option`, falling back to `T::default()`. +#[inline] pub fn none_as_default<'de, D, T>(deserializer: D) -> Result where D: Deserializer<'de>, @@ -53,6 +55,7 @@ where /// Simply returns `true`. /// /// Useful for `#[serde(default = ...)]`. +#[inline] pub fn default_true() -> bool { true } @@ -61,11 +64,13 @@ pub fn default_true() -> bool { /// /// Useful for `#[serde(skip_serializing_if = ...)]`. #[allow(clippy::trivially_copy_pass_by_ref)] +#[inline] pub fn is_true(b: &bool) -> bool { *b } /// Helper function for `serde_json::value::RawValue` deserialization. +#[inline(never)] pub fn from_raw_json_value<'a, T, E>(val: &'a RawJsonValue) -> Result where T: Deserialize<'a>, diff --git a/crates/ruma-common/src/serde/base64.rs b/crates/ruma-common/src/serde/base64.rs index a7e2e78b..c1100d89 100644 --- a/crates/ruma-common/src/serde/base64.rs +++ b/crates/ruma-common/src/serde/base64.rs @@ -65,16 +65,19 @@ impl Base64 { impl> Base64 { /// Create a `Base64` instance from raw bytes, to be base64-encoded in serialization. + #[inline] pub fn new(bytes: B) -> Self { Self { bytes, _phantom_conf: PhantomData } } /// Get a reference to the raw bytes held by this `Base64` instance. + #[inline] pub fn as_bytes(&self) -> &[u8] { self.bytes.as_ref() } /// Encode the bytes contained in this `Base64` instance to unpadded base64. + #[inline] pub fn encode(&self) -> String { Self::ENGINE.encode(self.as_bytes()) } @@ -82,6 +85,7 @@ impl> Base64 { impl Base64 { /// Get the raw bytes held by this `Base64` instance. + #[inline] pub fn into_inner(self) -> B { self.bytes } @@ -89,11 +93,13 @@ impl Base64 { impl Base64 { /// Create a `Base64` instance containing an empty `Vec`. + #[inline] pub fn empty() -> Self { Self::new(Vec::new()) } /// Parse some base64-encoded data to create a `Base64` instance. + #[inline] pub fn parse(encoded: impl AsRef<[u8]>) -> Result { Self::ENGINE.decode(encoded).map(Self::new).map_err(Base64DecodeError) } diff --git a/crates/ruma-common/src/serde/raw.rs b/crates/ruma-common/src/serde/raw.rs index 189030f5..3b2b1c71 100644 --- a/crates/ruma-common/src/serde/raw.rs +++ b/crates/ruma-common/src/serde/raw.rs @@ -110,10 +110,12 @@ impl Raw { impl Visitor<'_> for FieldVisitor<'_> { type Value = bool; + #[inline] fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { write!(formatter, "`{}`", self.0) } + #[inline] fn visit_str(self, value: &str) -> Result where E: de::Error, @@ -127,6 +129,7 @@ impl Raw { impl<'de> DeserializeSeed<'de> for Field<'_> { type Value = bool; + #[inline] fn deserialize(self, deserializer: D) -> Result where D: Deserializer<'de>, @@ -141,6 +144,7 @@ impl Raw { } impl<'b, T> SingleFieldVisitor<'b, T> { + #[inline] fn new(field_name: &'b str) -> Self { Self { field_name, _phantom: PhantomData } } @@ -152,10 +156,12 @@ impl Raw { { type Value = Option; + #[inline] fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("a string") } + #[inline(never)] fn visit_map(self, mut map: A) -> Result where A: MapAccess<'de>, @@ -178,6 +184,7 @@ impl Raw { } /// Try to deserialize the JSON as the expected type. + #[inline] pub fn deserialize<'a>(&'a self) -> serde_json::Result where T: Deserialize<'a>, @@ -186,6 +193,7 @@ impl Raw { } /// Try to deserialize the JSON as a custom type. + #[inline] pub fn deserialize_as<'a, U>(&'a self) -> serde_json::Result where U: Deserialize<'a>, @@ -196,6 +204,7 @@ impl Raw { /// Turns `Raw` into `Raw` without changing the underlying JSON. /// /// This is useful for turning raw specific event types into raw event enum types. + #[inline] pub fn cast(self) -> Raw { Raw::from_json(self.into_json()) } @@ -203,6 +212,7 @@ impl Raw { /// Turns `&Raw` into `&Raw` without changing the underlying JSON. /// /// This is useful for turning raw specific event types into raw event enum types. + #[inline] pub fn cast_ref(&self) -> &Raw { unsafe { mem::transmute(self) } } @@ -222,6 +232,7 @@ impl Debug for Raw { } impl<'de, T> Deserialize<'de> for Raw { + #[inline] fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, diff --git a/crates/ruma-events/src/kinds.rs b/crates/ruma-events/src/kinds.rs index 7384e163..751c79d1 100644 --- a/crates/ruma-events/src/kinds.rs +++ b/crates/ruma-events/src/kinds.rs @@ -351,6 +351,7 @@ impl InitialStateEvent { /// Create a new `InitialStateEvent` for an event type with an empty state key. /// /// For cases where the state key is not empty, use a struct literal to create the event. + #[inline] pub fn new(content: C) -> Self where C: StaticStateEventContent, @@ -365,6 +366,7 @@ impl InitialStateEvent { /// with a `Serialize` implementation that can error (for example because it contains an /// `enum` with one or more variants that use the `#[serde(skip)]` attribute), this method /// can panic. + #[inline] pub fn to_raw(&self) -> Raw { Raw::new(self).unwrap() } @@ -376,6 +378,7 @@ impl InitialStateEvent { /// with a `Serialize` implementation that can error (for example because it contains an /// `enum` with one or more variants that use the `#[serde(skip)]` attribute), this method /// can panic. + #[inline] pub fn to_raw_any(&self) -> Raw { self.to_raw().cast() } @@ -579,6 +582,7 @@ where C::Redacted: RedactedStateEventContent, { /// Get the event’s type, like `m.room.create`. + #[inline] pub fn event_type(&self) -> StateEventType { match self { Self::Original { content, .. } => content.event_type(), @@ -592,6 +596,7 @@ where /// /// A small number of events have room-version specific redaction behavior, so a version has to /// be specified. + #[inline] pub fn redact(self, version: &RoomVersionId) -> C::Redacted { match self { FullStateEventContent::Original { content, .. } => content.redact(version), @@ -613,6 +618,7 @@ macro_rules! impl_possibly_redacted_event { $( C::Redacted: $trait, )? { /// Returns the `type` of this event. + #[inline] pub fn event_type(&self) -> $event_type { match self { Self::Original(ev) => ev.content.event_type(), @@ -621,6 +627,7 @@ macro_rules! impl_possibly_redacted_event { } /// Returns this event's `event_id` field. + #[inline] pub fn event_id(&self) -> &EventId { match self { Self::Original(ev) => &ev.event_id, @@ -629,6 +636,7 @@ macro_rules! impl_possibly_redacted_event { } /// Returns this event's `sender` field. + #[inline] pub fn sender(&self) -> &UserId { match self { Self::Original(ev) => &ev.sender, @@ -637,6 +645,7 @@ macro_rules! impl_possibly_redacted_event { } /// Returns this event's `origin_server_ts` field. + #[inline] pub fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch { match self { Self::Original(ev) => ev.origin_server_ts, @@ -676,6 +685,7 @@ impl_possibly_redacted_event!( MessageLikeEventContent, RedactedMessageLikeEventContent, MessageLikeEventType ) { /// Returns this event's `room_id` field. + #[inline] pub fn room_id(&self) -> &RoomId { match self { Self::Original(ev) => &ev.room_id, @@ -684,6 +694,7 @@ impl_possibly_redacted_event!( } /// Get the inner `OriginalMessageLikeEvent` if this is an unredacted event. + #[inline] pub fn as_original(&self) -> Option<&OriginalMessageLikeEvent> { as_variant!(self, Self::Original) } @@ -695,11 +706,13 @@ impl_possibly_redacted_event!( MessageLikeEventContent, RedactedMessageLikeEventContent, MessageLikeEventType ) { /// Get the inner `OriginalSyncMessageLikeEvent` if this is an unredacted event. + #[inline] pub fn as_original(&self) -> Option<&OriginalSyncMessageLikeEvent> { as_variant!(self, Self::Original) } /// Convert this sync event into a full event (one with a `room_id` field). + #[inline] pub fn into_full_event(self, room_id: OwnedRoomId) -> MessageLikeEvent { match self { Self::Original(ev) => MessageLikeEvent::Original(ev.into_full_event(room_id)), @@ -715,6 +728,7 @@ impl_possibly_redacted_event!( C::Redacted: RedactedStateEventContent, { /// Returns this event's `room_id` field. + #[inline] pub fn room_id(&self) -> &RoomId { match self { Self::Original(ev) => &ev.room_id, @@ -723,6 +737,7 @@ impl_possibly_redacted_event!( } /// Returns this event's `state_key` field. + #[inline] pub fn state_key(&self) -> &C::StateKey { match self { Self::Original(ev) => &ev.state_key, @@ -731,6 +746,7 @@ impl_possibly_redacted_event!( } /// Get the inner `OriginalStateEvent` if this is an unredacted event. + #[inline] pub fn as_original(&self) -> Option<&OriginalStateEvent> { as_variant!(self, Self::Original) } @@ -743,6 +759,7 @@ impl_possibly_redacted_event!( C::Redacted: RedactedStateEventContent, { /// Returns this event's `state_key` field. + #[inline] pub fn state_key(&self) -> &C::StateKey { match self { Self::Original(ev) => &ev.state_key, @@ -751,11 +768,13 @@ impl_possibly_redacted_event!( } /// Get the inner `OriginalSyncStateEvent` if this is an unredacted event. + #[inline] pub fn as_original(&self) -> Option<&OriginalSyncStateEvent> { as_variant!(self, Self::Original) } /// Convert this sync event into a full event (one with a `room_id` field). + #[inline] pub fn into_full_event(self, room_id: OwnedRoomId) -> StateEvent { match self { Self::Original(ev) => StateEvent::Original(ev.into_full_event(room_id)), @@ -772,6 +791,7 @@ macro_rules! impl_sync_from_full { C: $content_trait + RedactContent, C::Redacted: $redacted_content_trait, { + #[inline] fn from(full: $full) -> Self { match full { $full::Original(ev) => Self::Original(ev.into()), diff --git a/crates/ruma-macros/src/identifiers.rs b/crates/ruma-macros/src/identifiers.rs index 17e1d327..89afd8e3 100644 --- a/crates/ruma-macros/src/identifiers.rs +++ b/crates/ruma-macros/src/identifiers.rs @@ -490,6 +490,7 @@ fn expand_checked_impls(input: &ItemStruct, validate: Path) -> TokenStream { quote! { #[automatically_derived] impl #impl_generics #id_ty { + #[inline] #[doc = #parse_doc_header] /// /// The same can also be done using `FromStr`, `TryFrom` or `TryInto`. @@ -502,6 +503,7 @@ fn expand_checked_impls(input: &ItemStruct, validate: Path) -> TokenStream { Ok(#id::from_borrowed(s).to_owned()) } + #[inline] #[doc = #parse_box_doc_header] /// /// The same can also be done using `FromStr`, `TryFrom` or `TryInto`. @@ -513,6 +515,7 @@ fn expand_checked_impls(input: &ItemStruct, validate: Path) -> TokenStream { Ok(#id::from_box(s.into())) } + #[inline] #[doc = #parse_rc_docs] pub fn parse_rc( s: impl AsRef + Into>, @@ -521,6 +524,7 @@ fn expand_checked_impls(input: &ItemStruct, validate: Path) -> TokenStream { Ok(#id::from_rc(s.into())) } + #[inline] #[doc = #parse_arc_docs] pub fn parse_arc( s: impl AsRef + Into>,