Skip to content

Commit c158b7b

Browse files
committed
Derive Eq/PartialEq instead of manually implementing it
1 parent e2d7cdc commit c158b7b

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

library/core/src/ptr/alignment.rs

+23-15
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,34 @@ use crate::{cmp, fmt, hash, mem, num};
99
/// Note that particularly large alignments, while representable in this type,
1010
/// are likely not to be supported by actual allocators and linkers.
1111
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
12-
#[derive(Copy, Clone)]
12+
#[derive(Copy, Clone, Eq, PartialEq)]
1313
#[repr(transparent)]
1414
pub struct Alignment(AlignmentEnum);
1515

1616
// Alignment is `repr(usize)`, but via extra steps.
1717
const _: () = assert!(mem::size_of::<Alignment>() == mem::size_of::<usize>());
1818
const _: () = assert!(mem::align_of::<Alignment>() == mem::align_of::<usize>());
1919

20+
fn _alignment_can_be_structurally_matched(a: Alignment) -> bool {
21+
matches!(a, Alignment::MIN)
22+
}
23+
2024
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+
2140
/// Returns the alignment for a type.
2241
///
2342
/// This provides the same numerical value as [`mem::align_of`],
@@ -127,17 +146,6 @@ impl TryFrom<usize> for Alignment {
127146
}
128147
}
129148

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-
141149
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
142150
impl cmp::Ord for Alignment {
143151
#[inline]
@@ -169,7 +177,7 @@ type AlignmentEnum = AlignmentEnum32;
169177
#[cfg(target_pointer_width = "64")]
170178
type AlignmentEnum = AlignmentEnum64;
171179

172-
#[derive(Copy, Clone)]
180+
#[derive(Copy, Clone, Eq, PartialEq)]
173181
#[repr(u16)]
174182
enum AlignmentEnum16 {
175183
_Align1Shl0 = 1 << 0,
@@ -190,7 +198,7 @@ enum AlignmentEnum16 {
190198
_Align1Shl15 = 1 << 15,
191199
}
192200

193-
#[derive(Copy, Clone)]
201+
#[derive(Copy, Clone, Eq, PartialEq)]
194202
#[repr(u32)]
195203
enum AlignmentEnum32 {
196204
_Align1Shl0 = 1 << 0,
@@ -227,7 +235,7 @@ enum AlignmentEnum32 {
227235
_Align1Shl31 = 1 << 31,
228236
}
229237

230-
#[derive(Copy, Clone)]
238+
#[derive(Copy, Clone, Eq, PartialEq)]
231239
#[repr(u64)]
232240
enum AlignmentEnum64 {
233241
_Align1Shl0 = 1 << 0,

0 commit comments

Comments
 (0)