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
Right now we cannot implement default trait methods that std::mem::transmute from Self into something else since the compiler doesn't know Selfs size, and thus std::mem::transmute's requirements cannot be verified:
traitFoo:Sized{fnto_i32(self) -> i32{unsafe{// lot's of types don't fulfill the requirements here:
std::mem::transmute::<Self,i32>(self)}}}implFooforu32{}// sad face :(
Possible solution
A cleaner way of enforcing std::mem::transmute requirements would be to enforce them with a trait:
unsafetraitTransmutable<U:Sized>:Sized{// warning magic inside!}unsafeimplTransmutable<U:Sized>forT{}// more magicunsafefntransmute<T:Transmutable<U>,U>(e:T) -> U
That is, instead of doing "compiler magic" at the fn transmute level, this magic could be lifted into a trait that constraints types on being Transmutable to each other.
That would allow the following code to type check:
// requires Self to be transmutable to i32:traitFoo:Transmutable<i32>{fnto_i32(self) -> i32{unsafe{// the compilers knows that Self can be transmuted:
std::mem::transmute::<Self,i32>(self)// type-checks yay!}}}implFooforu32{}// impl Foo for u64 {} // error: u64 is not `Transmutable<i32>`, yay!
Interaction with future language features
With type-level integers we will definitely want something analogous to a HasSize<u32> trait, and we will want to be able to std::mem::transmute between types of the same size. The Transmutable trait will still be useful, and can be implemented on top of HasSize if std::mem::size_of becomes a const fn. That is, this interaction between Transmutable and type-level integers can be solved in a backwards compatible way.
Bikeshedding
Instead of Transmutable the trait could also be called SameSize since that is exactly the constraint it imposes.
Implementability
No idea, haven't tried.
The text was updated successfully, but these errors were encountered:
nrc
added
the
T-libs-api
Relevant to the library API team, which will review and decide on the RFC.
label
Aug 19, 2016
Problem
Right now we cannot implement default trait methods that
std::mem::transmute
fromSelf
into something else since the compiler doesn't knowSelf
s size, and thusstd::mem::transmute
's requirements cannot be verified:Possible solution
A cleaner way of enforcing
std::mem::transmute
requirements would be to enforce them with atrait
:That is, instead of doing "compiler magic" at the
fn transmute
level, this magic could be lifted into atrait
that constraints types on beingTransmutable
to each other.That would allow the following code to type check:
Interaction with future language features
With type-level integers we will definitely want something analogous to a
HasSize<u32>
trait, and we will want to be able tostd::mem::transmute
between types of the same size. TheTransmutable
trait will still be useful, and can be implemented on top ofHasSize
ifstd::mem::size_of
becomes aconst fn
. That is, this interaction betweenTransmutable
and type-level integers can be solved in a backwards compatible way.Bikeshedding
Instead of
Transmutable
the trait could also be calledSameSize
since that is exactly the constraint it imposes.Implementability
No idea, haven't tried.
The text was updated successfully, but these errors were encountered: