From b76dda014fb5f036cb4fdebe987ef6e5d9c0bf40 Mon Sep 17 00:00:00 2001 From: minghuaw Date: Sun, 26 May 2024 03:56:10 -0700 Subject: [PATCH 1/2] initial attempt at From<& [T;N]> for Option<& [T]> --- library/core/src/option.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 1e3ed0f7c49f1..3a19b776f43c3 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -2096,6 +2096,22 @@ impl From for Option { } } +impl<'a, T, const N: usize> From<&'a [T; N]> for Option<&'a [T]> { + /// Converts a reference to an array into a reference to an array slice. + /// + /// # Examples + /// + /// ``` + /// let array = [1, 2, 3]; + /// let option: Option<&[i32]> = Option::from(&array); + /// + /// assert_eq!(option, Some(&[1, 2, 3][..])); + /// ``` + fn from(slice: &'a [T; N]) -> Option<&'a [T]> { + Some(slice.as_slice()) + } +} + #[stable(feature = "option_ref_from_ref_option", since = "1.30.0")] impl<'a, T> From<&'a Option> for Option<&'a T> { /// Converts from `&Option` to `Option<&T>`. From b923145089cbff4b3012a64a3bd4370fbc284d2a Mon Sep 17 00:00:00 2001 From: minghuaw Date: Mon, 27 May 2024 02:32:01 -0700 Subject: [PATCH 2/2] added stable attribute --- library/core/src/option.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 3a19b776f43c3..7c582e767832d 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -2096,6 +2096,7 @@ impl From for Option { } } +#[stable(feature = "option_slice_from_array_ref", since = "CURRENT_RUSTC_VERSION")] impl<'a, T, const N: usize> From<&'a [T; N]> for Option<&'a [T]> { /// Converts a reference to an array into a reference to an array slice. ///