From 44c670d44237fedb5043250193c39728450f0537 Mon Sep 17 00:00:00 2001 From: "Chai T. Rex" Date: Wed, 29 Dec 2021 12:32:12 -0500 Subject: [PATCH 1/3] Add as_slice and as_mut_slice to Option Extracts an immutable or mutable slice from an option such that, if the option is a None value, the slice is empty, and if the option is a Some value, the slice is length one. The slice is, for all practical purposes except extracting a pointer, a reference to the actual contents of the owned option. If it's a Some value, slice index zero contains the contents of the owned option. If it's a None value, the slice is length zero and cannot be read from or written to. --- library/core/src/option.rs | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index e6312b8b2d947..40811da6e6bff 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -690,6 +690,74 @@ impl Option { } } + ///////////////////////////////////////////////////////////////////////// + // 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 = Some(7); + /// assert_eq!(x.as_slice(), [7]); + /// + /// let x: Option = None; + /// assert_eq!(x.as_slice(), []); + /// ``` + #[inline] + #[unstable(feature = "option_as_slice", issue = "none")] + pub 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 = 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 = 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 ///////////////////////////////////////////////////////////////////////// From ac9bd888be8644315c0c0bcdec421dcf67f2373e Mon Sep 17 00:00:00 2001 From: "Chai T. Rex" Date: Wed, 29 Dec 2021 14:38:01 -0500 Subject: [PATCH 2/3] Added const to as_slice method of Option --- library/core/src/option.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 40811da6e6bff..86a5b69057888 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -713,7 +713,7 @@ impl Option { /// ``` #[inline] #[unstable(feature = "option_as_slice", issue = "none")] - pub fn as_slice(&self) -> &[T] { + pub const fn as_slice(&self) -> &[T] { match *self { Some(ref x) => core::slice::from_ref(x), None => &[], From 02d4462d292761cc26596334a7e6fdf333e87da0 Mon Sep 17 00:00:00 2001 From: "Chai T. Rex" Date: Wed, 29 Dec 2021 18:53:26 -0500 Subject: [PATCH 3/3] Formatted rustdoc comments on Option's as_slice and as_mut_slice --- library/core/src/option.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 86a5b69057888..f6997fb962ebd 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -694,11 +694,12 @@ impl Option { // 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. + /// 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. + /// Note that the slice extracted from a [`None`] value does not + /// necessarily contain an internal pointer to anything associated with + /// the option. /// /// # Examples /// @@ -720,11 +721,12 @@ impl Option { } } - /// Extracts a mutable slice that's empty if the option is a `None` value - /// or length one if the option is a `Some` value. + /// 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. + /// Note that the slice extracted from a [`None`] value does not + /// necessarily contain an internal pointer to anything associated with + /// the option. /// /// # Examples ///