@@ -2737,6 +2737,95 @@ impl<T> Option<Option<T>> {
27372737 }
27382738}
27392739
2740+ impl < ' a , T > Option < & ' a Option < T > > {
2741+ /// Converts from `Option<&Option<T>>` to `Option<&T>`.
2742+ ///
2743+ /// # Examples
2744+ ///
2745+ /// Basic usage:
2746+ ///
2747+ /// ```
2748+ /// #![feature(option_reference_flattening)]
2749+ ///
2750+ /// let x: Option<&Option<u32>> = Some(&Some(6));
2751+ /// assert_eq!(Some(&6), x.flatten_ref());
2752+ ///
2753+ /// let x: Option<&Option<u32>> = Some(&None);
2754+ /// assert_eq!(None, x.flatten_ref());
2755+ ///
2756+ /// let x: Option<&Option<u32>> = None;
2757+ /// assert_eq!(None, x.flatten_ref());
2758+ /// ```
2759+ #[ inline]
2760+ #[ unstable( feature = "option_reference_flattening" , issue = "149221" ) ]
2761+ pub const fn flatten_ref ( self ) -> Option < & ' a T > {
2762+ match self {
2763+ Some ( inner) => inner. as_ref ( ) ,
2764+ None => None ,
2765+ }
2766+ }
2767+ }
2768+
2769+ impl < ' a , T > Option < & ' a mut Option < T > > {
2770+ /// Converts from `Option<&mut Option<T>>` to `&Option<T>`.
2771+ ///
2772+ /// # Examples
2773+ ///
2774+ /// Basic usage:
2775+ ///
2776+ /// ```
2777+ /// #![feature(option_reference_flattening)]
2778+ ///
2779+ /// let y = &mut Some(6);
2780+ /// let x: Option<&mut Option<u32>> = Some(y);
2781+ /// assert_eq!(Some(&6), x.flatten_ref());
2782+ ///
2783+ /// let y: &mut Option<u32> = &mut None;
2784+ /// let x: Option<&mut Option<u32>> = Some(y);
2785+ /// assert_eq!(None, x.flatten_ref());
2786+ ///
2787+ /// let x: Option<&mut Option<u32>> = None;
2788+ /// assert_eq!(None, x.flatten_ref());
2789+ /// ```
2790+ #[ inline]
2791+ #[ unstable( feature = "option_reference_flattening" , issue = "149221" ) ]
2792+ pub const fn flatten_ref ( self ) -> Option < & ' a T > {
2793+ match self {
2794+ Some ( inner) => inner. as_ref ( ) ,
2795+ None => None ,
2796+ }
2797+ }
2798+
2799+ /// Converts from `Option<&mut Option<T>>` to `Option<&mut T>`.
2800+ ///
2801+ /// # Examples
2802+ ///
2803+ /// Basic usage:
2804+ ///
2805+ /// ```
2806+ /// #![feature(option_reference_flattening)]
2807+ ///
2808+ /// let y: &mut Option<u32> = &mut Some(6);
2809+ /// let x: Option<&mut Option<u32>> = Some(y);
2810+ /// assert_eq!(Some(&mut 6), x.flatten_mut());
2811+ ///
2812+ /// let y: &mut Option<u32> = &mut None;
2813+ /// let x: Option<&mut Option<u32>> = Some(y);
2814+ /// assert_eq!(None, x.flatten_mut());
2815+ ///
2816+ /// let x: Option<&mut Option<u32>> = None;
2817+ /// assert_eq!(None, x.flatten_mut());
2818+ /// ```
2819+ #[ inline]
2820+ #[ unstable( feature = "option_reference_flattening" , issue = "149221" ) ]
2821+ pub const fn flatten_mut ( self ) -> Option < & ' a mut T > {
2822+ match self {
2823+ Some ( inner) => inner. as_mut ( ) ,
2824+ None => None ,
2825+ }
2826+ }
2827+ }
2828+
27402829impl < T , const N : usize > [ Option < T > ; N ] {
27412830 /// Transposes a `[Option<T>; N]` into a `Option<[T; N]>`.
27422831 ///
0 commit comments