Encoding

Struct Encoding 

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

Source

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,

Source

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();
Source

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();
Source

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

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);
Source

pub fn to_slice<T>(self, out: &mut [u8], value: &T) -> Result<usize, Error>
where T: ?Sized + Encode<M>,

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);
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 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);
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 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);
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 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);
Source

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

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);
Source

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

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);
Source

pub fn from_str<'de, T>(self, string: &'de str) -> Result<T, Error>
where T: Decode<'de, M, System>,

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.

Source

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

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);
Source

pub fn to_slice_with<C, T>( self, cx: C, out: &mut [u8], value: &T, ) -> Result<usize, C::Error>
where C: Context, T: ?Sized + Encode<M>,

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);
Source

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

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);
Source

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

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);
Source

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

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);
Source

pub fn decode_with<'de, C, R, T>(self, cx: C, reader: R) -> Result<T, C::Error>
where C: Context, R: IntoReader<'de>, T: Decode<'de, M, C::Allocator>,

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);
Source

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

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);
Source

pub fn from_str_with<'de, C, T>( self, cx: C, string: &'de str, ) -> Result<T, C::Error>
where C: Context, T: Decode<'de, M, C::Allocator>,

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.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

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 Default for Encoding<OPTIONS, Binary>

Source§

fn default() -> Self

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

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<const OPT: u32, 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> 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> 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.