Expand description
Allocation support for Müsli.
This crate contains two types of allocators:
- The
Systemallocator, which uses the system allocation facilities. Particularlystd::alloc::System. - The
Sliceallocator, which can allocate buffers from a fixed-size slice.
The following types are also provided for convenience:
Vecwhich can be used as a vector of allocations.Stringwhich can be used as a safe string container.
§Examples
use musli::alloc::{AllocError, Vec};
musli::alloc::default(|alloc| {
let mut a = Vec::new_in(alloc);
let mut b = Vec::new_in(alloc);
b.extend_from_slice(b"He11o")?;
a.extend_from_slice(b.as_slice())?;
assert_eq!(a.as_slice(), b"He11o");
assert_eq!(a.len(), 5);
a.extend_from_slice(b" W0rld")?;
assert_eq!(a.as_slice(), b"He11o W0rld");
assert_eq!(a.len(), 11);
let mut c = Vec::new_in(alloc);
c.extend_from_slice(b"!")?;
a.extend_from_slice(c.as_slice())?;
assert_eq!(a.as_slice(), b"He11o W0rld!");
assert_eq!(a.len(), 12);
Ok::<_, AllocError>(())
});Explicitly using a buffer on the stack with the Slice allocator:
use musli::alloc::{ArrayBuffer, Slice, Vec};
let mut buf = ArrayBuffer::new();
let alloc = Slice::new(&mut buf);
let mut a = Vec::new_in(&alloc);
let mut b = Vec::new_in(&alloc);
b.extend_from_slice(b"He11o")?;
a.extend_from_slice(b.as_slice())?;
assert_eq!(a.as_slice(), b"He11o");
assert_eq!(a.len(), 5);
a.extend_from_slice(b" W0rld")?;
assert_eq!(a.as_slice(), b"He11o W0rld");
assert_eq!(a.len(), 11);
let mut c = Vec::new_in(&alloc);
c.extend_from_slice(b"!")?;
a.extend_from_slice(c.as_slice())?;
assert_eq!(a.as_slice(), b"He11o W0rld!");
assert_eq!(a.len(), 12);Structs§
- Alloc
Error - An allocation error.
- Array
Buffer - An array that can conveniently be used as a buffer, by default this is
DEFAULT_ARRAY_BUFFERbytes large. - Box
- A Müsli-allocated pointer type that uniquely owns a heap allocation of type
T. - Default
Allocator - The default allocator implementation.
- Disabled
- An allocator which cannot allocate anything.
- Slice
- A no-std compatible slice-based allocator that can be used with the
muslicrate. - Slice
Alloc - A slice allocated buffer.
- String
- A Müsli-allocated UTF-8–encoded, growable string.
- System
- System buffer that can be used in combination with an
Allocator. - System
Alloc - A vector-backed allocation.
- Vec
- A Müsli-allocated contiguous growable array type, written as
Vec<T>, short for ‘vector’.
Constants§
- DEFAULT_
ARRAY_ BUFFER - The default stack buffer size for the default allocator provided through
default().
Traits§
- Alloc
- A value allocated through
Allocator::alloc. - Allocator
- An allocator that can be used in combination with a context.
- Slice
Buffer - The trait over anything that can be treated as a buffer by the
Sliceallocator. - ToOwned
- The local `ToOwned`` implementation for Musli’s allocation system.
Functions§
- default
- Call the given block
bodywith an instance of theDefaultAllocator. - with_
buffer - Same as
default()but allows for specifying a default static buffer size other thanDEFAULT_ARRAY_BUFFER.