Skip to main content

luaur_ast/methods/
allocator_alloc.rs

1use crate::records::allocator::Allocator;
2
3impl Allocator {
4    #[allow(non_snake_case)]
5    pub fn alloc<T>(&mut self, value: T) -> *mut T {
6        // The C++ implementation uses a static_assert to ensure T is trivially destructible
7        // because the allocator never calls destructors. In Rust, we don't have an exact
8        // equivalent to std::is_trivially_destructible as a stable trait bound, but
9        // the contract remains: the caller must be aware that the memory is managed
10        // by the Allocator and won't be dropped automatically.
11
12        let ptr = self.allocate(core::mem::size_of::<T>()) as *mut T;
13        if !ptr.is_null() {
14            unsafe {
15                core::ptr::write(ptr, value);
16            }
17        }
18        ptr
19    }
20}