summaryrefslogtreecommitdiffstats
path: root/vendor/der/src/ord.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
commit10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch)
treebdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/der/src/ord.rs
parentReleasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff)
downloadrustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz
rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/der/src/ord.rs')
-rw-r--r--vendor/der/src/ord.rs71
1 files changed, 71 insertions, 0 deletions
diff --git a/vendor/der/src/ord.rs b/vendor/der/src/ord.rs
new file mode 100644
index 000000000..fd967bf88
--- /dev/null
+++ b/vendor/der/src/ord.rs
@@ -0,0 +1,71 @@
+//! Ordering trait.
+
+use crate::{EncodeValue, Result, Tagged};
+use core::cmp::Ordering;
+
+/// DER ordering trait.
+///
+/// Compares the ordering of two values based on their ASN.1 DER
+/// serializations.
+///
+/// This is used by the DER encoding for `SET OF` in order to establish an
+/// ordering for the elements of sets.
+pub trait DerOrd {
+ /// Return an [`Ordering`] between `self` and `other` when serialized as
+ /// ASN.1 DER.
+ fn der_cmp(&self, other: &Self) -> Result<Ordering>;
+}
+
+/// DER value ordering trait.
+///
+/// Compares the ordering of the value portion of TLV-encoded DER productions.
+pub trait ValueOrd {
+ /// Return an [`Ordering`] between value portion of TLV-encoded `self` and
+ /// `other` when serialized as ASN.1 DER.
+ fn value_cmp(&self, other: &Self) -> Result<Ordering>;
+}
+
+impl<T> DerOrd for T
+where
+ T: EncodeValue + ValueOrd + Tagged,
+{
+ fn der_cmp(&self, other: &Self) -> Result<Ordering> {
+ match self.header()?.der_cmp(&other.header()?)? {
+ Ordering::Equal => self.value_cmp(other),
+ ordering => Ok(ordering),
+ }
+ }
+}
+
+/// Marker trait for types whose `Ord` impl can be used as `ValueOrd`.
+///
+/// This means the `Ord` impl will sort values in the same order as their DER
+/// encodings.
+pub trait OrdIsValueOrd: Ord {}
+
+impl<T> ValueOrd for T
+where
+ T: OrdIsValueOrd,
+{
+ fn value_cmp(&self, other: &Self) -> Result<Ordering> {
+ Ok(self.cmp(other))
+ }
+}
+
+/// Compare the order of two iterators using [`DerCmp`] on the values.
+pub(crate) fn iter_cmp<'a, I, T: 'a>(a: I, b: I) -> Result<Ordering>
+where
+ I: Iterator<Item = &'a T> + ExactSizeIterator,
+ T: DerOrd,
+{
+ let length_ord = a.len().cmp(&b.len());
+
+ for (value1, value2) in a.zip(b) {
+ match value1.der_cmp(value2)? {
+ Ordering::Equal => (),
+ other => return Ok(other),
+ }
+ }
+
+ Ok(length_ord)
+}