Skip to content

Commit f914e77

Browse files
authored
Merge pull request #11 from garbageslam/add_eq_ord_hash
Aligned implements Eq, Ord, Hash if available
2 parents 6519c12 + 2231756 commit f914e77

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)