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>
impl Encoding<OPTIONS, Binary>
sourcepub const fn new() -> Self
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>
impl<const OPT: Options, M> Encoding<OPT, M>
sourcepub const fn with_mode<T>(self) -> Encoding<OPT, T>
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();
sourcepub const fn with_options<const U: Options>(self) -> Encoding<U, M>
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();
sourcepub fn encode_with<C, W, T>(
self,
cx: &C,
writer: W,
value: &T
) -> Result<(), C::Error>
pub fn encode_with<C, W, T>( self, cx: &C, writer: W, value: &T ) -> Result<(), C::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: &C, reader: R) -> Result<T, C::Error>
pub fn decode_with<'de, C, R, T>(self, cx: &C, reader: R) -> Result<T, C::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_writer_with<C, W, T>(
self,
cx: &C,
write: W,
value: &T
) -> Result<(), C::Error>
pub fn to_writer_with<C, W, T>( self, cx: &C, write: W, value: &T ) -> Result<(), C::Error>
Encode the given value to the given Write using the current
configuration and context C
.
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: &C, value: &T) -> Result<Vec<u8>, C::Error>
pub fn to_vec_with<C, T>(self, cx: &C, value: &T) -> Result<Vec<u8>, C::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: &C,
value: &T
) -> Result<FixedBytes<N>, C::Error>
pub fn to_fixed_bytes_with<C, const N: usize, T>( self, cx: &C, value: &T ) -> Result<FixedBytes<N>, C::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: &C,
bytes: &'de [u8]
) -> Result<T, C::Error>
pub fn from_slice_with<'de, C, T>( self, cx: &C, bytes: &'de [u8] ) -> Result<T, C::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
.