Skip to content

Commit 223f522

Browse files
authored
der: allow SetOf* duplicates (#2272)
Closes #2271
1 parent 3778ae3 commit 223f522

1 file changed

Lines changed: 28 additions & 50 deletions

File tree

der/src/asn1/set_of.rs

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ where
5151
/// Insert an item into this [`SetOf`].
5252
///
5353
/// # Errors
54-
/// If there's a duplicate or sorting error.
54+
/// If there's a sorting error.
5555
pub fn insert(&mut self, item: T) -> Result<(), Error> {
56-
check_duplicate(&item, self.iter())?;
5756
self.try_push(item)?;
5857
der_sort(self.inner.as_mut())
5958
}
@@ -304,9 +303,8 @@ where
304303
/// Insert an item into this [`SetOfVec`]. Must be unique.
305304
///
306305
/// # Errors
307-
/// If `item` is a duplicate or a sorting error occurred.
306+
/// If a sorting error occurred.
308307
pub fn insert(&mut self, item: T) -> Result<(), Error> {
309-
check_duplicate(&item, self.iter())?;
310308
self.inner.push(item);
311309
der_sort(&mut self.inner)
312310
}
@@ -488,29 +486,10 @@ where
488486
}
489487
}
490488

491-
/// Check if the given item is a duplicate, given an iterator over sorted items (which we can
492-
/// short-circuit once we hit `Ordering::Less`.
493-
fn check_duplicate<'a, T, I>(item: &T, iter: I) -> Result<(), Error>
494-
where
495-
T: DerOrd + 'a,
496-
I: Iterator<Item = &'a T>,
497-
{
498-
for item2 in iter {
499-
match item.der_cmp(item2)? {
500-
Ordering::Less => return Ok(()), // all remaining items are greater
501-
Ordering::Equal => return Err(ErrorKind::SetDuplicate.into()),
502-
Ordering::Greater => continue,
503-
}
504-
}
505-
506-
Ok(())
507-
}
508-
509489
/// Ensure set elements are lexicographically ordered using [`DerOrd`].
510490
fn check_der_ordering<T: DerOrd>(a: &T, b: &T) -> Result<(), Error> {
511491
match a.der_cmp(b)? {
512-
Ordering::Less => Ok(()),
513-
Ordering::Equal => Err(ErrorKind::SetDuplicate.into()),
492+
Ordering::Less | Ordering::Equal => Ok(()),
514493
Ordering::Greater => Err(ErrorKind::SetOrdering.into()),
515494
}
516495
}
@@ -531,8 +510,7 @@ fn der_sort<T: DerOrd>(slice: &mut [T]) -> Result<(), Error> {
531510

532511
while j > 0 {
533512
match slice[j - 1].der_cmp(&slice[j])? {
534-
Ordering::Less => break,
535-
Ordering::Equal => return Err(ErrorKind::SetDuplicate.into()),
513+
Ordering::Less | Ordering::Equal => break,
536514
Ordering::Greater => {
537515
slice.swap(j - 1, j);
538516
j -= 1;
@@ -549,7 +527,6 @@ fn der_sort<T: DerOrd>(slice: &mut [T]) -> Result<(), Error> {
549527
mod tests {
550528
#[cfg(feature = "alloc")]
551529
use super::SetOfVec;
552-
use crate::ErrorKind;
553530
#[cfg(feature = "heapless")]
554531
use {super::SetOf, crate::DerOrd};
555532

@@ -560,13 +537,22 @@ mod tests {
560537
setof.insert(42).unwrap();
561538
assert_eq!(setof.len(), 1);
562539
assert_eq!(*setof.iter().next().unwrap(), 42);
540+
}
563541

564-
// Ensure duplicates are disallowed
565-
assert_eq!(
566-
setof.insert(42).unwrap_err().kind(),
567-
ErrorKind::SetDuplicate
568-
);
542+
#[cfg(feature = "heapless")]
543+
#[test]
544+
fn setof_insert_duplicate() {
545+
let mut setof = SetOf::<u8, 10>::new();
546+
setof.insert(42).unwrap();
569547
assert_eq!(setof.len(), 1);
548+
549+
setof.insert(42).unwrap();
550+
551+
let mut iter = setof.iter();
552+
553+
assert_eq!(setof.len(), 2);
554+
assert_eq!(*iter.next().unwrap(), 42);
555+
assert_eq!(*iter.next().unwrap(), 42);
570556
}
571557

572558
#[cfg(feature = "heapless")]
@@ -577,14 +563,6 @@ mod tests {
577563
assert!(set.iter().copied().eq([0, 1, 2, 3, 65535]));
578564
}
579565

580-
#[cfg(feature = "heapless")]
581-
#[test]
582-
fn setof_tryfrom_array_reject_duplicates() {
583-
let arr = [1u16, 1];
584-
let err = SetOf::try_from(arr).err().unwrap();
585-
assert_eq!(err.kind(), ErrorKind::SetDuplicate);
586-
}
587-
588566
#[cfg(feature = "heapless")]
589567
#[test]
590568
fn setof_valueord_value_cmp() {
@@ -603,14 +581,14 @@ mod tests {
603581
let mut setof = SetOfVec::new();
604582
setof.insert(42).unwrap();
605583
assert_eq!(setof.len(), 1);
606-
assert_eq!(*setof.iter().next().unwrap(), 42);
607584

608-
// Ensure duplicates are disallowed
609-
assert_eq!(
610-
setof.insert(42).unwrap_err().kind(),
611-
ErrorKind::SetDuplicate
612-
);
613-
assert_eq!(setof.len(), 1);
585+
setof.insert(46).unwrap();
586+
587+
let mut iter = setof.iter();
588+
589+
assert_eq!(setof.len(), 2);
590+
assert_eq!(*iter.next().unwrap(), 42);
591+
assert_eq!(*iter.next().unwrap(), 46);
614592
}
615593

616594
#[cfg(feature = "alloc")]
@@ -631,9 +609,9 @@ mod tests {
631609

632610
#[cfg(feature = "alloc")]
633611
#[test]
634-
fn setofvec_tryfrom_vec_reject_duplicates() {
612+
fn setofvec_tryfrom_vec_allow_duplicates() {
635613
let vec = vec![1u16, 1];
636-
let err = SetOfVec::try_from(vec).err().unwrap();
637-
assert_eq!(err.kind(), ErrorKind::SetDuplicate);
614+
let set = SetOfVec::try_from(vec).unwrap();
615+
assert_eq!(set.as_ref(), &[1, 1]);
638616
}
639617
}

0 commit comments

Comments
 (0)