Kimundi / owning-ref-rs

A library for creating references that carry their owner with them.
MIT License
362 stars 50 forks source link

Erasing the Box itself #8

Closed diwic closed 7 years ago

diwic commented 8 years ago

This might be more of a question than an issue, but...

Suppose I'm writing a function from which I want to return a Vec<OwningRef<_, &[u8]>>. Some of these slices are Box<[u8]>, others are Rc<[u8]>, and some are perhaps Ref<[u8]>. So when the caller of the function drops that Vec<OwningRef<_, &[u8]>>, the Box<[u8]>s will be deallocated, the Rc<[u8]>s will have their refcount decreased, and the Ref<[u8]>s will have their read count decreased.

Is this possible using the Erased functionality? It seems like erasing a OwningRef<Box<[u8]>, [u8]> will give you a OwningRef<Box<Erased>, [u8]>, where I'm actually is looking for a OwningRef<Erased, [u8]> so I can abstract over several types of smart pointers.

Kimundi commented 7 years ago

Directly erasing the owner like this is not really doable with the memory layout of OwningRef, and looking at that ARef PR handling this by completely transmuting away the underlying type seems like a too error prone solution.

However, it should be possible to just add another layer of indirection, by creating a OwningRef of Box<Box<[u8]>>, Box<Rc<[u8]>>, Box<Ref<[u8]>>, etc. Those can all be erased to the common Box<Erased>.

Kimundi commented 7 years ago

I added some new methods for mapping the owner like this.

diwic commented 7 years ago

Oh, since I didn't get a reply in a timely fashion, I went ahead and created my own crate instead. Here's my ARef.