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>
impl Encoding<OPTIONS, Binary>
Sourcepub const fn new() -> Self
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,
impl<const OPT: Options, M> Encoding<OPT, M>where
M: 'static,
Sourcepub const fn with_mode<T>(self) -> Encoding<OPT, T>where
T: 'static,
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();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::value::Encoding;
const OPTIONS: Options = options::new().integer(Integer::Fixed).build();
const ENCODING: Encoding<OPTIONS> = Encoding::new().with_options();Sourcepub fn encode(&self, value: impl Encode<M>) -> Result<Value<Global>, Error>
Available on crate feature alloc only.
pub fn encode(&self, value: impl Encode<M>) -> Result<Value<Global>, Error>
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);Sourcepub fn encode_with<C>(
&self,
cx: C,
value: impl Encode<M>,
) -> Result<Value<C::Allocator>, C::Error>where
C: Context,
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);Sourcepub fn decode<'de, T>(
&self,
value: &'de Value<impl Allocator>,
) -> Result<T, Error>
Available on crate feature alloc only.
pub fn decode<'de, T>( &self, value: &'de Value<impl Allocator>, ) -> Result<T, Error>
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);Sourcepub fn decode_with<'de, C, T>(
&self,
cx: C,
value: &'de Value<impl Allocator>,
) -> Result<T, C::Error>
pub fn decode_with<'de, C, T>( &self, cx: C, value: &'de Value<impl Allocator>, ) -> Result<T, C::Error>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more