Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/der/src/ord.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / der / src / ord.rs` 85 lines ยท 2306 bytes Raw Blame History 1 //! Ordering trait. 2 3 use crate::{EncodeValue, Result, Tagged}; 4 use core::{cmp::Ordering, marker::PhantomData}; 5 6 /// DER ordering trait. 7 /// 8 /// Compares the ordering of two values based on their ASN.1 DER 9 /// serializations. 10 /// 11 /// This is used by the DER encoding for `SET OF` in order to establish an 12 /// ordering for the elements of sets. 13 pub trait DerOrd { 14 /// Return an [`Ordering`] between `self` and `other` when serialized as 15 /// ASN.1 DER. 16 fn der_cmp(&self, other: &Self) -> Result; 17 } 18 19 /// DER value ordering trait. 20 /// 21 /// Compares the ordering of the value portion of TLV-encoded DER productions. 22 pub trait ValueOrd { 23 /// Return an [`Ordering`] between value portion of TLV-encoded `self` and 24 /// `other` when serialized as ASN.1 DER. 25 fn value_cmp(&self, other: &Self) -> Result; 26 } 27 28 impl DerOrd for T 29 where 30 T: EncodeValue + ValueOrd + Tagged, 31 { 32 fn der_cmp(&self, other: &Self) -> Result { 33 match self.header()?.der_cmp(&other.header()?)? { 34 Ordering::Equal => self.value_cmp(other), 35 ordering => Ok(ordering), 36 } 37 } 38 } 39 40 /// Marker trait for types whose `Ord` impl can be used as `ValueOrd`. 41 /// 42 /// This means the `Ord` impl will sort values in the same order as their DER 43 /// encodings. 44 pub trait OrdIsValueOrd: Ord {} 45 46 impl ValueOrd for T 47 where 48 T: OrdIsValueOrd, 49 { 50 fn value_cmp(&self, other: &Self) -> Result { 51 Ok(self.cmp(other)) 52 } 53 } 54 55 /// Compare the order of two iterators using [`DerCmp`] on the values. 56 pub(crate) fn iter_cmp<'a, I, T: 'a>(a: I, b: I) -> Result 57 where 58 I: Iterator + ExactSizeIterator, 59 T: DerOrd, 60 { 61 let length_ord = a.len().cmp(&b.len()); 62 63 for (value1, value2) in a.zip(b) { 64 match value1.der_cmp(value2)? { 65 Ordering::Equal => (), 66 other => return Ok(other), 67 } 68 } 69 70 Ok(length_ord) 71 } 72 73 /// Provide a no-op implementation for PhantomData 74 impl ValueOrd for PhantomData { 75 fn value_cmp(&self, _other: &Self) -> Result { 76 Ok(Ordering::Equal) 77 } 78 } 79 80 /// Provide a no-op implementation for PhantomData 81 impl DerOrd for PhantomData { 82 fn der_cmp(&self, _other: &Self) -> Result { 83 Ok(Ordering::Equal) 84 } 85 }