Skip to content

Commit 6ee1eea

Browse files
committed
Manually implement Ord according to CAN bus arbitration rules
1 parent fd280d5 commit 6ee1eea

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/can/id.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! CAN Identifiers.
22
33
/// Standard 11-bit CAN Identifier (`0..=0x7FF`).
4-
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
4+
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Hash)]
55
pub struct StandardId(u16);
66

77
impl StandardId {
@@ -39,8 +39,15 @@ impl StandardId {
3939
}
4040
}
4141

42+
/// Note that this compares as `StandardId::MAX < StandardId::ZERO`
43+
impl Ord for StandardId {
44+
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
45+
other.0.cmp(&self.0)
46+
}
47+
}
48+
4249
/// Extended 29-bit CAN Identifier (`0..=1FFF_FFFF`).
43-
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
50+
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Hash)]
4451
pub struct ExtendedId(u32);
4552

4653
impl ExtendedId {
@@ -84,6 +91,13 @@ impl ExtendedId {
8491
}
8592
}
8693

94+
/// Note that this compares as `ExtendedId::MAX < ExtendedId::ZERO`
95+
impl Ord for ExtendedId {
96+
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
97+
other.0.cmp(&self.0)
98+
}
99+
}
100+
87101
/// A CAN Identifier (standard or extended).
88102
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
89103
pub enum Id {
@@ -157,4 +171,20 @@ mod tests {
157171
StandardId::new((ExtendedId::MAX.0 >> 18) as u16)
158172
);
159173
}
174+
175+
#[test]
176+
fn cmp_standard() {
177+
assert!(StandardId::MAX < StandardId::ZERO);
178+
}
179+
180+
#[test]
181+
fn cmp_extended() {
182+
assert!(ExtendedId::MAX < ExtendedId::ZERO);
183+
}
184+
185+
#[test]
186+
fn cmp_id() {
187+
assert!(Id::Standard(StandardId::MAX) < Id::Standard(StandardId::ZERO));
188+
assert!(Id::Extended(ExtendedId::MAX) < Id::Extended(ExtendedId::ZERO));
189+
}
160190
}

0 commit comments

Comments
 (0)