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
207 stars 9 forks source link

Allow DispatchFromDyn to be used in generic contexts #6

Open TimDiekmann opened 5 years ago

TimDiekmann commented 5 years ago

While ZST fields are now supported in DispatchFromDyn, it still cannot be used in a generic context as typeck does not know anything about the parameter. I.e. this is not allowed as A is unknown:

impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Alloc> DispatchFromDyn<Box<U, A>> for Box<T, A> {}

This does not block the implementation for Box<T, A>, as we only need DispatchFromDyn<Box<U, Global>> for Box<T, Global> to maintain semver compatibility.

How can this be solved?

One way I can think of is a trait similar to the Unsize trait:

#[unstable(feature = "zero_sized", issue = "0")]
#[lang = "zero_sized"]
pub trait ZeroSized {
    // Empty.
}

which is implemented for all ZSTs. typeck could then check for this trait to be implemented and the above code becomes

impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Alloc + ZeroSized> DispatchFromDyn<Box<U, A>> for Box<T, A> {}
glandium commented 5 years ago

It feels like the right thing to do would be to file an issue against rust to allow any type rather than ZSTs only. ZSTs was the easy way out, that doesn't require much thought, but eventually, non-ZSTs should be supported.

TimDiekmann commented 5 years ago

There was an discussion on Zulip recently on this topic.

TimDiekmann commented 4 years ago

ZSTs was the easy way out, that doesn't require much thought, but eventually, non-ZSTs should be supported.

Are there any downsides of supporting non-ZSTs?