Skip to content

Commit d87d4f7

Browse files
authored
feat: impl PartialOrd for newtypes to reference (#725)
* update some other impls in terms of Ord
1 parent cf875bb commit d87d4f7

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/sys/macros.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,37 @@ macro_rules! impl_id_traits {
124124

125125
impl PartialOrd<super::bindings::tsk_id_t> for $idtype {
126126
fn partial_cmp(&self, other: &super::bindings::tsk_id_t) -> Option<std::cmp::Ordering> {
127-
self.0.partial_cmp(other)
127+
Some(self.0.cmp(other))
128128
}
129129
}
130130

131131
impl PartialOrd<$idtype> for super::bindings::tsk_id_t {
132132
fn partial_cmp(&self, other: &$idtype) -> Option<std::cmp::Ordering> {
133-
self.partial_cmp(&other.0)
133+
Some(self.cmp(&other.0))
134+
}
135+
}
136+
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+
Some(self.0.cmp(&other.0))
152+
}
153+
}
154+
155+
impl PartialOrd<$idtype> for &$idtype {
156+
fn partial_cmp(&self, other: &$idtype) -> Option<std::cmp::Ordering> {
157+
Some(self.0.cmp(&other.0))
134158
}
135159
}
136160

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)