Struct musli_wire::encoding::Encoding

source ·
pub struct Encoding<const OPT: Options = OPTIONS, M = Binary> { /* private fields */ }
Expand description

Setting up encoding with parameters.

Implementations§

source§

impl Encoding<OPTIONS, Binary>

source

pub const fn new() -> Self

Construct a new Encoding instance with the OPTIONS configuration.

You can modify this using the available factory methods:

use musli::{Encode, Decode};
use musli_utils::options::{self, Options, Integer};
use musli_wire::Encoding;

const OPTIONS: Options = options::new().with_integer(Integer::Fixed).build();
const CONFIG: Encoding<OPTIONS> = Encoding::new().with_options();

#[derive(Debug, PartialEq, Encode, Decode)]
struct Struct<'a> {
    name: &'a str,
    age: u32,
}

let mut out = Vec::new();

let expected = Struct {
    name: "Aristotle",
    age: 61,
};

CONFIG.encode(&mut out, &expected)?;
let actual = CONFIG.decode(&out[..])?;

assert_eq!(expected, actual);
source§

impl<const OPT: Options, M> Encoding<OPT, M>

source

pub const fn with_mode<T>(self) -> Encoding<OPT, T>

Change the mode of the encoding.

§Examples
use musli_wire::{OPTIONS, Encoding};

enum Custom {}

const CONFIG: Encoding<OPTIONS, Custom> = Encoding::new().with_mode();
source

pub const fn with_options<const U: Options>(self) -> Encoding<U, M>

Change the options of the encoding.

§Examples
use musli_utils::options::{self, Options, Integer};
use musli_wire::Encoding;

const OPTIONS: Options = options::new().with_integer(Integer::Fixed).build();
const CONFIG: Encoding<OPTIONS> = Encoding::new().with_options();
source

pub fn encode_with<C, W, T>( self, cx: &C, writer: W, value: &T ) -> Result<(), C::Error>
where C: ?Sized + Context<Mode = M>, W: Writer, T: ?Sized + Encode<M>,

Encode the given value to the given Writer using the current configuration.

This is the same as Encoding::encode but allows for using a configurable Context.

source

pub fn decode_with<'de, C, R, T>(self, cx: &C, reader: R) -> Result<T, C::Error>
where C: ?Sized + Context<Mode = M>, R: Reader<'de>, T: Decode<'de, M>,

Decode the given type T from the given Reader using the current configuration.

This is the same as Encoding::decode but allows for using a configurable Context.

source

pub fn decode<'de, R, T>(self, reader: R) -> Result<T, Error>
where R: Reader<'de>, T: Decode<'de, M>,

Decode the given type T from the given Reader using the current configuration.

source

pub fn encode<W, T>(self, writer: W, value: &T) -> Result<(), Error>
where W: Writer, T: ?Sized + Encode<M>,

Encode the given value to the given Writer using the current configuration.

source

pub fn to_writer<W, T>(self, write: W, value: &T) -> Result<(), Error>
where W: Write, T: ?Sized + Encode<M>,

Encode the given value to the given Write using the current configuration.

source

pub fn to_writer_with<C, W, T>( self, cx: &C, write: W, value: &T ) -> Result<(), C::Error>
where C: ?Sized + Context<Mode = M>, W: Write, T: ?Sized + Encode<M>,

Encode the given value to the given Write using the current configuration and context C.

source

pub fn to_vec<T>(self, value: &T) -> Result<Vec<u8>, Error>
where T: ?Sized + Encode<M>,

Encode the given value to a Vec using the current configuration.

source

pub fn to_vec_with<C, T>(self, cx: &C, value: &T) -> Result<Vec<u8>, C::Error>
where C: ?Sized + Context<Mode = M>, T: ?Sized + Encode<M>,

Encode the given value to a Vec using the current configuration.

This is the same as Encoding::to_vec, but allows for using a configurable Context.

source

pub fn to_fixed_bytes<const N: usize, T>( self, value: &T ) -> Result<FixedBytes<N>, Error>
where T: ?Sized + Encode<M>,

Encode the given value to a fixed-size bytes using the current configuration.

source

pub fn to_fixed_bytes_with<C, const N: usize, T>( self, cx: &C, value: &T ) -> Result<FixedBytes<N>, C::Error>
where C: ?Sized + Context<Mode = M>, T: ?Sized + Encode<M>,

Encode the given value to a fixed-size bytes using the current configuration.

source

pub fn from_slice<'de, T>(self, bytes: &'de [u8]) -> Result<T, Error>
where T: Decode<'de, M>,

Decode the given type T from the given slice using the current configuration.

source

pub fn from_slice_with<'de, C, T>( self, cx: &C, bytes: &'de [u8] ) -> Result<T, C::Error>
where C: ?Sized + Context<Mode = M>, T: Decode<'de, M>,

Decode the given type T from the given slice using the current configuration.

This is the same as Encoding::from_slice, but allows for using a configurable Context.

Trait Implementations§

source§

impl<const OPT: Options, M> Clone for Encoding<OPT, M>

source§

fn clone(&self) -> Self

Returns a copy 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<const OPT: Options, M> Copy for Encoding<OPT, M>

Auto Trait Implementations§

§

impl<const OPT: u128, M> Freeze for Encoding<OPT, M>

§

impl<const OPT: u128, M> RefUnwindSafe for Encoding<OPT, M>
where M: RefUnwindSafe,

§

impl<const OPT: u128, M> Send for Encoding<OPT, M>
where M: Send,

§

impl<const OPT: u128, M> Sync for Encoding<OPT, M>
where M: Sync,

§

impl<const OPT: u128, M> Unpin for Encoding<OPT, M>
where M: Unpin,

§

impl<const OPT: u128, M> UnwindSafe for Encoding<OPT, M>
where M: UnwindSafe,

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> 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> ToOwned for T
where T: Clone,

§

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

§

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

§

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.