Skip to main content

Encoding

Struct Encoding 

Source
pub struct Encoding<const OPT: Options = OPTIONS, M = Binary>
where M: 'static,
{ /* private fields */ }
Available on crate feature value only.
Expand description

Setting up encoding with parameters.

Implementations§

Source§

impl Encoding<OPTIONS, Binary>

Source

pub const fn new() -> Self

Construct a new Encoding.

use musli::{Encode, Decode};
use musli::value::Encoding;

const ENCODING: Encoding = Encoding::new();

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

let person = Person { name: "Alice", age: 30 };
let value = ENCODING.encode(&person)?;

let decoded: Person<'_> = ENCODING.decode(&value)?;
assert_eq!(decoded, person);
Source§

impl<const OPT: Options, M> Encoding<OPT, M>
where M: 'static,

Source

pub const fn with_mode<T>(self) -> Encoding<OPT, T>
where T: 'static,

Change the mode of the encoding.

§Examples
use musli::value::{OPTIONS, Encoding};

enum Custom {}

const ENCODING: 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::value::Encoding;

const OPTIONS: Options = options::new().integer(Integer::Fixed).build();
const ENCODING: Encoding<OPTIONS> = Encoding::new().with_options();
Source

pub fn encode(&self, value: impl Encode<M>) -> Result<Value<Global>, Error>

Available on crate feature alloc only.

Encode something that implements Encode into a Value.

§Examples
use musli::{Encode, Decode};
use musli::value;

const ENCODING: value::Encoding = value::Encoding::new();

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

let person = Person { name: "Alice", age: 30 };
let value = ENCODING.encode(&person)?;

let decoded: Person<'_> = ENCODING.decode(&value)?;
assert_eq!(decoded, person);
Source

pub fn encode_with<C>( &self, cx: C, value: impl Encode<M>, ) -> Result<Value<C::Allocator>, C::Error>
where C: Context,

Encode something that implements Encode into a Value using a custom Context C.

A custom context allows Value encoding to be performed without an allocator.

§Examples
use musli::{Encode, Decode};
use musli::context;
use musli::value;

const ENCODING: value::Encoding = value::Encoding::new();

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

let person = Person { name: "Alice", age: 30 };

let cx = context::new();
let value = ENCODING.encode_with(&cx, &person)?;

let decoded: Person<'_> = ENCODING.decode(&value)?;
assert_eq!(decoded, person);
Source

pub fn decode<'de, T>( &self, value: &'de Value<impl Allocator>, ) -> Result<T, Error>
where T: Decode<'de, M, Global>,

Available on crate feature alloc only.

Decode a Value into a type which implements Decode.

§Examples
use musli::{Encode, Decode};
use musli::value;

const ENCODING: value::Encoding = value::Encoding::new();

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

let person = Person { name: "Alice", age: 30 };
let encoded = ENCODING.encode(&person)?;

let decoded: Person<'_> = ENCODING.decode(&encoded)?;
assert_eq!(decoded, person);
Source

pub fn decode_with<'de, C, T>( &self, cx: C, value: &'de Value<impl Allocator>, ) -> Result<T, C::Error>
where C: Context, T: Decode<'de, M, C::Allocator>,

Decode a Value into a type which implements Decode using a custom Context C.

A custom context allows Value decoding to be performed without an allocator.

§Examples
use musli::{Encode, Decode};
use musli::context;
use musli::value;

const ENCODING: value::Encoding = value::Encoding::new();

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

let person = Person { name: "Alice", age: 30 };

let value = ENCODING.encode(&person)?;

let cx = context::new();
let decoded: Person<'_> = ENCODING.decode_with(&cx, &value)?;
assert_eq!(decoded, person);

Trait Implementations§

Source§

impl Default for Encoding<OPTIONS, Binary>

Source§

fn default() -> Self

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

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> UnsafeUnpin for Encoding<OPT, M>

§

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