@@ -2008,6 +2008,30 @@ impl<T> Option<T> {
20082008 }
20092009}
20102010
2011+ impl <T : IntoIterator > Option < T > {
2012+ /// Transforms an optional iterator into an iterator.
2013+ ///
2014+ /// If `self` is `None`, the resulting iterator is empty.
2015+ /// Otherwise, an iterator is made from the `Some` value and returned.
2016+ /// # Examples
2017+ /// ```
2018+ /// #![feature(option_into_flat_iter)]
2019+ ///
2020+ /// let o1 = Some([1, 2]);
2021+ /// let o2 = None::<&[usize]>;
2022+ ///
2023+ /// assert_eq!(o1.into_flat_iter().collect::<Vec<_>>(), [1, 2]);
2024+ /// assert_eq!(o2.into_flat_iter().collect::<Vec<_>>(), Vec::<&usize>::new());
2025+ /// ```
2026+ #[ unstable ( feature = "option_into_flat_iter" , issue = "148441" ) ]
2027+ pub fn into_flat_iter < A > ( self ) -> OptionFlatten < A >
2028+ where
2029+ T : IntoIterator < IntoIter = A > ,
2030+ {
2031+ OptionFlatten { iter : self . map ( IntoIterator :: into_iter ) }
2032+ }
2033+ }
2034+
20112035impl <T , U > Option < ( T , U ) > {
20122036 /// Unzips an option containing a tuple of two options.
20132037 ///
@@ -2575,6 +2599,42 @@ impl<A> FusedIterator for IntoIter<A> {}
25752599#[ unstable( feature = "trusted_len" , issue = "37572" ) ]
25762600unsafe impl < A > TrustedLen for IntoIter < A > { }
25772601
2602+ /// The iterator produced by [`Option::into_flat_iter`]. See its documentation for more.
2603+ #[ derive( Clone , Debug ) ]
2604+ #[ unstable( feature = "option_into_flat_iter" , issue = "148441" ) ]
2605+ pub struct OptionFlatten < A > {
2606+ iter : Option < A > ,
2607+ }
2608+
2609+ #[ unstable( feature = "option_into_flat_iter" , issue = "148441" ) ]
2610+ impl < A : Iterator > Iterator for OptionFlatten < A > {
2611+ type Item = A :: Item ;
2612+
2613+ fn next ( & mut self ) -> Option < Self :: Item > {
2614+ self . iter . as_mut ( ) ?. next ( )
2615+ }
2616+
2617+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2618+ self . iter . as_ref ( ) . map ( |i| i. size_hint ( ) ) . unwrap_or ( ( 0 , Some ( 0 ) ) )
2619+ }
2620+ }
2621+
2622+ #[ unstable( feature = "option_into_flat_iter" , issue = "148441" ) ]
2623+ impl < A : DoubleEndedIterator > DoubleEndedIterator for OptionFlatten < A > {
2624+ fn next_back ( & mut self ) -> Option < Self :: Item > {
2625+ self . iter . as_mut ( ) ?. next_back ( )
2626+ }
2627+ }
2628+
2629+ #[ unstable( feature = "option_into_flat_iter" , issue = "148441" ) ]
2630+ impl < A : ExactSizeIterator > ExactSizeIterator for OptionFlatten < A > { }
2631+
2632+ #[ unstable( feature = "option_into_flat_iter" , issue = "148441" ) ]
2633+ impl < A : FusedIterator > FusedIterator for OptionFlatten < A > { }
2634+
2635+ #[ unstable( feature = "option_into_flat_iter" , issue = "148441" ) ]
2636+ unsafe impl < A : TrustedLen > TrustedLen for OptionFlatten < A > { }
2637+
25782638/////////////////////////////////////////////////////////////////////////////
25792639// FromIterator
25802640/////////////////////////////////////////////////////////////////////////////
0 commit comments