pub struct Encoding<M = DefaultMode, I = Variable, L = Variable>where
I: WireIntegerEncoding,
L: WireUsizeEncoding,{ /* private fields */ }Expand description
Setting up encoding with parameters.
Implementations§
Source§impl Encoding
impl Encoding
Sourcepub const fn new() -> Encoding
pub const fn new() -> Encoding
Construct a new Encoding instance which uses Variable integer encoding.
You can modify this using the available factory methods:
use musli_wire::Encoding;
use musli_wire::int::{Fixed, Variable};
use musli::{Encode, Decode};
use musli::mode::DefaultMode;
const CONFIG: Encoding<DefaultMode, Fixed, Variable> = Encoding::new()
.with_fixed_integers();
#[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<M, I, L> Encoding<M, I, L>where
M: Mode,
I: WireIntegerEncoding,
L: WireUsizeEncoding,
impl<M, I, L> Encoding<M, I, L>where
M: Mode,
I: WireIntegerEncoding,
L: WireUsizeEncoding,
Sourcepub const fn with_mode<T>(self) -> Encoding<T, I, L>where
T: Mode,
pub const fn with_mode<T>(self) -> Encoding<T, I, L>where
T: Mode,
Change the mode of the encoding.
Sourcepub const fn with_variable_integers(self) -> Encoding<M, Variable, L>
pub const fn with_variable_integers(self) -> Encoding<M, Variable, L>
Configure the encoding to use variable integer encoding.
Sourcepub const fn with_fixed_integers(self) -> Encoding<M, Fixed, L>
pub const fn with_fixed_integers(self) -> Encoding<M, Fixed, L>
Configure the encoding to use fixed integer encoding.
Sourcepub const fn with_fixed_integers_le(self) -> Encoding<M, Fixed<LittleEndian>, L>
pub const fn with_fixed_integers_le(self) -> Encoding<M, Fixed<LittleEndian>, L>
Configure the encoding to use fixed integer little-endian encoding.
Sourcepub const fn with_fixed_integers_be(self) -> Encoding<M, Fixed, L>
pub const fn with_fixed_integers_be(self) -> Encoding<M, Fixed, L>
Configure the encoding to use fixed integer big-endian encoding.
Sourcepub const fn with_fixed_integers_ne(self) -> Encoding<M, Fixed, L>
pub const fn with_fixed_integers_ne(self) -> Encoding<M, Fixed, L>
Configure the encoding to use fixed integer network-endian encoding (Default).
Sourcepub const fn with_fixed_integers_endian<E>(self) -> Encoding<M, Fixed<E>, L>where
E: ByteOrder,
pub const fn with_fixed_integers_endian<E>(self) -> Encoding<M, Fixed<E>, L>where
E: ByteOrder,
Configure the encoding to use fixed integer custom endian encoding.
Sourcepub const fn with_variable_lengths(self) -> Encoding<M, I>
pub const fn with_variable_lengths(self) -> Encoding<M, I>
Configure the encoding to use variable length encoding.
Sourcepub const fn with_fixed_lengths(self) -> Encoding<M, I, FixedUsize>
pub const fn with_fixed_lengths(self) -> Encoding<M, I, FixedUsize>
Configure the encoding to use fixed length 32-bit encoding when encoding lengths.
Sourcepub const fn with_fixed_lengths64(self) -> Encoding<M, I, FixedUsize<u64>>
pub const fn with_fixed_lengths64(self) -> Encoding<M, I, FixedUsize<u64>>
Configure the encoding to use fixed length 64-bit encoding when encoding lengths.
Sourcepub fn encode_with<C, W, T>(
self,
cx: &mut C,
writer: W,
value: &T,
) -> Result<(), <C as Context>::Error>
pub fn encode_with<C, W, T>( self, cx: &mut C, writer: W, value: &T, ) -> Result<(), <C as Context>::Error>
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.
Sourcepub fn decode_with<'de, C, R, T>(
self,
cx: &mut C,
reader: R,
) -> Result<T, <C as Context>::Error>
pub fn decode_with<'de, C, R, T>( self, cx: &mut C, reader: R, ) -> Result<T, <C as Context>::Error>
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.
Sourcepub fn decode<'de, R, T>(self, reader: R) -> Result<T, Error>
pub fn decode<'de, R, T>(self, reader: R) -> Result<T, Error>
Decode the given type T from the given Reader using the current
configuration.
Sourcepub fn encode<W, T>(self, writer: W, value: &T) -> Result<(), Error>
pub fn encode<W, T>(self, writer: W, value: &T) -> Result<(), Error>
Encode the given value to the given Writer using the current configuration.
Sourcepub fn to_writer<W, T>(self, write: W, value: &T) -> Result<(), Error>
pub fn to_writer<W, T>(self, write: W, value: &T) -> Result<(), Error>
Encode the given value to the given Write using the current configuration.
Sourcepub fn to_vec<T>(self, value: &T) -> Result<Vec<u8>, Error>
pub fn to_vec<T>(self, value: &T) -> Result<Vec<u8>, Error>
Encode the given value to a Vec using the current configuration.
Sourcepub fn to_vec_with<C, T>(
self,
cx: &mut C,
value: &T,
) -> Result<Vec<u8>, <C as Context>::Error>
pub fn to_vec_with<C, T>( self, cx: &mut C, value: &T, ) -> Result<Vec<u8>, <C as Context>::Error>
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.
Sourcepub fn to_fixed_bytes<const N: usize, T>(
self,
value: &T,
) -> Result<FixedBytes<N>, Error>
pub fn to_fixed_bytes<const N: usize, T>( self, value: &T, ) -> Result<FixedBytes<N>, Error>
Encode the given value to a fixed-size bytes using the current configuration.
Sourcepub fn to_fixed_bytes_with<C, const N: usize, T>(
self,
cx: &mut C,
value: &T,
) -> Result<FixedBytes<N>, <C as Context>::Error>
pub fn to_fixed_bytes_with<C, const N: usize, T>( self, cx: &mut C, value: &T, ) -> Result<FixedBytes<N>, <C as Context>::Error>
Encode the given value to a fixed-size bytes using the current configuration.
Sourcepub fn from_slice<'de, T>(self, bytes: &'de [u8]) -> Result<T, Error>where
T: Decode<'de, M>,
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.
Sourcepub fn from_slice_with<'de, C, T>(
self,
cx: &mut C,
bytes: &'de [u8],
) -> Result<T, <C as Context>::Error>
pub fn from_slice_with<'de, C, T>( self, cx: &mut C, bytes: &'de [u8], ) -> Result<T, <C as Context>::Error>
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.