//! Change MessagePack behavior with configuration wrappers.
/// Represents configuration that dicatates what the serializer does. /// /// Implemented as an empty trait depending on a hidden trait in order to allow changing the /// methods of this trait without breaking backwards compatibility. pubtrait SerializerConfig: sealed::SerializerConfig {}
impl<T: sealed::SerializerConfig> SerializerConfig for T {}
pub(crate) mod sealed { usecrate::config::BytesMode;
/// This is the inner trait - the real `SerializerConfig`. /// /// This hack disallows external implementations and usage of `SerializerConfig` and thus /// allows us to change `SerializerConfig` methods freely without breaking backwards compatibility. pubtrait SerializerConfig: Copy { /// Determines the value of `Serializer::is_human_readable` and /// `Deserializer::is_human_readable`. fn is_human_readable(&self) -> bool;
/// When to encode `[u8]` as `bytes` rather than a sequence /// of integers. Serde without `serde_bytes` has trouble /// using `bytes`, and this is hack to force it. It may /// break some data types. #[non_exhaustive] #[derive(Debug, Copy, Clone, Default, PartialEq, Eq)] pubenum BytesMode { /// Use bytes only when Serde requires it /// (typically only when `serde_bytes` is used) #[default]
Normal, /// Use bytes for slices, `Vec`, and a few other types that /// use `Iterator` in Serde. /// /// This may break some implementations of `Deserialize`. /// /// This does not include fixed-length arrays.
ForceIterables, /// Use bytes for everything that looks like a container of `u8`. /// This breaks some implementations of `Deserialize`.
ForceAll,
}
/// The default serializer/deserializer configuration. /// /// This configuration: /// - Writes structs as a tuple, without field names /// - Writes enum variants as integers /// - Writes and reads types as binary, not human-readable // /// This is the most compact representation. #[derive(Copy, Clone, Debug)] pubstruct DefaultConfig;
/// Config wrapper, that overrides struct serialization by packing as a map with field names. /// /// MessagePack specification does not tell how to serialize structs. This trait allows you to /// extend serialization to match your app's requirements. /// /// Default `Serializer` implementation writes structs as a tuple, i.e. only its length is encoded, /// because it is the most compact representation. #[derive(Copy, Clone, Debug)] pubstruct StructMapConfig<C>(C);
impl<C> StructMapConfig<C> { /// Creates a `StructMapConfig` inheriting unchanged configuration options from the given configuration. #[inline] pubfn new(inner: C) -> Self {
StructMapConfig(inner)
}
}
impl<C> sealed::SerializerConfig for StructMapConfig<C> where
C: sealed::SerializerConfig,
{ #[inline(always)] fn is_named(&self) -> bool { true
}
/// Config wrapper that overrides struct serlization by packing as a tuple without field /// names. #[derive(Copy, Clone, Debug)] pubstruct StructTupleConfig<C>(C);
impl<C> StructTupleConfig<C> { /// Creates a `StructTupleConfig` inheriting unchanged configuration options from the given configuration. #[inline] pubfn new(inner: C) -> Self {
StructTupleConfig(inner)
}
}
impl<C> sealed::SerializerConfig for StructTupleConfig<C> where
C: sealed::SerializerConfig,
{ #[inline(always)] fn is_named(&self) -> bool { false
}
/// Config wrapper that overrides `Serializer::is_human_readable` and /// `Deserializer::is_human_readable` to return `true`. #[derive(Copy, Clone, Debug)] pubstruct HumanReadableConfig<C>(C);
impl<C> HumanReadableConfig<C> { /// Creates a `HumanReadableConfig` inheriting unchanged configuration options from the given configuration. #[inline] pubfn new(inner: C) -> Self { Self(inner)
}
}
impl<C> sealed::SerializerConfig for HumanReadableConfig<C> where
C: sealed::SerializerConfig,
{ #[inline(always)] fn is_named(&self) -> bool { self.0.is_named()
}
/// Config wrapper that overrides `Serializer::is_human_readable` and /// `Deserializer::is_human_readable` to return `false`. #[derive(Copy, Clone, Debug)] pubstruct BinaryConfig<C>(C);
impl<C> BinaryConfig<C> { /// Creates a `BinaryConfig` inheriting unchanged configuration options from the given configuration. #[inline(always)] pubfn new(inner: C) -> Self { Self(inner)
}
}
impl<C> sealed::SerializerConfig for BinaryConfig<C> where
C: sealed::SerializerConfig,
{ #[inline(always)] fn is_named(&self) -> bool { self.0.is_named()
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.