Skip to main content

FromAllocator

Trait FromAllocator 

Source
pub trait FromAllocator<A> {
    // Required method
    fn from_allocator(alloc: A) -> Self;
}
Expand description

An allocator which can be obtained from the allocator A.

Every Allocator can be obtained from itself, and the Global allocator can be obtained from any allocator since it carries no state.

This is used when decoding a container which is tied to a particular allocator. A decoder hands out its own allocator A, which the container converts into the allocator it stores its data in. This is what allows a field of type String<Global>, Vec<T, Global> or Value<Global> to be decoded by a decoder using any allocator.

This is implemented for every allocator provided by Müsli, and only Müsli can implement Allocator, so it does not need to be implemented manually.

§Examples

use musli::alloc::{Allocator, FromAllocator, Global, Vec};

fn copy_to<A, B>(alloc: A, bytes: &[u8]) -> Vec<u8, B>
where
    A: Allocator,
    B: Allocator + FromAllocator<A>,
{
    let mut out = Vec::new_in(B::from_allocator(alloc));
    out.extend_from_slice(bytes).unwrap();
    out
}

musli::alloc::default(|alloc| {
    let bytes: Vec<u8, Global> = copy_to(alloc, b"hello");
    assert_eq!(bytes.as_slice(), b"hello");
});

Required Methods§

Source

fn from_allocator(alloc: A) -> Self

Obtain this allocator from the allocator alloc.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<A> FromAllocator<A> for Global

The Global allocator carries no state, so it can be obtained from any allocator.