Struct Encoding

Source
pub struct Encoding<M = Text> { /* private fields */ }
Expand description

Setting up encoding with parameters.

Implementations§

Source§

impl Encoding<Text>

Source

pub const fn new() -> Self

Construct a new Encoding.

You can modify this using the available factory methods:

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

const CONFIG: Encoding<Json> = Encoding::new().with_mode();

// Mode marker indicating that some attributes should
// only apply when we're decoding in a JSON mode.
enum Json {}

#[derive(Debug, PartialEq, Encode, Decode)]
#[musli(mode = Json, name_all = "name")]
struct Struct<'a> {
    name: &'a str,
    age: u32,
}

let expected = Struct {
    name: "Aristotle",
    age: 61,
};

let out = CONFIG.to_vec(&expected).unwrap();
println!("{}", core::str::from_utf8(out.as_slice()).unwrap());

let out = musli_json::to_vec(&expected).unwrap();
println!("{}", core::str::from_utf8(out.as_slice()).unwrap());
let actual = musli_json::from_slice(out.as_slice()).unwrap();
assert_eq!(expected, actual);
Source§

impl<M> Encoding<M>

Source

pub const fn with_mode<T>(self) -> Encoding<T>

Change the mode of the encoding.

§Examples
use musli_json::Encoding;

enum Custom {}

const CONFIG: Encoding<Custom> = Encoding::new().with_mode();
Source

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

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.

Source

pub fn to_string<T>(self, value: &T) -> Result<String, Error>
where T: ?Sized + Encode<M>,

Encode the given value to a String using the current configuration.

Source

pub fn to_string_with<T, C>(self, cx: &C, value: &T) -> Result<String, C::Error>
where C: ?Sized + Context<Mode = M>, T: ?Sized + Encode<M>,

Encode the given value to a String using the current configuration.

This is the same as Encoding::to_string but allows for using a configurable Context.

Source

pub fn decode<'de, P, T>(self, parser: P) -> Result<T, Error>
where P: Parser<'de>, T: Decode<'de, M>,

Decode the given type T from the given Parser using the current configuration.

Source

pub fn decode_with<'de, C, P, T>(self, cx: &C, parser: P) -> Result<T, C::Error>
where C: ?Sized + Context<Mode = M>, P: Parser<'de>, T: Decode<'de, M>,

Decode the given type T from the given Parser using the current configuration.

This is the same as Encoding::decode but allows for using a configurable Context.

Source

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

Decode the given type T from the given string using the current configuration.

Source

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

Decode the given type T from the given string using the current configuration.

This is the same as Encoding::from_str but allows for using a configurable Context.

Source

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.

Source

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

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.

Source

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

Encode the given value to the given Writer using the current configuration.

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

Source

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

Encode the given value to the given Write using the current configuration and context C.

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

Source

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

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.

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

Source

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

Encode the given value to a fixed-size bytes using the current configuration.

Trait Implementations§

Source§

impl<M> Clone for Encoding<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<M> Copy for Encoding<M>

Auto Trait Implementations§

§

impl<M> Freeze for Encoding<M>

§

impl<M> RefUnwindSafe for Encoding<M>
where M: RefUnwindSafe,

§

impl<M> Send for Encoding<M>
where M: Send,

§

impl<M> Sync for Encoding<M>
where M: Sync,

§

impl<M> Unpin for Encoding<M>
where M: Unpin,

§

impl<M> UnwindSafe for Encoding<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.