diff options
| author | EFanZh <efanzh@gmail.com> | 2023-07-15 19:20:41 +0800 |
|---|---|---|
| committer | EFanZh <efanzh@gmail.com> | 2023-07-16 20:57:47 +0800 |
| commit | 27e10e2b5e2a589196c3d72f5da58f480ad14374 (patch) | |
| tree | c888c5d9dbbbe6fb72c7503f3e59245808bb66b2 /library/alloc/src | |
| parent | 55be59d2cefe33529a07b0e011384658c9240035 (diff) | |
| download | rust-27e10e2b5e2a589196c3d72f5da58f480ad14374.tar.gz rust-27e10e2b5e2a589196c3d72f5da58f480ad14374.zip | |
Implement `From<{&,&mut} [T; N]>` for `Vec<T>`
Diffstat (limited to 'library/alloc/src')
| -rw-r--r-- | library/alloc/src/vec/mod.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 598ecf05e82..d42c8720e6b 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -3102,6 +3102,36 @@ impl<T: Clone> From<&mut [T]> for Vec<T> { } #[cfg(not(no_global_oom_handling))] +#[stable(feature = "vec_from_array_ref", since = "CURRENT_RUSTC_VERSION")] +impl<T: Clone, const N: usize> From<&[T; N]> for Vec<T> { + /// Allocate a `Vec<T>` and fill it by cloning `s`'s items. + /// + /// # Examples + /// + /// ``` + /// assert_eq!(Vec::from(&[1, 2, 3]), vec![1, 2, 3]); + /// ``` + fn from(s: &[T; N]) -> Vec<T> { + Self::from(s.as_slice()) + } +} + +#[cfg(not(no_global_oom_handling))] +#[stable(feature = "vec_from_array_ref", since = "CURRENT_RUSTC_VERSION")] +impl<T: Clone, const N: usize> From<&mut [T; N]> for Vec<T> { + /// Allocate a `Vec<T>` and fill it by cloning `s`'s items. + /// + /// # Examples + /// + /// ``` + /// assert_eq!(Vec::from(&mut [1, 2, 3]), vec![1, 2, 3]); + /// ``` + fn from(s: &mut [T; N]) -> Vec<T> { + Self::from(s.as_mut_slice()) + } +} + +#[cfg(not(no_global_oom_handling))] #[stable(feature = "vec_from_array", since = "1.44.0")] impl<T, const N: usize> From<[T; N]> for Vec<T> { /// Allocate a `Vec<T>` and move `s`'s items into it. |
