various inlines

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-11-05 03:09:13 +00:00
parent 112ccc24cb
commit 9bdc048cdb
5 changed files with 46 additions and 0 deletions

View File

@ -37,11 +37,13 @@ pub use self::{
pub type JsonObject = serde_json::Map<String, JsonValue>;
/// Check whether a value is equal to its default value.
#[inline]
pub fn is_default<T: Default + PartialEq>(val: &T) -> bool {
*val == T::default()
}
/// Deserialize a `T` via `Option<T>`, falling back to `T::default()`.
#[inline]
pub fn none_as_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
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<T, E>
where
T: Deserialize<'a>,

View File

@ -65,16 +65,19 @@ impl<C: Base64Config, B> Base64<C, B> {
impl<C: Base64Config, B: AsRef<[u8]>> Base64<C, B> {
/// 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<C: Base64Config, B: AsRef<[u8]>> Base64<C, B> {
impl<C, B> Base64<C, B> {
/// Get the raw bytes held by this `Base64` instance.
#[inline]
pub fn into_inner(self) -> B {
self.bytes
}
@ -89,11 +93,13 @@ impl<C, B> Base64<C, B> {
impl<C: Base64Config> Base64<C> {
/// Create a `Base64` instance containing an empty `Vec<u8>`.
#[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, Base64DecodeError> {
Self::ENGINE.decode(encoded).map(Self::new).map_err(Base64DecodeError)
}

View File

@ -110,10 +110,12 @@ impl<T> Raw<T> {
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<E>(self, value: &str) -> Result<bool, E>
where
E: de::Error,
@ -127,6 +129,7 @@ impl<T> Raw<T> {
impl<'de> DeserializeSeed<'de> for Field<'_> {
type Value = bool;
#[inline]
fn deserialize<D>(self, deserializer: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
@ -141,6 +144,7 @@ impl<T> Raw<T> {
}
impl<'b, T> SingleFieldVisitor<'b, T> {
#[inline]
fn new(field_name: &'b str) -> Self {
Self { field_name, _phantom: PhantomData }
}
@ -152,10 +156,12 @@ impl<T> Raw<T> {
{
type Value = Option<T>;
#[inline]
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a string")
}
#[inline(never)]
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
@ -178,6 +184,7 @@ impl<T> Raw<T> {
}
/// Try to deserialize the JSON as the expected type.
#[inline]
pub fn deserialize<'a>(&'a self) -> serde_json::Result<T>
where
T: Deserialize<'a>,
@ -186,6 +193,7 @@ impl<T> Raw<T> {
}
/// Try to deserialize the JSON as a custom type.
#[inline]
pub fn deserialize_as<'a, U>(&'a self) -> serde_json::Result<U>
where
U: Deserialize<'a>,
@ -196,6 +204,7 @@ impl<T> Raw<T> {
/// Turns `Raw<T>` into `Raw<U>` without changing the underlying JSON.
///
/// This is useful for turning raw specific event types into raw event enum types.
#[inline]
pub fn cast<U>(self) -> Raw<U> {
Raw::from_json(self.into_json())
}
@ -203,6 +212,7 @@ impl<T> Raw<T> {
/// Turns `&Raw<T>` into `&Raw<U>` without changing the underlying JSON.
///
/// This is useful for turning raw specific event types into raw event enum types.
#[inline]
pub fn cast_ref<U>(&self) -> &Raw<U> {
unsafe { mem::transmute(self) }
}
@ -222,6 +232,7 @@ impl<T> Debug for Raw<T> {
}
impl<'de, T> Deserialize<'de> for Raw<T> {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,

View File

@ -351,6 +351,7 @@ impl<C: StaticStateEventContent> InitialStateEvent<C> {
/// 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<StateKey = EmptyStateKey>,
@ -365,6 +366,7 @@ impl<C: StaticStateEventContent> InitialStateEvent<C> {
/// 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<Self> {
Raw::new(self).unwrap()
}
@ -376,6 +378,7 @@ impl<C: StaticStateEventContent> InitialStateEvent<C> {
/// 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<AnyInitialStateEvent> {
self.to_raw().cast()
}
@ -579,6 +582,7 @@ where
C::Redacted: RedactedStateEventContent,
{
/// Get the events 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<StateKey = C::StateKey>, )?
{
/// 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<C>> {
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<C>> {
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<C> {
match self {
Self::Original(ev) => MessageLikeEvent::Original(ev.into_full_event(room_id)),
@ -715,6 +728,7 @@ impl_possibly_redacted_event!(
C::Redacted: RedactedStateEventContent<StateKey = C::StateKey>,
{
/// 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<C>> {
as_variant!(self, Self::Original)
}
@ -743,6 +759,7 @@ impl_possibly_redacted_event!(
C::Redacted: RedactedStateEventContent<StateKey = C::StateKey>,
{
/// 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<C>> {
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<C> {
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<C>) -> Self {
match full {
$full::Original(ev) => Self::Original(ev.into()),

View File

@ -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<str> + Into<std::rc::Rc<str>>,
@ -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<str> + Into<std::sync::Arc<str>>,