-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DispatchFromDyn is not implemented for Box, Rc, Arc, or Weak with custom allocators #135
Comments
Upon further inspection, it seems like Box has some hints to this:
I guess then my question is, why does the allocator (specifically the location of the allocated memory) matter to a vtable? Isn't memory just memory? |
I think because the compiler assumes that box behaves like a primitive pointer and so it can just turn it from a thin into a fat pointer. This this isn't guranteed if the allocator is a non-ZST. Or at least it expects that the pointer is at offset zero.
I recently special-cased the |
Wouldn't this mean that the GlobalAlloc trait could also mess up this assumption? So really, the compiler should be enforcing that the box is using the global allocator and that the global allocator hasn't been overridden? |
The
DispatchFromDyn
trait allows for a type to "beself
" such that a trait can become object safe. For example, sinceRc<T>
isDispatchFromDyn
, you can write code likeand have a
Box<dyn Foo>
be valid. This trait is defined in Rc like:However, this implementation does not apply for custom allocators (e.g.
Rc<T, A>
). This is the same in Arc and in both Rc and Arc's Weak, too. This means that replacing the above trait function withwill not compile if you have a
Box<dyn Foo>
.Is this an oversight or is this intentional?
The text was updated successfully, but these errors were encountered: