Trait not_empty::IteratorExt
source · [−]pub trait IteratorExt: Iterator {
unsafe fn collect_nonempty_unchecked(self) -> NonEmptyVec<Self::Item>ⓘNotable traits for NonEmptyVec<u8>impl Write for NonEmptyVec<u8>
where
Self: Sized,
{ ... }
fn collect_nonempty(self) -> Result<NonEmptyVec<Self::Item>, EmptyError>
where
Self: Sized,
{ ... }
}Expand description
Extends Iterator with methods for collecting NonEmptyVecs as a
stop-gap to retain a failable Iterator::collect functionality.
Unlike Iterator::collect, the use of the turbofish operator is likely
not required as the method only collects into a NonEmptyVec.
Provided Methods
sourceunsafe fn collect_nonempty_unchecked(self) -> NonEmptyVec<Self::Item>ⓘNotable traits for NonEmptyVec<u8>impl Write for NonEmptyVec<u8>where
Self: Sized,
unsafe fn collect_nonempty_unchecked(self) -> NonEmptyVec<Self::Item>ⓘNotable traits for NonEmptyVec<u8>impl Write for NonEmptyVec<u8>where
Self: Sized,
Available on (crate features
alloc or std) and non-no_global_oom_handling only.Transforms an iterator into a NonEmptyVec<Self::Item> without
validating if the iterator has items.
If you’re not certain that the iterator won’t be empty, use
IteratorExt::collect_nonempty instead.
Safety
The iterator must not be empty.
Examples
use not_empty::{IteratorExt, NonEmptyVec};
let array = [1, 2, 3];
unsafe {
let doubled = array
.iter()
.map(|&x| x * 2)
.collect_nonempty_unchecked();
assert_eq!(doubled, not_empty::vec![2, 4, 6]);
}sourcefn collect_nonempty(self) -> Result<NonEmptyVec<Self::Item>, EmptyError>where
Self: Sized,
fn collect_nonempty(self) -> Result<NonEmptyVec<Self::Item>, EmptyError>where
Self: Sized,
Available on (crate features
alloc or std) and non-no_global_oom_handling only.Transforms an iterator into a NonEmptyVec<Self::Item>.
Errors
Returns an EmptyError if the iterator yields no values.
Examples
use not_empty::IteratorExt;
let array = [1, 2, 3];
let doubled = array.iter().map(|&x| x * 2).collect_nonempty()?;
assert_eq!(doubled, not_empty::vec![2, 4, 6]);