Context

Struct Context 

Source
#[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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional 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: BinSettings

The 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>

Source

pub const fn new() -> Self

Constructs the default encoder state. Options will be set to default, flatten to None.

Source

pub const fn settings(self, settings: BinSettings) -> Self

Replaces the settings with settings, then returns self.

Source

pub const fn user_data<'b>(self, data: &'b dyn Any) -> Context<'b>

Replaces the user data with data, then returns self.

Source

pub const fn bool_flatten(self, value: bool) -> Self

Replaces the bool flatten state variable with Some(value), then returns self.

Source

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) or Opaque::from(3 as u32)
  • Better yet, use an explicit opaque value (Opaque::signed, Opaque::unsigned)
Source

pub const fn size_flatten(self, value: usize) -> Self

Replaces the size flatten state variable with Some(value), then returns self.

Source

pub fn with_settings(settings: BinSettings) -> Self

Just like Self::new but uses the given settings instead of the default.

Source

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.

Source

pub fn reset(&mut self, options: BinSettings)

Resets the context to its defaults, then overwrites the options with the given options.

Source

pub fn consume_bool_flatten(&mut self) -> Option<bool>

Returns the state of the bool flatten variable, consuming it.

Source

pub fn consume_variant_flatten(&mut self) -> Option<Opaque>

Returns the state of the Variant flatten variable, consuming it.

Source

pub fn consume_size_flatten(&mut self) -> Option<usize>

Returns the state of the usize flatten variable, consuming it.

Trait Implementations§

Source§

impl<'a> Clone for Context<'a>

Source§

fn clone(&self) -> Context<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Context<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for Context<'a>

Source§

fn default() -> Context<'a>

Returns the “default value” for a type. Read more
Source§

impl<'a> Copy for Context<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Context<'a>

§

impl<'a> !RefUnwindSafe for Context<'a>

§

impl<'a> !Send for Context<'a>

§

impl<'a> !Sync for Context<'a>

§

impl<'a> Unpin for Context<'a>

§

impl<'a> !UnwindSafe for Context<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoResult<T> for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.