Skip to content

Commit dc3ab70

Browse files
committed
add Option::into_flat_iter
1 parent e5efc33 commit dc3ab70

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

library/core/src/option.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,30 @@ impl<T> Option<T> {
20072007
}
20082008
}
20092009

2010+
impl<T: IntoIterator> Option<T> {
2011+
/// Transforms an optional iterator into an iterator.
2012+
///
2013+
/// If `self` is `None`, the resulting iterator is empty.
2014+
/// Otherwise, an iterator is made from the `Some` value and returned.
2015+
/// # Examples
2016+
/// ```
2017+
/// #![feature(option_into_flat_iter)]
2018+
///
2019+
/// let o1 = Some([1, 2]);
2020+
/// let o2 = None::<&[usize]>;
2021+
///
2022+
/// assert_eq!(o1.into_flat_iter().collect::<Vec<_>>(), [1, 2]);
2023+
/// assert_eq!(o2.into_flat_iter().collect::<Vec<_>>(), Vec::<&usize>::new());
2024+
/// ```
2025+
#[unstable(feature = "option_into_flat_iter", issue = "148441")]
2026+
pub fn into_flat_iter<A>(self) -> OptionFlatten<A>
2027+
where
2028+
T: IntoIterator<IntoIter = A>,
2029+
{
2030+
OptionFlatten { iter: self.map(IntoIterator::into_iter) }
2031+
}
2032+
}
2033+
20102034
impl<T, U> Option<(T, U)> {
20112035
/// Unzips an option containing a tuple of two options.
20122036
///
@@ -2569,6 +2593,42 @@ impl<A> FusedIterator for IntoIter<A> {}
25692593
#[unstable(feature = "trusted_len", issue = "37572")]
25702594
unsafe impl<A> TrustedLen for IntoIter<A> {}
25712595

2596+
/// The iterator produced by [`Option::into_flat_iter`]. See its documentation for more.
2597+
#[derive(Clone, Debug)]
2598+
#[unstable(feature = "option_into_flat_iter", issue = "148441")]
2599+
pub struct OptionFlatten<A> {
2600+
iter: Option<A>,
2601+
}
2602+
2603+
#[unstable(feature = "option_into_flat_iter", issue = "148441")]
2604+
impl<A: Iterator> Iterator for OptionFlatten<A> {
2605+
type Item = A::Item;
2606+
2607+
fn next(&mut self) -> Option<Self::Item> {
2608+
self.iter.as_mut()?.next()
2609+
}
2610+
2611+
fn size_hint(&self) -> (usize, Option<usize>) {
2612+
self.iter.as_ref().map(|i| i.size_hint()).unwrap_or((0, Some(0)))
2613+
}
2614+
}
2615+
2616+
#[unstable(feature = "option_into_flat_iter", issue = "148441")]
2617+
impl<A: DoubleEndedIterator> DoubleEndedIterator for OptionFlatten<A> {
2618+
fn next_back(&mut self) -> Option<Self::Item> {
2619+
self.iter.as_mut()?.next_back()
2620+
}
2621+
}
2622+
2623+
#[unstable(feature = "option_into_flat_iter", issue = "148441")]
2624+
impl<A: ExactSizeIterator> ExactSizeIterator for OptionFlatten<A> {}
2625+
2626+
#[unstable(feature = "option_into_flat_iter", issue = "148441")]
2627+
impl<A: FusedIterator> FusedIterator for OptionFlatten<A> {}
2628+
2629+
#[unstable(feature = "option_into_flat_iter", issue = "148441")]
2630+
unsafe impl<A: TrustedLen> TrustedLen for OptionFlatten<A> {}
2631+
25722632
/////////////////////////////////////////////////////////////////////////////
25732633
// FromIterator
25742634
/////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)