|
14 | 14 |
|
15 | 15 | //! Types for expressing CBOR values. |
16 | 16 |
|
17 | | -use super::writer::write; |
18 | 17 | use alloc::boxed::Box; |
19 | 18 | use alloc::string::{String, ToString}; |
20 | 19 | use alloc::vec::Vec; |
@@ -124,16 +123,34 @@ impl Ord for Value { |
124 | 123 | (ByteString(b1), ByteString(b2)) => b1.len().cmp(&b2.len()).then(b1.cmp(b2)), |
125 | 124 | (TextString(t1), TextString(t2)) => t1.len().cmp(&t2.len()).then(t1.cmp(t2)), |
126 | 125 | (Array(a1), Array(a2)) if a1.len() != a2.len() => a1.len().cmp(&a2.len()), |
| 126 | + (Array(a1), Array(a2)) => { |
| 127 | + // Arrays of same length. |
| 128 | + let mut ordering = Ordering::Equal; |
| 129 | + for (e1, e2) in a1.iter().zip(a2.iter()) { |
| 130 | + ordering = e1.cmp(e2); |
| 131 | + if !matches!(ordering, Ordering::Equal) { |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + ordering |
| 136 | + } |
127 | 137 | (Map(m1), Map(m2)) if m1.len() != m2.len() => m1.len().cmp(&m2.len()), |
| 138 | + (Map(m1), Map(m2)) => { |
| 139 | + // Maps of same length. |
| 140 | + let mut ordering = Ordering::Equal; |
| 141 | + for ((k1, v1), (k2, v2)) in m1.iter().zip(m2.iter()) { |
| 142 | + ordering = k1.cmp(k2).then_with(|| v1.cmp(v2)); |
| 143 | + if !matches!(ordering, Ordering::Equal) { |
| 144 | + break; |
| 145 | + } |
| 146 | + } |
| 147 | + ordering |
| 148 | + } |
128 | 149 | (Tag(t1, v1), Tag(t2, v2)) => t1.cmp(t2).then(v1.cmp(v2)), |
129 | 150 | (Simple(s1), Simple(s2)) => s1.cmp(s2), |
130 | | - (v1, v2) => { |
131 | | - // This case could handle all of the above as well. Checking individually is faster. |
132 | | - let mut encoding1 = Vec::new(); |
133 | | - let _ = write(v1.clone(), &mut encoding1); |
134 | | - let mut encoding2 = Vec::new(); |
135 | | - let _ = write(v2.clone(), &mut encoding2); |
136 | | - encoding1.cmp(&encoding2) |
| 151 | + (_, _) => { |
| 152 | + // The case of different major types is caught above. |
| 153 | + unreachable!(); |
137 | 154 | } |
138 | 155 | } |
139 | 156 | } |
|
0 commit comments