pub struct Encoding<const OPT: Options = OPTIONS, M = Binary>where
M: 'static,{ /* 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 which uses OPTIONS.
You can modify this behavior by using a custom Options instance:
use musli::{Encode, Decode};
use musli::options::{self, Options, Integer};
use musli::packed::Encoding;
const OPTIONS: Options = options::new().integer(Integer::Fixed).build();
const CONFIG: Encoding<OPTIONS> = Encoding::new().with_options();
#[derive(Debug, PartialEq, Encode, Decode)]
struct Person<'a> {
name: &'a str,
age: u32,
}
let mut out = Vec::new();
let expected = Person {
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>where
M: 'static,
impl<const OPT: Options, M> Encoding<OPT, M>where
M: 'static,
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::packed::{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::options::{self, Options, Integer};
use musli::packed::Encoding;
const OPTIONS: Options = options::new().build();
const CONFIG: Encoding<OPTIONS> = Encoding::new().with_options();Sourcepub fn encode<W, T>(self, writer: W, value: &T) -> Result<W::Ok, Error>
pub fn encode<W, T>(self, writer: W, value: &T) -> Result<W::Ok, Error>
Encode the given value to the given Writer using the current
Encoding.
§Examples
use musli::{Decode, Encode};
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let mut data = Vec::new();
ENCODING.encode(&mut data, &Person {
name: "Aristotle".to_string(),
age: 61,
})?;
let person: Person = ENCODING.from_slice(&data[..])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);Sourcepub fn to_slice<T>(self, out: &mut [u8], value: &T) -> Result<usize, Error>
pub fn to_slice<T>(self, out: &mut [u8], value: &T) -> Result<usize, Error>
Encode the given value to the given slice using the current
Encoding and return the number of bytes encoded.
§Examples
use musli::{Decode, Encode};
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let mut data = Vec::new();
data.resize(128, 0);
let w = ENCODING.to_slice(&mut data, &Person {
name: "Aristotle".to_string(),
age: 61,
})?;
assert!(w > 0);
let person: Person = ENCODING.from_slice(&data[..w])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);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 Encoding.
§Examples
use musli::{Decode, Encode};
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let data = ENCODING.to_vec(&Person {
name: "Aristotle".to_string(),
age: 61,
})?;
let person: Person = ENCODING.from_slice(&data[..])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);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
Encoding.
§Examples
use musli::{Decode, Encode, FixedBytes};
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let data: FixedBytes<128> = ENCODING.to_fixed_bytes(&Person {
name: "Aristotle".to_string(),
age: 61,
})?;
let person: Person = ENCODING.from_slice(&data[..])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);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
Encoding.
§Examples
use musli::{Decode, Encode};
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let mut data = Vec::new();
ENCODING.to_writer(&mut data, &Person {
name: "Aristotle".to_string(),
age: 61,
})?;
let person: Person = ENCODING.from_slice(&data[..])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);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 Encoding.
§Examples
use musli::{Decode, Encode};
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let mut data = ENCODING.to_vec(&Person {
name: "Aristotle".to_string(),
age: 61,
})?;
// Add some extra data which will be ignored during decoding.
data.extend_from_slice(&[0xde, 0xad, 0xbe, 0xef]);
// Note: A slice implements `musli::Reader`.
let mut slice = &data[..];
let person: Person = ENCODING.decode(&mut slice)?;
assert_eq!(slice, &[0xde, 0xad, 0xbe, 0xef]);
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);Sourcepub fn from_slice<'de, T>(self, bytes: &'de [u8]) -> Result<T, Error>
pub fn from_slice<'de, T>(self, bytes: &'de [u8]) -> Result<T, Error>
Decode the given type T from the given slice using the current
Encoding.
§Examples
use musli::{Decode, Encode};
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let data = ENCODING.to_vec(&Person {
name: "Aristotle".to_string(),
age: 61,
})?;
let person: Person = ENCODING.from_slice(&data[..])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);Sourcepub fn from_str<'de, T>(self, string: &'de str) -> Result<T, Error>
pub fn from_str<'de, T>(self, string: &'de str) -> Result<T, Error>
Decode the given type T from the given string using the current
Encoding.
This is an alias over Encoding::from_slice for convenience. See
its documentation for more.
Sourcepub fn encode_with<C, W, T>(
self,
cx: C,
writer: W,
value: &T,
) -> Result<W::Ok, C::Error>
pub fn encode_with<C, W, T>( self, cx: C, writer: W, value: &T, ) -> Result<W::Ok, C::Error>
Encode the given value to the given Writer using the current
Encoding.
This is the same as Encoding::encode but allows for using a
configurable Context.
§Examples
use musli::{Decode, Encode};
use musli::alloc::System;
use musli::context;
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let cx = context::new().with_error();
let mut data = Vec::new();
ENCODING.encode_with(&cx, &mut data, &Person {
name: "Aristotle".to_string(),
age: 61,
})?;
let person: Person = ENCODING.from_slice_with(&cx, &data[..])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);Sourcepub fn to_slice_with<C, T>(
self,
cx: C,
out: &mut [u8],
value: &T,
) -> Result<usize, C::Error>
pub fn to_slice_with<C, T>( self, cx: C, out: &mut [u8], value: &T, ) -> Result<usize, C::Error>
Encode the given value to the given slice using the current
Encoding and return the number of bytes encoded.
This is the same as Encoding::to_slice but allows for using a
configurable Context.
§Examples
use musli::{Decode, Encode};
use musli::alloc::System;
use musli::context;
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let cx = context::new().with_error();
let mut data = Vec::new();
data.resize(128, 0);
let w = ENCODING.to_slice_with(&cx, &mut data[..], &Person {
name: "Aristotle".to_string(),
age: 61,
})?;
assert!(w > 0);
let person: Person = ENCODING.from_slice_with(&cx, &data[..w])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);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 Encoding.
This is the same as Encoding::to_vec, but allows for using a
configurable Context.
§Examples
use musli::{Decode, Encode};
use musli::alloc::System;
use musli::context;
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let cx = context::new().with_error();
let data = ENCODING.to_vec_with(&cx, &Person {
name: "Aristotle".to_string(),
age: 61,
})?;
let person: Person = ENCODING.from_slice_with(&cx, &data[..])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);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
Encoding.
§Examples
use musli::{Decode, Encode, FixedBytes};
use musli::alloc::System;
use musli::context;
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let cx = context::new().with_error();
let data: FixedBytes<128> = ENCODING.to_fixed_bytes_with(&cx, &Person {
name: "Aristotle".to_string(),
age: 61,
})?;
let person: Person = ENCODING.from_slice_with(&cx, &data[..])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);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
Encoding and context C.
§Examples
use musli::{Decode, Encode};
use musli::alloc::System;
use musli::context;
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let cx = context::new().with_error();
let mut data = Vec::new();
ENCODING.to_writer_with(&cx, &mut data, &Person {
name: "Aristotle".to_string(),
age: 61,
})?;
let person: Person = ENCODING.from_slice_with(&cx, &data[..])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);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 Encoding.
This is the same as Encoding::decode but allows for using a
configurable Context.
§Examples
use musli::{Decode, Encode};
use musli::alloc::System;
use musli::context;
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let cx = context::new().with_error();
let buf = ENCODING.to_vec_with(&cx, &Person {
name: "Aristotle".to_string(),
age: 61,
})?;
let mut slice = &buf[..];
let person: Person = ENCODING.decode_with(&cx, &mut slice)?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);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
Encoding.
This is the same as Encoding::from_slice, but allows for using a
configurable Context.
§Examples
use musli::{Decode, Encode};
use musli::alloc::System;
use musli::context;
use musli::packed::Encoding;
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let cx = context::new().with_error();
let buf = ENCODING.to_vec_with(&cx, &Person {
name: "Aristotle".to_string(),
age: 61,
})?;
let person: Person = ENCODING.from_slice_with(&cx, &buf[..])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);Sourcepub fn from_str_with<'de, C, T>(
self,
cx: C,
string: &'de str,
) -> Result<T, C::Error>
pub fn from_str_with<'de, C, T>( self, cx: C, string: &'de str, ) -> Result<T, C::Error>
Decode the given type T from the given string using the current
Encoding.
This is the same as Encoding::from_str but allows for using a
configurable Context.
This is an alias over Encoding::from_slice_with for convenience.
See its documentation for more.