Alloc

Trait Alloc 

Source
pub trait Alloc<T>: Sized {
    // Required methods
    fn as_ptr(&self) -> *const T;
    fn as_mut_ptr(&mut self) -> *mut T;
    fn capacity(&self) -> usize;
    fn resize(
        &mut self,
        len: usize,
        additional: usize,
    ) -> Result<(), AllocError>;
    fn try_merge<B>(
        &mut self,
        this_len: usize,
        other: B,
        other_len: usize,
    ) -> Result<(), B>
       where B: Alloc<T>;
}
Expand description

A value allocated through Allocator::alloc.

§Examples

use musli::alloc::{AllocError, Allocator, Alloc};

musli::alloc::default(|alloc| {
    let mut buf = alloc.alloc(10u32)?;

    unsafe {
        buf.as_mut_ptr().write(20u32);
        assert_eq!(buf.as_ptr().read(), 20u32);
    }

    Ok::<_, AllocError>(())
});

Example of a correctly managed slice allocation:

use core::slice;
use musli::alloc::{Alloc, AllocError, Allocator};

let values: [u32; 4] = [1, 2, 3, 4];

musli::alloc::default(|alloc| {
    let mut buf = alloc.alloc_empty::<u32>();
    let mut len = 0;

    for value in values {
        buf.resize(len, 1)?;

        // SAFETY: We've just resized the above buffer.
        unsafe {
            buf.as_mut_ptr().add(len).write(value);
        }

        len += 1;
    }

    // SAFETY: Slice does not outlive the buffer it references.
    let bytes = unsafe { slice::from_raw_parts(buf.as_ptr(), len) };
    assert_eq!(bytes, values);
    Ok::<_, AllocError>(())
});

Required Methods§

Source

fn as_ptr(&self) -> *const T

Get a pointer into the allocation.

§Examples
use musli::alloc::{AllocError, Allocator, Alloc};

musli::alloc::default(|alloc| {
    let mut buf = alloc.alloc(10u32)?;

    unsafe {
        buf.as_mut_ptr().write(20u32);
        assert_eq!(buf.as_ptr().read(), 20u32);
    }

    Ok::<_, AllocError>(())
});
Source

fn as_mut_ptr(&mut self) -> *mut T

Get a mutable pointer into the allocation.

§Examples
use musli::alloc::{AllocError, Allocator, Alloc};

musli::alloc::default(|alloc| {
    let mut buf = alloc.alloc(10u32)?;

    unsafe {
        buf.as_mut_ptr().write(20u32);
        assert_eq!(buf.as_ptr().read(), 20u32);
    }

    Ok::<_, AllocError>(())
});
Source

fn capacity(&self) -> usize

Returns the capacity of the buffer.

Source

fn resize(&mut self, len: usize, additional: usize) -> Result<(), AllocError>

Resize the buffer to fit at least additional elements.

Returns the new capacity of the buffer.

Source

fn try_merge<B>( &mut self, this_len: usize, other: B, other_len: usize, ) -> Result<(), B>
where B: Alloc<T>,

Try to merge one buffer with another.

The two length parameters refers to the initialized length of the two buffers.

If this returns Err(B) if merging was not possible.

Returns the capacity of the newly merged buffer.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> Alloc<T> for SystemAlloc<T>