#![allow(dead_code, deprecated, unused_variables, unused_mut)] #![feature(align_to_uninit_mut)] use std::mem::MaybeUninit; pub struct BumpAllocator<'scope> { memory: &'scope mut [MaybeUninit], } impl<'scope> BumpAllocator<'scope> { pub fn new(memory: &'scope mut [MaybeUninit]) -> Self { Self { memory } } pub fn try_alloc_uninit(&mut self) -> Option<&'scope mut MaybeUninit> { let first_end = self.memory.as_ptr().align_offset(align_of::()) + size_of::(); let prefix = self.memory.split_off_mut(..first_end)?; Some(&mut prefix.align_to_uninit_mut::().1[0]) } pub fn try_alloc_u32(&mut self, value: u32) -> Option<&'scope mut u32> { let uninit = self.try_alloc_uninit()?; Some(uninit.write(value)) } } fn main() { let mut memory = [MaybeUninit::::uninit(); 10]; let mut allocator = BumpAllocator::new(&mut memory); let v = allocator.try_alloc_u32(42); assert_eq!(v, Some(& mut 42)); }