Kimundi / owning-ref-rs

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

ErasedSend*Ref? #24

Open scottlamb opened 7 years ago

scottlamb commented 7 years ago

ErasedBoxRef and ErasedArcRef aren't Send. Would it be possible to have very similar types which are? I guess with a method similar to erase_owner to create them?

Context: I proposed using these types to back hyper's http::Chunk, so that I can do such things as subsetting vectors as I put them into a Chunk or pass in mmap-backed chunks. But Chunk must be passable between threads, so this doesn't compile.

@seanmonstar doesn't seem too thrilled with my proposal, so this probably won't happen regardless, but I imagine there are other situations where erased types are desirable but so is passing between threads. In fact, ErasedArcRef seems basically useless as-is; Send is the point of using Arc rather than Rc.

Kimundi commented 7 years ago

Hi, I'm sincerely sorry for taking so long to respond.

Yeah, the marker traits are always tricky to support properly. Really makes me wish we could be generic over traits and trait bounds, as you could then write something like this:

unsafe trait IntoErased<'a, Trait: ?Sized> {
    type Erased;
    fn into_erased(self) -> Self::Erased;
}
unsafe impl<'a, T: 'a, Trait: ?Sized, T: Trait> IntoErased<'a, Trait> for Box<T> {
    type Erased = Box<Trait + 'a>;
    fn into_erased(self) -> Self::Erased {
        self
    }
}

let a = owning_ref.erase_owner<Erased>();
let b = owning_ref.erase_owner<ErasedSend>();
// ...

But I think it should still be possible to support somewhat conveniently by refactoring the typedefs a bit

scottlamb commented 7 years ago

Hi, I'm sincerely sorry for taking so long to respond.

No worries—I don't have a leg to stand on complaining about such things.

If you're looking for a co-maintainer for owning-ref, I wonder if @diwic would be interested? I found diwic's similar reffers crate shortly after filing this issue. For the hyper body chunk use case I described, reffers::ARefs<[u8]> worked out quite well—functionally similar to having an enum of the various owner types using owning_ref.