@@ -9,15 +9,34 @@ use crate::{cmp, fmt, hash, mem, num};
9
9
/// Note that particularly large alignments, while representable in this type,
10
10
/// are likely not to be supported by actual allocators and linkers.
11
11
#[ unstable( feature = "ptr_alignment_type" , issue = "102070" ) ]
12
- #[ derive( Copy , Clone ) ]
12
+ #[ derive( Copy , Clone , Eq , PartialEq ) ]
13
13
#[ repr( transparent) ]
14
14
pub struct Alignment ( AlignmentEnum ) ;
15
15
16
16
// Alignment is `repr(usize)`, but via extra steps.
17
17
const _: ( ) = assert ! ( mem:: size_of:: <Alignment >( ) == mem:: size_of:: <usize >( ) ) ;
18
18
const _: ( ) = assert ! ( mem:: align_of:: <Alignment >( ) == mem:: align_of:: <usize >( ) ) ;
19
19
20
+ fn _alignment_can_be_structurally_matched ( a : Alignment ) -> bool {
21
+ matches ! ( a, Alignment :: MIN )
22
+ }
23
+
20
24
impl Alignment {
25
+ /// The smallest possible alignment, 1.
26
+ ///
27
+ /// All addresses are always aligned at least this much.
28
+ ///
29
+ /// # Examples
30
+ ///
31
+ /// ```
32
+ /// #![feature(ptr_alignment_type)]
33
+ /// use std::ptr::Alignment;
34
+ ///
35
+ /// assert_eq!(Alignment::MIN.as_usize(), 1);
36
+ /// ```
37
+ #[ unstable( feature = "ptr_alignment_type" , issue = "102070" ) ]
38
+ pub const MIN : Self = Self ( AlignmentEnum :: _Align1Shl0) ;
39
+
21
40
/// Returns the alignment for a type.
22
41
///
23
42
/// This provides the same numerical value as [`mem::align_of`],
@@ -127,17 +146,6 @@ impl TryFrom<usize> for Alignment {
127
146
}
128
147
}
129
148
130
- #[ unstable( feature = "ptr_alignment_type" , issue = "102070" ) ]
131
- impl cmp:: Eq for Alignment { }
132
-
133
- #[ unstable( feature = "ptr_alignment_type" , issue = "102070" ) ]
134
- impl cmp:: PartialEq for Alignment {
135
- #[ inline]
136
- fn eq ( & self , other : & Self ) -> bool {
137
- self . as_nonzero ( ) == other. as_nonzero ( )
138
- }
139
- }
140
-
141
149
#[ unstable( feature = "ptr_alignment_type" , issue = "102070" ) ]
142
150
impl cmp:: Ord for Alignment {
143
151
#[ inline]
@@ -169,7 +177,7 @@ type AlignmentEnum = AlignmentEnum32;
169
177
#[ cfg( target_pointer_width = "64" ) ]
170
178
type AlignmentEnum = AlignmentEnum64 ;
171
179
172
- #[ derive( Copy , Clone ) ]
180
+ #[ derive( Copy , Clone , Eq , PartialEq ) ]
173
181
#[ repr( u16 ) ]
174
182
enum AlignmentEnum16 {
175
183
_Align1Shl0 = 1 << 0 ,
@@ -190,7 +198,7 @@ enum AlignmentEnum16 {
190
198
_Align1Shl15 = 1 << 15 ,
191
199
}
192
200
193
- #[ derive( Copy , Clone ) ]
201
+ #[ derive( Copy , Clone , Eq , PartialEq ) ]
194
202
#[ repr( u32 ) ]
195
203
enum AlignmentEnum32 {
196
204
_Align1Shl0 = 1 << 0 ,
@@ -227,7 +235,7 @@ enum AlignmentEnum32 {
227
235
_Align1Shl31 = 1 << 31 ,
228
236
}
229
237
230
- #[ derive( Copy , Clone ) ]
238
+ #[ derive( Copy , Clone , Eq , PartialEq ) ]
231
239
#[ repr( u64 ) ]
232
240
enum AlignmentEnum64 {
233
241
_Align1Shl0 = 1 << 0 ,
0 commit comments