1 use crate::convert::{TryFrom, TryInto};
2 use crate::intrinsics::assert_unsafe_precondition;
3 use crate::num::NonZeroUsize;
4 use crate::{cmp, fmt, hash, mem, num};
6 /// A type storing a `usize` which is a power of two, and thus
7 /// represents a possible alignment in the rust abstract machine.
9 /// Note that particularly large alignments, while representable in this type,
10 /// are likely not to be supported by actual allocators and linkers.
11 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
12 #[derive(Copy, Clone, Eq, PartialEq)]
14 pub struct Alignment(AlignmentEnum);
16 // Alignment is `repr(usize)`, but via extra steps.
17 const _: () = assert!(mem::size_of::<Alignment>() == mem::size_of::<usize>());
18 const _: () = assert!(mem::align_of::<Alignment>() == mem::align_of::<usize>());
20 fn _alignment_can_be_structurally_matched(a: Alignment) -> bool {
21 matches!(a, Alignment::MIN)
25 /// The smallest possible alignment, 1.
27 /// All addresses are always aligned at least this much.
32 /// #![feature(ptr_alignment_type)]
33 /// use std::ptr::Alignment;
35 /// assert_eq!(Alignment::MIN.as_usize(), 1);
37 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
38 pub const MIN: Self = Self(AlignmentEnum::_Align1Shl0);
40 /// Returns the alignment for a type.
42 /// This provides the same numerical value as [`mem::align_of`],
43 /// but in an `Alignment` instead of a `usize.
44 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
46 pub const fn of<T>() -> Self {
47 // SAFETY: rustc ensures that type alignment is always a power of two.
48 unsafe { Alignment::new_unchecked(mem::align_of::<T>()) }
51 /// Creates an `Alignment` from a `usize`, or returns `None` if it's
52 /// not a power of two.
54 /// Note that `0` is not a power of two, nor a valid alignment.
55 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
57 pub const fn new(align: usize) -> Option<Self> {
58 if align.is_power_of_two() {
59 // SAFETY: Just checked it only has one bit set
60 Some(unsafe { Self::new_unchecked(align) })
66 /// Creates an `Alignment` from a power-of-two `usize`.
70 /// `align` must be a power of two.
72 /// Equivalently, it must be `1 << exp` for some `exp` in `0..usize::BITS`.
73 /// It must *not* be zero.
74 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
75 #[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")]
77 pub const unsafe fn new_unchecked(align: usize) -> Self {
78 // SAFETY: Precondition passed to the caller.
79 unsafe { assert_unsafe_precondition!((align: usize) => align.is_power_of_two()) };
81 // SAFETY: By precondition, this must be a power of two, and
82 // our variants encompass all possible powers of two.
83 unsafe { mem::transmute::<usize, Alignment>(align) }
86 /// Returns the alignment as a [`usize`]
87 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
88 #[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")]
90 pub const fn as_usize(self) -> usize {
94 /// Returns the alignment as a [`NonZeroUsize`]
95 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
97 pub const fn as_nonzero(self) -> NonZeroUsize {
98 // SAFETY: All the discriminants are non-zero.
99 unsafe { NonZeroUsize::new_unchecked(self.as_usize()) }
102 /// Returns the base-2 logarithm of the alignment.
104 /// This is always exact, as `self` represents a power of two.
109 /// #![feature(ptr_alignment_type)]
110 /// use std::ptr::Alignment;
112 /// assert_eq!(Alignment::of::<u8>().log2(), 0);
113 /// assert_eq!(Alignment::new(1024).unwrap().log2(), 10);
115 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
117 pub fn log2(self) -> u32 {
118 self.as_nonzero().trailing_zeros()
122 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
123 impl fmt::Debug for Alignment {
124 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125 write!(f, "{:?} (1 << {:?})", self.as_nonzero(), self.log2())
129 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
130 impl TryFrom<NonZeroUsize> for Alignment {
131 type Error = num::TryFromIntError;
134 fn try_from(align: NonZeroUsize) -> Result<Alignment, Self::Error> {
135 align.get().try_into()
139 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
140 impl TryFrom<usize> for Alignment {
141 type Error = num::TryFromIntError;
144 fn try_from(align: usize) -> Result<Alignment, Self::Error> {
145 Self::new(align).ok_or(num::TryFromIntError(()))
149 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
150 impl From<Alignment> for NonZeroUsize {
152 fn from(align: Alignment) -> NonZeroUsize {
157 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
158 impl From<Alignment> for usize {
160 fn from(align: Alignment) -> usize {
165 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
166 impl cmp::Ord for Alignment {
168 fn cmp(&self, other: &Self) -> cmp::Ordering {
169 self.as_nonzero().cmp(&other.as_nonzero())
173 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
174 impl cmp::PartialOrd for Alignment {
176 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
177 Some(self.cmp(other))
181 #[unstable(feature = "ptr_alignment_type", issue = "102070")]
182 impl hash::Hash for Alignment {
184 fn hash<H: hash::Hasher>(&self, state: &mut H) {
185 self.as_nonzero().hash(state)
189 #[cfg(target_pointer_width = "16")]
190 type AlignmentEnum = AlignmentEnum16;
191 #[cfg(target_pointer_width = "32")]
192 type AlignmentEnum = AlignmentEnum32;
193 #[cfg(target_pointer_width = "64")]
194 type AlignmentEnum = AlignmentEnum64;
196 #[derive(Copy, Clone, Eq, PartialEq)]
198 enum AlignmentEnum16 {
199 _Align1Shl0 = 1 << 0,
200 _Align1Shl1 = 1 << 1,
201 _Align1Shl2 = 1 << 2,
202 _Align1Shl3 = 1 << 3,
203 _Align1Shl4 = 1 << 4,
204 _Align1Shl5 = 1 << 5,
205 _Align1Shl6 = 1 << 6,
206 _Align1Shl7 = 1 << 7,
207 _Align1Shl8 = 1 << 8,
208 _Align1Shl9 = 1 << 9,
209 _Align1Shl10 = 1 << 10,
210 _Align1Shl11 = 1 << 11,
211 _Align1Shl12 = 1 << 12,
212 _Align1Shl13 = 1 << 13,
213 _Align1Shl14 = 1 << 14,
214 _Align1Shl15 = 1 << 15,
217 #[derive(Copy, Clone, Eq, PartialEq)]
219 enum AlignmentEnum32 {
220 _Align1Shl0 = 1 << 0,
221 _Align1Shl1 = 1 << 1,
222 _Align1Shl2 = 1 << 2,
223 _Align1Shl3 = 1 << 3,
224 _Align1Shl4 = 1 << 4,
225 _Align1Shl5 = 1 << 5,
226 _Align1Shl6 = 1 << 6,
227 _Align1Shl7 = 1 << 7,
228 _Align1Shl8 = 1 << 8,
229 _Align1Shl9 = 1 << 9,
230 _Align1Shl10 = 1 << 10,
231 _Align1Shl11 = 1 << 11,
232 _Align1Shl12 = 1 << 12,
233 _Align1Shl13 = 1 << 13,
234 _Align1Shl14 = 1 << 14,
235 _Align1Shl15 = 1 << 15,
236 _Align1Shl16 = 1 << 16,
237 _Align1Shl17 = 1 << 17,
238 _Align1Shl18 = 1 << 18,
239 _Align1Shl19 = 1 << 19,
240 _Align1Shl20 = 1 << 20,
241 _Align1Shl21 = 1 << 21,
242 _Align1Shl22 = 1 << 22,
243 _Align1Shl23 = 1 << 23,
244 _Align1Shl24 = 1 << 24,
245 _Align1Shl25 = 1 << 25,
246 _Align1Shl26 = 1 << 26,
247 _Align1Shl27 = 1 << 27,
248 _Align1Shl28 = 1 << 28,
249 _Align1Shl29 = 1 << 29,
250 _Align1Shl30 = 1 << 30,
251 _Align1Shl31 = 1 << 31,
254 #[derive(Copy, Clone, Eq, PartialEq)]
256 enum AlignmentEnum64 {
257 _Align1Shl0 = 1 << 0,
258 _Align1Shl1 = 1 << 1,
259 _Align1Shl2 = 1 << 2,
260 _Align1Shl3 = 1 << 3,
261 _Align1Shl4 = 1 << 4,
262 _Align1Shl5 = 1 << 5,
263 _Align1Shl6 = 1 << 6,
264 _Align1Shl7 = 1 << 7,
265 _Align1Shl8 = 1 << 8,
266 _Align1Shl9 = 1 << 9,
267 _Align1Shl10 = 1 << 10,
268 _Align1Shl11 = 1 << 11,
269 _Align1Shl12 = 1 << 12,
270 _Align1Shl13 = 1 << 13,
271 _Align1Shl14 = 1 << 14,
272 _Align1Shl15 = 1 << 15,
273 _Align1Shl16 = 1 << 16,
274 _Align1Shl17 = 1 << 17,
275 _Align1Shl18 = 1 << 18,
276 _Align1Shl19 = 1 << 19,
277 _Align1Shl20 = 1 << 20,
278 _Align1Shl21 = 1 << 21,
279 _Align1Shl22 = 1 << 22,
280 _Align1Shl23 = 1 << 23,
281 _Align1Shl24 = 1 << 24,
282 _Align1Shl25 = 1 << 25,
283 _Align1Shl26 = 1 << 26,
284 _Align1Shl27 = 1 << 27,
285 _Align1Shl28 = 1 << 28,
286 _Align1Shl29 = 1 << 29,
287 _Align1Shl30 = 1 << 30,
288 _Align1Shl31 = 1 << 31,
289 _Align1Shl32 = 1 << 32,
290 _Align1Shl33 = 1 << 33,
291 _Align1Shl34 = 1 << 34,
292 _Align1Shl35 = 1 << 35,
293 _Align1Shl36 = 1 << 36,
294 _Align1Shl37 = 1 << 37,
295 _Align1Shl38 = 1 << 38,
296 _Align1Shl39 = 1 << 39,
297 _Align1Shl40 = 1 << 40,
298 _Align1Shl41 = 1 << 41,
299 _Align1Shl42 = 1 << 42,
300 _Align1Shl43 = 1 << 43,
301 _Align1Shl44 = 1 << 44,
302 _Align1Shl45 = 1 << 45,
303 _Align1Shl46 = 1 << 46,
304 _Align1Shl47 = 1 << 47,
305 _Align1Shl48 = 1 << 48,
306 _Align1Shl49 = 1 << 49,
307 _Align1Shl50 = 1 << 50,
308 _Align1Shl51 = 1 << 51,
309 _Align1Shl52 = 1 << 52,
310 _Align1Shl53 = 1 << 53,
311 _Align1Shl54 = 1 << 54,
312 _Align1Shl55 = 1 << 55,
313 _Align1Shl56 = 1 << 56,
314 _Align1Shl57 = 1 << 57,
315 _Align1Shl58 = 1 << 58,
316 _Align1Shl59 = 1 << 59,
317 _Align1Shl60 = 1 << 60,
318 _Align1Shl61 = 1 << 61,
319 _Align1Shl62 = 1 << 62,
320 _Align1Shl63 = 1 << 63,