Skip to content

Commit 2231756

Browse files
committed
Aligned implements Eq, Ord, Hash if available
The motivations are mainly to allow this to be used easily in types that live in sets and maps. Implementing `PartialEq` also allows to make tests that `assert_eq!` that `Aligned` values are equal.
1 parent 6519c12 commit 2231756

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

src/lib.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
#![deny(warnings)]
3030
#![cfg_attr(not(test), no_std)]
3131

32-
use core::fmt::{Display, Debug};
33-
use core::ops;
32+
use core::{cmp::Ordering, fmt::{Display, Debug}, hash::{Hash, Hasher}, ops};
3433

3534
use as_slice::{AsMutSlice, AsSlice};
3635

@@ -172,6 +171,52 @@ where
172171
}
173172
}
174173

174+
impl<A, T> PartialEq for Aligned<A, T>
175+
where
176+
A: sealed::Alignment,
177+
T: PartialEq,
178+
{
179+
fn eq(&self, other: &Self) -> bool {
180+
self.value == other.value
181+
}
182+
}
183+
184+
impl<A, T> Eq for Aligned<A, T>
185+
where
186+
A: sealed::Alignment,
187+
T: Eq,
188+
{}
189+
190+
impl<A, T> Hash for Aligned<A, T>
191+
where
192+
A: sealed::Alignment,
193+
T: Hash,
194+
{
195+
fn hash<H: Hasher>(&self, state: &mut H) {
196+
self.value.hash(state);
197+
}
198+
}
199+
200+
impl<A, T> Ord for Aligned<A, T>
201+
where
202+
A: sealed::Alignment,
203+
T: Ord,
204+
{
205+
fn cmp(&self, other: &Self) -> Ordering {
206+
self.value.cmp(&other.value)
207+
}
208+
}
209+
210+
impl<A, T> PartialOrd for Aligned<A, T>
211+
where
212+
A: sealed::Alignment,
213+
T: PartialOrd,
214+
{
215+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
216+
self.value.partial_cmp(&other.value)
217+
}
218+
}
219+
175220
#[test]
176221
fn sanity() {
177222
use core::mem;

0 commit comments

Comments
 (0)