1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
//@ check-pass
//! Checks that niche optimizations are encoded correctly.
#![crate_type = "lib"]
#![feature(transmutability)]
#![allow(dead_code, incomplete_features, non_camel_case_types)]
mod assert {
use std::mem::{Assume, TransmuteFrom};
pub fn is_transmutable<Src, Dst>()
where
Dst: TransmuteFrom<Src, {
Assume {
alignment: false,
lifetimes: false,
safety: true,
validity: false,
}
}>
{}
pub fn is_maybe_transmutable<Src, Dst>()
where
Dst: TransmuteFrom<Src, {
Assume {
alignment: false,
lifetimes: false,
safety: true,
validity: true,
}
}>
{}
}
#[repr(u8)] enum V0 { V = 0 }
#[repr(u8)] enum V1 { V = 1 }
#[repr(u8)] enum V2 { V = 2 }
#[repr(u8)] enum V253 { V = 253 }
#[repr(u8)] enum V254 { V = 254 }
#[repr(u8)] enum V255 { V = 255 }
fn bool() {
enum OptionLike {
A(bool),
B,
}
const _: () = {
assert!(std::mem::size_of::<OptionLike>() == 1);
};
assert::is_transmutable::<OptionLike, u8>();
assert::is_transmutable::<bool, OptionLike>();
assert::is_transmutable::<V0, OptionLike>();
assert::is_transmutable::<V1, OptionLike>();
assert::is_transmutable::<V2, OptionLike>();
}
fn one_niche() {
#[repr(u8)]
enum N1 {
S = 0,
E = 255 - 1,
}
enum OptionLike {
A(N1),
B,
}
const _: () = {
assert!(std::mem::size_of::<OptionLike>() == 1);
};
assert::is_transmutable::<OptionLike, u8>();
assert::is_transmutable::<V0, OptionLike>();
assert::is_transmutable::<V1, OptionLike>();
assert::is_transmutable::<V254, OptionLike>();
}
fn one_niche_alt() {
#[repr(u8)]
enum N1 {
S = 1,
E = 255 - 1,
}
enum OptionLike {
A(N1),
B,
C,
}
const _: () = {
assert!(std::mem::size_of::<OptionLike>() == 1);
};
assert::is_transmutable::<OptionLike, u8>();
assert::is_transmutable::<V1, OptionLike>();
assert::is_transmutable::<V2, OptionLike>();
assert::is_transmutable::<V254, OptionLike>();
}
fn two_niche() {
#[repr(u8)]
enum Niche {
S = 0,
E = 255 - 2,
}
enum OptionLike {
A(Niche),
B,
C,
}
const _: () = {
assert!(std::mem::size_of::<OptionLike>() == 1);
};
assert::is_transmutable::<OptionLike, u8>();
assert::is_transmutable::<V0, OptionLike>();
assert::is_transmutable::<V1, OptionLike>();
assert::is_transmutable::<V2, OptionLike>();
assert::is_transmutable::<V253, OptionLike>();
}
fn no_niche() {
use std::mem::MaybeUninit;
#[repr(u8)]
enum Niche {
S = 0,
E = 255 - 1,
}
enum OptionLike {
A(Niche),
B,
C,
}
const _: () = {
assert!(std::mem::size_of::<OptionLike>() == 1);
};
#[repr(C)]
struct Pair<T, U>(T, U);
assert::is_transmutable::<V0, Niche>();
assert::is_transmutable::<V254, Niche>();
assert::is_transmutable::<Pair<V0, Niche>, OptionLike>();
assert::is_transmutable::<Pair<V1, MaybeUninit<u8>>, OptionLike>();
assert::is_transmutable::<Pair<V2, MaybeUninit<u8>>, OptionLike>();
}
fn niche_fields() {
enum Kind {
A(bool, bool),
B(bool),
}
assert::is_maybe_transmutable::<u16, Kind>();
}
|