Skip to content

Commit b0be47a

Browse files
authored
der: add SetOfVec::{extend, from_iter} (#1065)
Adds inherent methods to `SetOfVec` shaped like the `Extend` and `FromIterator` traits. However, the methods are fallible, so they don't quite fit the existing traits, hence why inherent methods are used instead.
1 parent 16aaafe commit b0be47a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

der/src/asn1/set_of.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,18 @@ where
214214
}
215215
}
216216

217+
/// Create a new [`SetOfVec`] from the given iterator.
218+
///
219+
/// Note: this is an inherent method instead of an impl of the
220+
/// [`FromIterator`] trait in order to be fallible.
221+
#[allow(clippy::should_implement_trait)]
222+
pub fn from_iter<I>(iter: I) -> Result<Self>
223+
where
224+
I: IntoIterator<Item = T>,
225+
{
226+
Vec::from_iter(iter).try_into()
227+
}
228+
217229
/// Add an element to this [`SetOfVec`].
218230
///
219231
/// Items MUST be added in lexicographical order according to the
@@ -230,6 +242,18 @@ where
230242
Ok(())
231243
}
232244

245+
/// Extend a [`SetOfVec`] using an iterator.
246+
///
247+
/// Note: this is an inherent method instead of an impl of the
248+
/// [`Extend`] trait in order to be fallible.
249+
pub fn extend<I>(&mut self, iter: I) -> Result<()>
250+
where
251+
I: IntoIterator<Item = T>,
252+
{
253+
self.inner.extend(iter);
254+
der_sort(&mut self.inner)
255+
}
256+
233257
/// Borrow the elements of this [`SetOfVec`] as a slice.
234258
pub fn as_slice(&self) -> &[T] {
235259
self.inner.as_slice()

0 commit comments

Comments
 (0)