Skip to content

Commit 8e28c88

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

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,20 @@ 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+
}

tests/book_trees.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn initialize_from_table_collection() {
8686
// The children function returns another iterator
8787
let _siblings = tree
8888
.children(parent)
89-
.filter(|child| child != &node)
89+
.filter(|child| child != node)
9090
.collect::<Vec<_>>();
9191
}
9292
}

0 commit comments

Comments
 (0)