Skip to content

Implement AllocRef for references to A: AllocRef #53

Closed
rust-lang/rust
#71442
@lachlansneff

Description

@lachlansneff

For usability purposes, I'm proposing that the following implementation be added:

unsafe impl<A> AllocRef for &'_ mut A where A: AllocRef {
    fn alloc(&mut self, layout: Layout, init: AllocInit) -> Result<MemoryBlock, AllocErr> {
        A::alloc(self, layout, init)
    }

    unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
        A::dealloc(self, ptr, layout)
    }

    unsafe fn grow(&mut self, ptr: NonNull<u8>, layout: Layout, new_size: usize, placement: ReallocPlacement, init: AllocInit) -> Result<MemoryBlock, AllocErr> {
        A::grow(self,ptr, layout, new_size, placement, init)
    }

    unsafe fn shrink(&mut self, ptr: NonNull<u8>, layout: Layout, new_size: usize, placement: ReallocPlacement) -> Result<MemoryBlock, AllocErr> {
        A::shrink(self, ptr, layout, new_size, placement)
    }
}

Without this implementation, I believe it's impossible to implement composable allocators, as I have started doing in this gist: https://gist.github.com/lachlansneff/8f59278bccb82ec5b36c359f385b7e3f.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions