Module alloc

Module alloc 

Source
Expand description

Allocation support for Müsli.

This crate contains two types of allocators:

  • The System allocator, which uses the system allocation facilities. Particularly std::alloc::System.
  • The Slice allocator, which can allocate buffers from a fixed-size slice.

The following types are also provided for convenience:

  • Vec which can be used as a vector of allocations.
  • String which 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§

AllocError
An allocation error.
ArrayBuffer
An array that can conveniently be used as a buffer, by default this is DEFAULT_ARRAY_BUFFER bytes large.
Box
A Müsli-allocated pointer type that uniquely owns a heap allocation of type T.
DefaultAllocator
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 musli crate.
SliceAlloc
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.
SystemAlloc
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.
SliceBuffer
The trait over anything that can be treated as a buffer by the Slice allocator.
ToOwned
The local `ToOwned`` implementation for Musli’s allocation system.

Functions§

default
Call the given block body with an instance of the DefaultAllocator.
with_buffer
Same as default() but allows for specifying a default static buffer size other than DEFAULT_ARRAY_BUFFER.