Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions der/src/arrayvec.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -121,4 +121,11 @@ impl<'a, T> Iterator for Iter<'a, T> {
None
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.elements.len() - self.position;
(len, Some(len))
}
}

impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
28 changes: 7 additions & 21 deletions der/src/asn1/sequence_of.rs
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
//! ASN.1 `SEQUENCE OF` support.

use crate::{
arrayvec, ArrayVec, Decodable, DecodeValue, Decoder, DerOrd, Encodable, EncodeValue, Encoder,
ErrorKind, FixedTag, Length, Result, Tag, ValueOrd,
arrayvec, ord::iter_cmp, ArrayVec, Decodable, DecodeValue, Decoder, DerOrd, Encodable,
EncodeValue, Encoder, ErrorKind, FixedTag, Length, Result, Tag, ValueOrd,
};
use core::cmp::Ordering;

Expand DownExpand Up@@ -109,7 +109,7 @@ where
T: DerOrd,
{
fn value_cmp(&self, other: &Self) -> Result<Ordering> {
value_cmp(self.iter(), other.iter())
iter_cmp(self.iter(), other.iter())
}
}

Expand All@@ -128,6 +128,8 @@ impl<'a, T> Iterator for SequenceOfIter<'a, T> {
}
}

impl<'a, T> ExactSizeIterator for SequenceOfIter<'a, T> {}

impl<'a, T, const N: usize> DecodeValue<'a> for [T; N]
where
T: Decodable<'a>,
Expand DownExpand Up@@ -166,7 +168,7 @@ where
T: DerOrd,
{
fn value_cmp(&self, other: &Self) -> Result<Ordering> {
value_cmp(self.iter(), other.iter())
iter_cmp(self.iter(), other.iter())
}
}

Expand DownExpand Up@@ -225,22 +227,6 @@ where
T: DerOrd,
{
fn value_cmp(&self, other: &Self) -> Result<Ordering> {
value_cmp(self.iter(), other.iter())
iter_cmp(self.iter(), other.iter())
}
}

/// Compare two `SEQUENCE OF`s by value.
fn value_cmp<'a, I, T: 'a>(a: I, b: I) -> Result<Ordering>
where
I: Iterator<Item = &'a T>,
T: DerOrd,
{
for (value1, value2) in a.zip(b) {
match value1.der_cmp(value2)? {
Ordering::Equal => (),
other => return Ok(other),
}
}

Ok(Ordering::Equal)
}
26 changes: 24 additions & 2 deletions der/src/asn1/set_of.rs
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
//! ASN.1 `SET OF` support.

use crate::{
arrayvec, ArrayVec, Decodable, DecodeValue, Decoder, DerOrd, Encodable, EncodeValue, Encoder,
ErrorKind, FixedTag, Length, Result, Tag,
arrayvec, ord::iter_cmp, ArrayVec, Decodable, DecodeValue, Decoder, DerOrd, Encodable,
EncodeValue, Encoder, ErrorKind, FixedTag, Length, Result, Tag, ValueOrd,
};
use core::cmp::Ordering;

Expand DownExpand Up@@ -126,6 +126,15 @@ where
const TAG: Tag = Tag::Set;
}

impl<T, const N: usize> ValueOrd for SetOf<T, N>
where
T: Clone + DerOrd,
{
fn value_cmp(&self, other: &Self) -> Result<Ordering> {
iter_cmp(self.iter(), other.iter())
}
}

/// Iterator over the elements of an [`SetOf`].
#[derive(Clone, Debug)]
pub struct SetOfIter<'a, T> {
Expand All@@ -141,6 +150,8 @@ impl<'a, T> Iterator for SetOfIter<'a, T> {
}
}

impl<'a, T> ExactSizeIterator for SetOfIter<'a, T> {}

/// ASN.1 `SET OF` backed by a [`Vec`].
///
/// This type implements an append-only `SET OF` type which is heap-backed
Expand DownExpand Up@@ -255,3 +266,14 @@ where
{
const TAG: Tag = Tag::Set;
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<T> ValueOrd for SetOfVec<T>
where
T: Clone + DerOrd,
{
fn value_cmp(&self, other: &Self) -> Result<Ordering> {
iter_cmp(self.iter(), other.iter())
}
}
18 changes: 18 additions & 0 deletions der/src/ord.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,3 +51,21 @@ where
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)
}