#[non_exhaustive]pub struct Context<'a> {
pub user: Option<&'a dyn Any>,
pub settings: BinSettings,
pub bool_flatten: Option<bool>,
pub variant_flatten: Option<Opaque>,
pub size_flatten: Option<usize>,
}Expand description
The state of the encoder, including its options and a flatten state variable
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.user: Option<&'a dyn Any>User provided data. This can be purposed for storing any kind of data, like cryptographic keys that are unknown to the data structures but known at a higher level.
settings: BinSettingsThe actual settings, which determine the numerical representations and the string representations.
Implementations of Encode and Decode are required to
preserve the state of the settings, even though they are allowed to temporarily modify it.
In case of an error occurring, no guarantee is made about the state of the settings: for this reason it’s good practice to store a copy of the settings somewhere.
bool_flatten: Option<bool>The bool flatten state variable.
When present, for Option, Result and any bool it indicates,
while Encoding not to write the value,
and while Decoding it contains the boolean value itself
(it won’t be read from the stream).
variant_flatten: Option<Opaque>The Variant flatten state variable.
When present, for any enum it indicates, while Encoding not to write the value of the discriminant, and while Decoding it contains the discriminant value itself (it won’t be read from the stream).
size_flatten: Option<usize>The usize flatten state variable.
When present, for Vec, HashMap and other data
structures with a length it indicates while Encoding not to write said length,
and while Decoding it contains the length itself
(it won’t be read from the stream).
Implementations§
Source§impl<'a> Context<'a>
impl<'a> Context<'a>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Constructs the default encoder state. Options will be set to default, flatten to None.
Sourcepub const fn settings(self, settings: BinSettings) -> Self
pub const fn settings(self, settings: BinSettings) -> Self
Replaces the settings with settings, then returns self.
Sourcepub const fn user_data<'b>(self, data: &'b dyn Any) -> Context<'b>
pub const fn user_data<'b>(self, data: &'b dyn Any) -> Context<'b>
Replaces the user data with data, then returns self.
Sourcepub const fn bool_flatten(self, value: bool) -> Self
pub const fn bool_flatten(self, value: bool) -> Self
Replaces the bool flatten state variable with Some(value), then returns self.
Sourcepub const fn variant_flatten(self, value: Opaque) -> Self
pub const fn variant_flatten(self, value: Opaque) -> Self
Replaces the variant flatten state variable with Some(value), then returns self.
Beware of providing an Opaque with an integer literal without a specific type!
By default, rust will infer i32 as the type, thus it will be converted to a Signed
enum variant value, and you will get a (bad) surprise when you try to then encode/decode
an enum that uses Unsigned variant discriminants (most enums).
How to avoid this?
- Write the type in the num literal E.G.
Opaque::from(3u32)orOpaque::from(3 as u32) - Better yet, use an explicit opaque value (
Opaque::signed,Opaque::unsigned)
Sourcepub const fn size_flatten(self, value: usize) -> Self
pub const fn size_flatten(self, value: usize) -> Self
Replaces the size flatten state variable with Some(value), then returns self.
Sourcepub fn with_settings(settings: BinSettings) -> Self
pub fn with_settings(settings: BinSettings) -> Self
Just like Self::new but uses the given settings instead of the default.
Sourcepub fn with_user_data(settings: BinSettings, data: &'a dyn Any) -> Self
pub fn with_user_data(settings: BinSettings, data: &'a dyn Any) -> Self
Just like Self::new but uses the given settings instead of the default
and the given user data.
Sourcepub fn reset(&mut self, options: BinSettings)
pub fn reset(&mut self, options: BinSettings)
Resets the context to its defaults, then overwrites the options with the given options.
Sourcepub fn consume_bool_flatten(&mut self) -> Option<bool>
pub fn consume_bool_flatten(&mut self) -> Option<bool>
Returns the state of the bool flatten variable, consuming it.
Sourcepub fn consume_variant_flatten(&mut self) -> Option<Opaque>
pub fn consume_variant_flatten(&mut self) -> Option<Opaque>
Returns the state of the Variant flatten variable, consuming it.
Sourcepub fn consume_size_flatten(&mut self) -> Option<usize>
pub fn consume_size_flatten(&mut self) -> Option<usize>
Returns the state of the usize flatten variable, consuming it.