You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fncast<T: ?Sized,U: ?Sized>(t:*mutT) -> *mutU{
t as*mutU}
Rust isn't able to guarantee that T and U have the same "vtable kinds", and thus isn't able to prove that *mut T and *mut U have compatible pointer metadata. We currently work around this using this method on KnownLayout:
This has a limitation: It can't be called in a const context.
Instead, we could make casting unsafe, requiring the caller to promise to ensure that the pointers are either both thin or both fat, and use a union-transmute under the hood to avoid the vtable problem:
/// # Safety////// The caller must ensure that `Src` and `Dst` must either both be `Sized` or/// both be unsized.constunsafefncast_unchecked<Src: ?Sized,Dst: ?Sized>(src:*mutSrc) -> *mutDst{#[repr(C)]unionTransmute<Src:Copy,Dst:Copy>{src:Src,dst:Dst,}unsafe{Transmute{ src }.dst}}
The behavior of this union-transmute is almost well-defined, but not quite. The Reference guarantees the behavior of raw pointer casts, but makes no guarantee that the equivalent transmute has the same behavior as a cast. I've put up a PR to guarantee that this is well-defined: rust-lang/reference#1661
The text was updated successfully, but these errors were encountered:
The following is not valid:
Rust isn't able to guarantee that
T
andU
have the same "vtable kinds", and thus isn't able to prove that*mut T
and*mut U
have compatible pointer metadata. We currently work around this using this method onKnownLayout
:zerocopy/src/lib.rs
Line 731 in 0fae530
This has a limitation: It can't be called in a
const
context.Instead, we could make casting unsafe, requiring the caller to promise to ensure that the pointers are either both thin or both fat, and use a union-transmute under the hood to avoid the vtable problem:
The behavior of this union-transmute is almost well-defined, but not quite. The Reference guarantees the behavior of raw pointer casts, but makes no guarantee that the equivalent transmute has the same behavior as a cast. I've put up a PR to guarantee that this is well-defined: rust-lang/reference#1661
The text was updated successfully, but these errors were encountered: