@@ -85,7 +85,7 @@ impl ExtendedId {
85
85
}
86
86
87
87
/// A CAN Identifier (standard or extended).
88
- #[ derive( Debug , Copy , Clone , Eq , PartialEq , PartialOrd , Ord , Hash ) ]
88
+ #[ derive( Debug , Copy , Clone , Eq , PartialEq , Hash ) ]
89
89
pub enum Id {
90
90
/// Standard 11-bit Identifier (`0..=0x7FF`).
91
91
Standard ( StandardId ) ,
@@ -94,6 +94,30 @@ pub enum Id {
94
94
Extended ( ExtendedId ) ,
95
95
}
96
96
97
+ impl Ord for Id {
98
+ fn cmp ( & self , other : & Self ) -> core:: cmp:: Ordering {
99
+ let split_id = |id : & Id | {
100
+ let ( standard_id_part, ide_bit, extended_id_part) = match id {
101
+ Id :: Standard ( StandardId ( x) ) => ( * x, 0 , 0 ) ,
102
+ Id :: Extended ( x) => (
103
+ x. standard_id ( ) . 0 ,
104
+ 1 ,
105
+ x. 0 & ( ( 1 << 18 ) - 1 ) , // Bit ID-17 to ID-0
106
+ ) ,
107
+ } ;
108
+ ( standard_id_part, ide_bit, extended_id_part)
109
+ } ;
110
+
111
+ split_id ( self ) . cmp ( & split_id ( other) )
112
+ }
113
+ }
114
+
115
+ impl PartialOrd for Id {
116
+ fn partial_cmp ( & self , other : & Id ) -> Option < core:: cmp:: Ordering > {
117
+ Some ( self . cmp ( other) )
118
+ }
119
+ }
120
+
97
121
impl From < StandardId > for Id {
98
122
#[ inline]
99
123
fn from ( id : StandardId ) -> Self {
@@ -157,4 +181,20 @@ mod tests {
157
181
StandardId :: new( ( ExtendedId :: MAX . 0 >> 18 ) as u16 )
158
182
) ;
159
183
}
184
+
185
+ #[ test]
186
+ fn cmp_standard ( ) {
187
+ assert ! ( StandardId :: MAX < StandardId :: ZERO ) ;
188
+ }
189
+
190
+ #[ test]
191
+ fn cmp_extended ( ) {
192
+ assert ! ( ExtendedId :: MAX < ExtendedId :: ZERO ) ;
193
+ }
194
+
195
+ #[ test]
196
+ fn cmp_id ( ) {
197
+ assert ! ( Id :: Standard ( StandardId :: MAX ) < Id :: Standard ( StandardId :: ZERO ) ) ;
198
+ assert ! ( Id :: Extended ( ExtendedId :: MAX ) < Id :: Extended ( ExtendedId :: ZERO ) ) ;
199
+ }
160
200
}
0 commit comments