Skip to content
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

Add as_slice and as_mut_slice to Option #92411

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,76 @@ impl<T> Option<T> {
}
}

/////////////////////////////////////////////////////////////////////////
// Extracting slices
/////////////////////////////////////////////////////////////////////////

/// Extracts a slice that's empty if the option is a [`None`] value or
/// length one if the option is a [`Some`] value.
///
/// Note that the slice extracted from a [`None`] value does not
/// necessarily contain an internal pointer to anything associated with
/// the option.
///
/// # Examples
///
/// ```
/// #![feature(option_as_slice)]
///
/// let x: Option<u8> = Some(7);
/// assert_eq!(x.as_slice(), [7]);
///
/// let x: Option<u8> = None;
/// assert_eq!(x.as_slice(), []);
/// ```
#[inline]
#[unstable(feature = "option_as_slice", issue = "none")]
pub const fn as_slice(&self) -> &[T] {
match *self {
Some(ref x) => core::slice::from_ref(x),
None => &[],
}
}

/// Extracts a mutable slice that's empty if the option is a [`None`]
/// value or length one if the option is a [`Some`] value.
///
/// Note that the slice extracted from a [`None`] value does not
/// necessarily contain an internal pointer to anything associated with
/// the option.
///
/// # Examples
///
/// ```
/// #![feature(option_as_slice)]
///
/// let mut x: Option<u8> = Some(2);
/// let x_as_slice = x.as_mut_slice();
/// assert_eq!(x_as_slice, [2]);
///
/// if !x_as_slice.is_empty() {
/// x_as_slice[0] = 42;
/// }
/// assert_eq!(x, Some(42));
///
/// let mut x: Option<u8> = None;
/// let x_as_slice = x.as_mut_slice();
/// assert_eq!(x_as_slice, []);
///
/// if !x_as_slice.is_empty() {
/// x_as_slice[0] = 42;
/// }
/// assert_eq!(x, None);
/// ```
#[inline]
#[unstable(feature = "option_as_slice", issue = "none")]
pub const fn as_mut_slice(&mut self) -> &mut [T] {
match *self {
Some(ref mut x) => core::slice::from_mut(x),
None => &mut [],
}
}

/////////////////////////////////////////////////////////////////////////
// Getting to contained values
/////////////////////////////////////////////////////////////////////////
Expand Down