-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.rs
More file actions
224 lines (188 loc) · 5.84 KB
/
Copy pathmain.rs
File metadata and controls
224 lines (188 loc) · 5.84 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use core::mem::{size_of, transmute};
#[repr(u8)]
enum Sparse {
One = 1,
Eight = 8,
High = 128,
}
#[repr(transparent)]
struct TransparentSparse(Sparse);
#[repr(transparent)]
struct NestedTransparent(TransparentSparse);
#[repr(i16)]
enum SignedDiscriminant {
Negative = -7,
Positive = 300,
}
#[repr(C)]
struct Pair {
first: u16,
second: u16,
}
#[repr(C)]
struct Padded {
first: u8,
middle: u16,
last: u8,
}
#[repr(C, u8)]
enum Payload {
Number(u16),
Bytes(u8, u8),
}
#[repr(C)]
union WordBytes {
word: u32,
bytes: [u8; 4],
}
struct Marker;
#[repr(transparent)]
struct ByteSlice([u8]);
#[repr(transparent)]
struct OuterByteSlice(ByteSlice);
impl ByteSlice {
#[inline(never)]
fn first(&self) -> u8 {
self.0[0]
}
#[inline(never)]
fn len(&self) -> usize {
self.0.len()
}
}
impl OuterByteSlice {
#[inline(never)]
fn first(&self) -> u8 {
self.0.first()
}
#[inline(never)]
fn len(&self) -> usize {
self.0.len()
}
}
fn test_scalar_bits() {
let truth: bool = unsafe { transmute(1u8) };
assert!(truth);
let truth_bits: u8 = unsafe { transmute(truth) };
assert!(truth_bits == 1);
let lambda: char = unsafe { transmute(0x03bbu32) };
assert!(lambda == 'λ');
let lambda_bits: u32 = unsafe { transmute(lambda) };
assert!(lambda_bits == 0x03bb);
}
fn test_float_bits() {
let nan_bits = 0x7fc1_2345u32;
let nan: f32 = unsafe { transmute(nan_bits) };
let round_trip: u32 = unsafe { transmute(nan) };
assert!(round_trip == nan_bits);
let pi_bits = 0x4009_21fb_5444_2d18i64;
let pi: f64 = unsafe { transmute(pi_bits) };
assert!(pi > 3.141 && pi < 3.142);
let round_trip: i64 = unsafe { transmute(pi) };
assert!(round_trip == pi_bits);
}
fn test_fieldless_enums_and_transparent_wrappers() {
let sparse: Sparse = unsafe { transmute(128u8) };
assert!(matches!(sparse, Sparse::High));
let sparse_bits: u8 = unsafe { transmute(sparse) };
assert!(sparse_bits == 128);
let wrapped: TransparentSparse = unsafe { transmute(8u8) };
assert!(matches!(wrapped.0, Sparse::Eight));
let wrapped_bits: u8 = unsafe { transmute(wrapped) };
assert!(wrapped_bits == 8);
let nested: NestedTransparent = unsafe { transmute(1u8) };
assert!(matches!((nested.0).0, Sparse::One));
let nested_bits: u8 = unsafe { transmute(nested) };
assert!(nested_bits == 1);
let signed: SignedDiscriminant = unsafe { transmute(-7i16) };
assert!(matches!(signed, SignedDiscriminant::Negative));
let signed_bits: i16 = unsafe { transmute(signed) };
assert!(signed_bits == -7);
let positive: SignedDiscriminant = unsafe { transmute(300i16) };
assert!(matches!(positive, SignedDiscriminant::Positive));
}
fn test_arrays_tuples_and_structs() {
let bytes = [0x78u8, 0x56, 0x34, 0x12];
let word: u32 = unsafe { transmute(bytes) };
assert!(word == 0x1234_5678);
let round_trip: [u8; 4] = unsafe { transmute(word) };
assert!(round_trip[0] == 0x78);
assert!(round_trip[1] == 0x56);
assert!(round_trip[2] == 0x34);
assert!(round_trip[3] == 0x12);
let pair: Pair = unsafe { transmute((0x1234u16, 0x5678u16)) };
assert!(pair.first == 0x1234);
assert!(pair.second == 0x5678);
let tuple: (u16, u16) = unsafe { transmute(pair) };
assert!(tuple.0 == 0x1234);
assert!(tuple.1 == 0x5678);
let padded = Padded {
first: 3,
middle: 0x4567,
last: 9,
};
let encoded: [u8; size_of::<Padded>()] = unsafe { transmute(padded) };
let decoded: Padded = unsafe { transmute(encoded) };
assert!(decoded.first == 3);
assert!(decoded.middle == 0x4567);
assert!(decoded.last == 9);
}
fn test_data_carrying_enums() {
let number = Payload::Number(0x3456);
let number_bytes: [u8; size_of::<Payload>()] = unsafe { transmute(number) };
let number_round_trip: Payload = unsafe { transmute(number_bytes) };
match number_round_trip {
Payload::Number(value) => assert!(value == 0x3456),
Payload::Bytes(_, _) => panic!(),
}
let bytes = Payload::Bytes(17, 29);
let encoded: [u8; size_of::<Payload>()] = unsafe { transmute(bytes) };
let decoded: Payload = unsafe { transmute(encoded) };
match decoded {
Payload::Bytes(first, second) => {
assert!(first == 17);
assert!(second == 29);
}
Payload::Number(_) => panic!(),
}
}
fn test_union_storage_round_trips() {
let from_bytes = WordBytes {
bytes: [0x44, 0x33, 0x22, 0x11],
};
let word: u32 = unsafe { transmute(from_bytes) };
assert!(word == 0x1122_3344);
let from_word: WordBytes = unsafe { transmute(0x89ab_cdefu32) };
let bytes: [u8; 4] = unsafe { transmute(from_word) };
assert!(bytes[0] == 0xef);
assert!(bytes[1] == 0xcd);
assert!(bytes[2] == 0xab);
assert!(bytes[3] == 0x89);
}
fn test_zero_sized_values() {
let marker: Marker = unsafe { transmute(()) };
let unit: () = unsafe { transmute(marker) };
assert!(unit == ());
}
fn test_unsized_transparent_reference_transmute() {
let bytes = [11_u8, 22, 33, 44];
let wrapped: &ByteSlice = unsafe { transmute::<&[u8], &ByteSlice>(&bytes) };
assert!(wrapped.len() == 4);
assert!(wrapped.first() == 11);
let outer = unsafe { &*(wrapped as *const ByteSlice as *const OuterByteSlice) };
assert!(outer.len() == 4);
assert!(outer.first() == 11);
let empty: &[u8] = &[];
let empty_wrapped: &ByteSlice = unsafe { transmute(empty) };
assert!(empty_wrapped.len() == 0);
}
fn main() {
test_scalar_bits();
test_float_bits();
test_fieldless_enums_and_transparent_wrappers();
test_arrays_tuples_and_structs();
test_data_carrying_enums();
test_union_storage_round_trips();
test_zero_sized_values();
test_unsized_transparent_reference_transmute();
}