pub fn with<F, O>(f: F) -> OExpand description
Call the given closure with the default allocator.
This is useful if you want to write application which are agnostic to
whether the alloc feature is or isn’t enabled.
- If the
allocfeature is enabled, this is theSystemallocator. - If the
allocfeature is disabled, this is theStackallocator withDEFAULT_STACK_BUFFERbytes allocated on the stack.
§Examples
use musli::{Allocator, Buf};
musli_allocator::with(|alloc| {
let mut a = alloc.alloc().expect("allocation a failed");
let mut b = alloc.alloc().expect("allocation b failed");
b.write(b"He11o");
a.write(b.as_slice());
assert_eq!(a.as_slice(), b"He11o");
assert_eq!(a.len(), 5);
a.write(b" W0rld");
assert_eq!(a.as_slice(), b"He11o W0rld");
assert_eq!(a.len(), 11);
let mut c = alloc.alloc().expect("allocation c failed");
c.write(b"!");
a.write(c.as_slice());
assert_eq!(a.as_slice(), b"He11o W0rld!");
assert_eq!(a.len(), 12);
});