Skip to content

Commit 8251b9b

Browse files
jswrennm1el
andcommitted
Initial (incomplete) implementation of transmutability trait.
This initial implementation handles transmutations between types with specified layouts, except when references are involved. Co-authored-by: Igor null <m1el.2027@gmail.com>
1 parent 59c7b33 commit 8251b9b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

core/src/mem/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ mod valid_align;
2727
// alignment as a parameter, such as `Layout::padding_needed_for`.
2828
pub(crate) use valid_align::ValidAlign;
2929

30+
mod transmutability;
31+
#[unstable(feature = "transmutability", issue = "none")]
32+
pub use transmutability::{Assume, BikeshedIntrinsicFrom};
33+
3034
#[stable(feature = "rust1", since = "1.0.0")]
3135
#[doc(inline)]
3236
pub use crate::intrinsics::transmute;

core/src/mem/transmutability.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/// Are values of a type transmutable into values of another type?
2+
///
3+
/// This trait is implemented on-the-fly by the compiler for types `Src` and `Self` when the bits of
4+
/// any value of type `Self` are safely transmutable into a value of type `Dst`, in a given `Context`,
5+
/// notwithstanding whatever safety checks you have asked the compiler to [`Assume`] are satisfied.
6+
#[unstable(feature = "transmutability", issue = "none")]
7+
#[cfg_attr(not(bootstrap), lang = "transmute_trait")]
8+
pub unsafe trait BikeshedIntrinsicFrom<
9+
Src,
10+
Context,
11+
const ASSUME_ALIGNMENT: bool,
12+
const ASSUME_LIFETIMES: bool,
13+
const ASSUME_VALIDITY: bool,
14+
const ASSUME_VISIBILITY: bool,
15+
> where
16+
Src: ?Sized,
17+
{
18+
}
19+
20+
/// What transmutation safety conditions shall the compiler assume that *you* are checking?
21+
#[unstable(feature = "transmutability", issue = "none")]
22+
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
23+
pub struct Assume {
24+
/// When `true`, the compiler assumes that *you* are ensuring (either dynamically or statically) that
25+
/// destination referents do not have stricter alignment requirements than source referents.
26+
pub alignment: bool,
27+
28+
/// When `true`, the compiler assume that *you* are ensuring that lifetimes are not extended in a manner
29+
/// that violates Rust's memory model.
30+
pub lifetimes: bool,
31+
32+
/// When `true`, the compiler assumes that *you* are ensuring that the source type is actually a valid
33+
/// instance of the destination type.
34+
pub validity: bool,
35+
36+
/// When `true`, the compiler assumes that *you* have ensured that it is safe for you to violate the
37+
/// type and field privacy of the destination type (and sometimes of the source type, too).
38+
pub visibility: bool,
39+
}

0 commit comments

Comments
 (0)