Skip to content

Commit c6e71e1

Browse files
committed
feat: impl PartialOrd for newtypes to reference
1 parent cf875bb commit c6e71e1

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/sys/macros.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,30 @@ macro_rules! impl_id_traits {
134134
}
135135
}
136136

137+
impl PartialEq<&$idtype> for $idtype {
138+
fn eq(&self, other: &&$idtype) -> bool {
139+
self.0 == other.0
140+
}
141+
}
142+
143+
impl PartialEq<$idtype> for &$idtype {
144+
fn eq(&self, other: &$idtype) -> bool {
145+
self.0 == other.0
146+
}
147+
}
148+
149+
impl PartialOrd<&$idtype> for $idtype {
150+
fn partial_cmp(&self, other: &&$idtype) -> Option<std::cmp::Ordering> {
151+
self.partial_cmp(&other.0)
152+
}
153+
}
154+
155+
impl PartialOrd<$idtype> for &$idtype {
156+
fn partial_cmp(&self, other: &$idtype) -> Option<std::cmp::Ordering> {
157+
self.0.partial_cmp(&other.0)
158+
}
159+
}
160+
137161
impl Default for $idtype {
138162
fn default() -> Self {
139163
Self::NULL

src/sys/newtypes.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,22 @@ fn test_try_from_reference() {
243243
let y = NodeId::from(2);
244244
assert_eq!(2, usize::try_from(&y).unwrap());
245245
}
246+
247+
#[test]
248+
fn test_eq_to_self_ref() {
249+
let x = NodeId::from(1);
250+
let rx = &x;
251+
assert_eq!(x, rx);
252+
assert_eq!(rx, x);
253+
}
254+
255+
#[test]
256+
fn test_ord_to_self_ref() {
257+
let x = NodeId::from(1);
258+
let y = NodeId::from(2);
259+
let ry = &y;
260+
assert!(x < ry);
261+
assert!(ry > x);
262+
}
263+
264+

0 commit comments

Comments
 (0)