Move "same size" requirement of transmute into a trait
Nobody has claimed this yet.
- Dominant language
- Markdown
- Stars
- 6.6k
- Forks
- 1.7k
- Avg merge
- 16h 14m
- Merged PRs (30d)
- 1
Description
Problem
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:
trait Foo : Sized {
fn to_i32(self) -> i32 {
unsafe {
// lot's of types don't fulfill the requirements here:
std::mem::transmute::<Self, i32>(self)
}
}
}
impl Foo for u32 {} // sad face :(
Possible solution
A cleaner way of enforcing std::mem::transmute requirements would be to enforce them with a trait:
unsafe trait Transmutable<U: Sized> : Sized {
// warning magic inside!
}
unsafe impl Transmutable<U: Sized> for T { } // more magic
unsafe fn transmute<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:
trait Foo : Transmutable<i32> {
fn to_i32(self) -> i32 {
unsafe {
// the compilers knows that Self can be transmuted:
std::mem::transmute::<Self, i32>(self) // type-checks yay!
}
}
}
impl Foo for u32 {}
// 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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue proposes moving transmute's same-size requirement into a Transmutable or SameSize trait, but names no files, tests, or compiler entry points. Start by determining whether the proposed trait-based enforcement is implementable and how it would interact with default trait methods and future type-level integers; done requires a concrete, accepted design.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100