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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2024-2026 Gabriel Bjørnager Jensen.
//
// SPDX: MIT OR Apache-2.0
//! The [`Unaligned`] trait.
use ;
use ;
use PhantomData;
use ;
use ;
use ascii;
use ByteStr;
use ;
/// Denotes an unaligned type.
///
/// # Safety
///
/// Only types that always have an alignment
/// requirement of exactly one byte may implement
/// this trait.
pub unsafe
// SAFETY: `[T; N]` has the same alignment as `T`:
// [0]
//
// > An array of `[T; N]` has... and the same
// > alignment of `T`.
//
// [0]: The Rust Standard Library, array
unsafe
// SAFETY: `AtomicBool` has the same layout as
// `bool`: [0]
//
// > This type has the same size, alignment, and
// > bit validity as a `bool`.
//
// [0]: The Rust Standard Library, core::sync::atomic::AtomicBool
unsafe
// SAFETY: `AtomicI8` has the same layout as `i8`:
// [0]
//
// > This type has the same size, alignment, and
// > bit validity as the underlying integer type,
// `i8`.
//
// [0]: The Rust Standard Library, core::sync::atomic::AtomicI8
unsafe
// SAFETY: `AtomicU8` has the same layout as `u8`:
// [0]
//
// > This type has the same size, alignment, and
// > bit validity as the underlying integer type,
// `i8`.
//
// [0]: The Rust Standard Library, core::sync::atomic::AtomicU8
unsafe
// SAFETY: `Cell<T>` has the same layout as
// `UnsafeCell<T>`: [0]
//
// > `Cell<T>` has the same memory layout and cave-
// > ats as `UnsafeCell<T>`.
//
// [0]: The Rust Standard Library, core::cell::Cell
unsafe
// SAFETY: The size of `Char` is guaranteed to be
// `1`: [0]
//
// > This type is guaranteed to have a size and
// > alignment of 1 byte.
//
// [0]: The Rust Standard Library, core::ascii::Char
unsafe
// SAFETY: `ByteStr` is a transparent, structural
// wrapper for `[u8]`. It is generally *inferred*
// that this is a stable guarantee (although, for-
// mally, only the constant reference representa-
// tion is guaranteed:) [0]
//
// > A `&ByteStr` has the same representation as a
// > `&str`.
//
// [0]: The Rust Standard Library, core::bstr::ByteStr
unsafe
// SAFETY: The size of a boolean is `1`. [0] The
// alignment of a type cannot be greater than its
// size: [1]
//
// > The alignment of primitives is platform-spe-
// > cific. In most cases, their alignment is equal
// to their size, but it may be less.
//
// [0]: The Rust Reference, layout.primitive.size
// [1]: The Rust Reference, layout.primitive.platform-specific-alignement
unsafe
// SAFETY: The size of `i8` is `1`. [0] The align-
// ment of a type cannot be greater than its size:
// [1]
//
// > The alignment of primitives is platform-spe-
// > cific. In most cases, their alignment is equal
// to their size, but it may be less.
//
// [0]: The Rust Reference, layout.primitive.size
// [1]: The Rust Reference, layout.primitive.platform-specific-alignement
unsafe
// SAFETY: `ManuallyDrop<T>` has the same layout as
// `T`: [0]
//
// > `ManuallyDrop<T>` is guaranteed to have the
// > same layout and bit validity as `T`...
//
// [0]: The Rust Standard Library, core::mem::ManuallyDrop
unsafe
// SAFETY: `MaybeUninit<T>` has the same layout as
// `T`: [0]
//
// > `MaybeUninit<T>` is guaranteed to have the
// > same size, alignment, and ABI as `T`...
//
// [0]: The Rust Standard Library, core::mem::MaybeUninit
unsafe
// SAFETY: `NonZero<i8>` has the same layout as
// `i8`: [0]
//
// > `NonZero<T>` is guaranteed to have the same
// > layout and bit validity as `T`...
//
// [0]: The Rust Standard Library, core::num::NonZero
unsafe
// SAFETY: `NonZero<u8>` has the same layout as
// `u8`: [0]
//
// > `NonZero<T>` is guaranteed to have the same
// > layout and bit validity as `T`...
//
// [0]: The Rust Standard Library, core::num::NonZero
unsafe
// SAFETY: `Option<NonZero<i8>>` has the same
// layout as `NonZero<i8>`: [0]
//
// > Thanks to the null pointer optimization,
// > `NonZero<T>` and `Option<NonZero<T>>` are
// > guaranteed to have the same size and align-
// > ment...
//
// [0]: The Rust Standard Library, core::num::NonZero
unsafe
// SAFETY: `Option<NonZero<u8>>` has the same
// layout as `NonZero<u8>`: [0]
//
// > Thanks to the null pointer optimization,
// > `NonZero<T>` and `Option<NonZero<T>>` are
// > guaranteed to have the same size and align-
// > ment...
//
// [0]: The Rust Standard Library, core::num::NonZero
unsafe
// SAFETY: `Ordering` has `i8` representation and
// no explicit alignment, making it have the same
// layout as `i8`: [0]
//
// > For field-less enums, primitive representa-
// > tions set the size and alignment to be the
// > same as the primitive type of the same name.
//
// [0]: The Rust Reference, layout.repr.primitive.enum
unsafe
// SAFETY: `PhantomData` is guaranteed to be un-
// aligned: [0]
//
// > For all `T`, the following are guaranteed:
// >
// > * `size_of::<PhantomData<T>>() == 0`
// > * `align_of::<PhantomData<T>>() == 1`
//
// [0]: The Rust Standard Library, core::marker::PhantomData
unsafe
// SAFETY: NOT GUARANTEED, DO NOT UNCOMMENT.
//unsafe impl Unaligned for PhantomPinned {}
// SAFETY: `Reverse<T>` is a structure transparent
// to `T`.
unsafe
// SAFETY: `Saturating<T>` is a structure transpar-
// ent to `T`.
unsafe
// SAFETY: Slices have the same layout as arrays:
// [0]
//
// > Slices have the same layout as the section of
// > the array they slice.
//
// [0]: The Rust Reference, layout.slice
unsafe
// SAFETY: String slices have the same layout as
// octet slices: [0]
//
// > String slices are a UTF-8 representation of
// > characters that have the same layout as slices
// > of type `[u8]`.
//
// [0]: The Rust Reference, layout.str
unsafe
// SAFETY: The size of `u8` is `1`. [0] The align-
// ment of a type cannot be greater than its size:
// [1]
//
// > The alignment of primitives is platform-spe-
// > cific. In most cases, their alignment is equal
// to their size, but it may be less.
//
// [0]: The Rust Reference, layout.primitive.size
// [1]: The Rust Reference, layout.primitive.platform-specific-alignement
unsafe
// SAFETY: `()` always has alignment of `1`: [0]
//
// > ... The exception to this is the unit tuple
// > (`()`), which is guaranteed as a zero-sized
// > type to have a size of 0 and an alignment of
// > 1.
//
// [0]: The Rust Reference, layout.tuple.unit
unsafe
// SAFETY: `UnsafeCell<T>` has the same layout as
// `T`: [0]
//
// > Even though `T` and `UnsafeCell<T>` have the
// > same memory layout...
//
// [0]: The Rust Standard Library, core::cell::UnsafeCell
unsafe
// SAFETY: `Wrapping<T>` has the same layout as
// `T`: [0]
//
// > `Wrapping<T>` is guaranteed to have the same
// > layout and ABI as `T`.
//
// [0]: The Rust Standard Library, core::num::Wrapping
unsafe