|
1 | 1 | //! CAN Identifiers.
|
2 | 2 |
|
3 | 3 | /// 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)] |
5 | 5 | pub struct StandardId(u16);
|
6 | 6 |
|
7 | 7 | impl StandardId {
|
@@ -39,8 +39,15 @@ impl StandardId {
|
39 | 39 | }
|
40 | 40 | }
|
41 | 41 |
|
| 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 | + |
42 | 49 | /// 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)] |
44 | 51 | pub struct ExtendedId(u32);
|
45 | 52 |
|
46 | 53 | impl ExtendedId {
|
@@ -84,6 +91,13 @@ impl ExtendedId {
|
84 | 91 | }
|
85 | 92 | }
|
86 | 93 |
|
| 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 | + |
87 | 101 | /// A CAN Identifier (standard or extended).
|
88 | 102 | #[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
|
89 | 103 | pub enum Id {
|
@@ -157,4 +171,20 @@ mod tests {
|
157 | 171 | StandardId::new((ExtendedId::MAX.0 >> 18) as u16)
|
158 | 172 | );
|
159 | 173 | }
|
| 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 | + } |
160 | 190 | }
|
0 commit comments