zallocators 0.1.0

Bringing Zig like allocators to Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use std::ptr::NonNull;

pub struct FixedBufferAllocator<'a> {
    buf: &'a mut [u8],
}

impl<'a> FixedBufferAllocator<'a> {
    pub fn new(buf: &'a mut [u8]) -> Self {
        Self { buf }
    }

    pub fn allocate(&mut self, size: usize) -> NonNull<[u8]> {
        let ptr = NonNull::new(self.buf[0..size].as_mut_ptr()).unwrap();

        NonNull::slice_from_raw_parts(ptr, size)
    }
}