new_in

Function new_in 

Source
pub fn new_in<A>(alloc: A) -> DefaultContext<A, NoTrace, Ignore>
where A: Allocator,
Expand description

Construct a new default context using the provided allocator.

ยงExamples

The default macro provides access to the default allocator. This is how it can be used with this method:

use musli::context;

musli::alloc::default(|alloc| {
    let cx = context::new_in(alloc);
    let encoding = musli::json::Encoding::new();
    let string = encoding.to_string_with(&cx, &42)?;
    assert_eq!(string, "42");
    Ok(())
})?;

We can also very conveniently set up an allocator which uses an existing buffer:

use musli::{alloc, context};

let mut buf = alloc::ArrayBuffer::new();
let alloc = alloc::Slice::new(&mut buf);
let cx = context::new_in(&alloc);

let encoding = musli::json::Encoding::new();
let string = encoding.to_string_with(&cx, &42)?;
assert_eq!(string, "42");