rust-lang / wg-allocators

Home of the Allocators working group: Paving a path for a standard set of allocator traits to be used in collections!
http://bit.ly/hello-wg-allocators
205 stars 9 forks source link

Implementing `AllocRef` for actual references #34

Open oliver-giersch opened 4 years ago

oliver-giersch commented 4 years ago

From what I can gather from the currently proposed API design, the way the associated types are set up between the traits BuildAllocRef, DeallocRef and AllocRef it would be impossible to implement the latter two for an actual reference as in e.g. impl<'a> AllocRef for &'a CustomAllocator. If I am not mistaken, this means that &'a CustomAllocator must also implement DeallocRef and needs an associated builder type. However, without GAT it is not possible to implement BuildAllocRef for CustomAllocator and define &'a CustomAllocator as the associated Ref type.

Is my understanding of the intended usage for these traits correct or am I wrong somehow? Furthermore, even if GAT were available and BuildAllocRef were defined as:

pub trait BuildAllocRef {
    type Ref<'a>: DeallocRef + 'a;

    unsafe fn build_alloc_ref<'a>(
        &'a mut self,
        ptr: NonNull<u8>,
        layout: Option<NonZeroLayout>
    ) -> Self::Ref<'a>;
}

This would result in DeallocRef/AllocRef being impossible to implement for any owning handles (like Arc) or ZST handles, since built Ref would be required to be bound to the lifetime of the builder, at least if I understand the proposed GAT RFC correctly.