Skip to content

Implement flatten for Option<&Option<T>> and Option<&mut Option<T>> #108671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
91 changes: 91 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2606,3 +2606,94 @@ impl<T> Option<Option<T>> {
}
}
}

impl<'a, T> Option<&'a Option<T>> {
/// Converts from `Option<&Option<T>>` to `Option<&T>`.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(option_reference_flattening)]
///
/// let x: Option<&Option<u32>> = Some(&Some(6));
/// assert_eq!(&Some(6), x.flatten_ref());
///
/// let x: Option<&Option<u32>> = Some(&None);
/// assert_eq!(&None, x.flatten_ref());
///
/// let x: Option<&Option<u32>> = None;
/// assert_eq!(&None, x.flatten_ref());
/// ```
#[inline]
#[unstable(feature = "option_reference_flattening", issue = "none")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
pub const fn flatten_ref(self) -> &'a Option<T> {
match self {
Some(inner) => inner,
None => const { &None },
}
}
}

impl<'a, T> Option<&'a mut Option<T>> {
/// Converts from `Option<&mut Option<T>>` to `&Option<T>`.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(option_reference_flattening)]
///
/// let y = &mut Some(6);
/// let x: Option<&mut Option<u32>> = Some(y);
/// assert_eq!(&Some(6), x.flatten_ref());
///
/// let y: &mut Option<u32> = &mut None;
/// let x: Option<&mut Option<u32>> = Some(y);
/// assert_eq!(&None, x.flatten_ref());
///
/// let x: Option<&mut Option<u32>> = None;
/// assert_eq!(&None, x.flatten_ref());
/// ```
#[unstable(feature = "option_reference_flattening", issue = "none")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
pub const fn flatten_ref(self) -> &'a Option<T> {
match self {
Some(inner) => inner,
None => const { &None },
}
}

/// Converts from `Option<&mut Option<T>>` to `Option<&mut T>`.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(option_reference_flattening)]
///
/// let y: &mut Option<u32> = &mut Some(6);
/// let x: Option<&mut Option<u32>> = Some(y);
/// assert_eq!(Some(&mut 6), x.flatten_mut());
///
/// let y: &mut Option<u32> = &mut None;
/// let x: Option<&mut Option<u32>> = Some(y);
/// assert_eq!(None, x.flatten_mut());
///
/// let x: Option<&mut Option<u32>> = None;
/// assert_eq!(None, x.flatten_mut());
/// ```
#[inline]
#[unstable(feature = "option_reference_flattening", issue = "none")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
pub const fn flatten_mut(self) -> Option<&'a mut T> {
match self {
Some(inner) => inner.as_mut(),
None => None,
}
}
}