1ad218f3af
As our implementation of MCP411 nears completion and we begin to solicit testing, it's no longer reasonable to expect testers to type or remember `BikeshedIntrinsicFrom`. The name degrades the ease-of-reading of documentation, and the overall experience of using compiler safe transmute. Tentatively, we'll instead adopt `TransmuteFrom`. This name seems to be the one most likely to be stabilized, after discussion on Zulip [1]. We may want to revisit the ordering of `Src` and `Dst` before stabilization, at which point we'd likely consider `TransmuteInto` or `Transmute`. [1] https://rust-lang.zulipchat.com/#narrow/stream/216762-project-safe-transmute/topic/What.20should.20.60BikeshedIntrinsicFrom.60.20be.20named.3F
66 lines
2.0 KiB
Rust
66 lines
2.0 KiB
Rust
//! An array must have a well-defined layout to participate in a transmutation.
|
|
|
|
#![crate_type = "lib"]
|
|
#![feature(transmutability)]
|
|
#![allow(dead_code, incomplete_features, non_camel_case_types)]
|
|
|
|
mod assert {
|
|
use std::mem::{Assume, TransmuteFrom};
|
|
|
|
pub fn is_maybe_transmutable<Src, Dst>()
|
|
where
|
|
Dst: TransmuteFrom<Src, {
|
|
Assume::ALIGNMENT
|
|
.and(Assume::LIFETIMES)
|
|
.and(Assume::SAFETY)
|
|
.and(Assume::VALIDITY)
|
|
}>
|
|
{}
|
|
}
|
|
|
|
fn should_reject_repr_rust()
|
|
{
|
|
fn unit() {
|
|
type repr_rust = [String; 0];
|
|
assert::is_maybe_transmutable::<repr_rust, ()>(); //~ ERROR cannot be safely transmuted
|
|
assert::is_maybe_transmutable::<u128, repr_rust>(); //~ ERROR cannot be safely transmuted
|
|
}
|
|
|
|
fn singleton() {
|
|
type repr_rust = [String; 1];
|
|
assert::is_maybe_transmutable::<repr_rust, ()>(); //~ ERROR cannot be safely transmuted
|
|
assert::is_maybe_transmutable::<u128, repr_rust>(); //~ ERROR cannot be safely transmuted
|
|
}
|
|
|
|
fn duplex() {
|
|
type repr_rust = [String; 2];
|
|
assert::is_maybe_transmutable::<repr_rust, ()>(); //~ ERROR cannot be safely transmuted
|
|
assert::is_maybe_transmutable::<u128, repr_rust>(); //~ ERROR cannot be safely transmuted
|
|
}
|
|
}
|
|
|
|
fn should_accept_repr_C()
|
|
{
|
|
fn unit() {
|
|
#[repr(C)] struct repr_c(u8, u16, u8);
|
|
type array = [repr_c; 0];
|
|
assert::is_maybe_transmutable::<array, ()>();
|
|
assert::is_maybe_transmutable::<i128, array>();
|
|
}
|
|
|
|
fn singleton() {
|
|
#[repr(C)] struct repr_c(u8, u16, u8);
|
|
type array = [repr_c; 1];
|
|
assert::is_maybe_transmutable::<array, repr_c>();
|
|
assert::is_maybe_transmutable::<repr_c, array>();
|
|
}
|
|
|
|
fn duplex() {
|
|
#[repr(C)] struct repr_c(u8, u16, u8);
|
|
#[repr(C)] struct duplex(repr_c, repr_c);
|
|
type array = [repr_c; 2];
|
|
assert::is_maybe_transmutable::<array, duplex>();
|
|
assert::is_maybe_transmutable::<duplex, array>();
|
|
}
|
|
}
|