Skip to content

Commit 5dd7027

Browse files
committed
impl From<&[T; N]> and From<&mut [T; N]> for Vec<T>
1 parent a8adf76 commit 5dd7027

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

library/alloc/src/vec/mod.rs

+42
Original file line numberDiff line numberDiff line change
@@ -2929,6 +2929,48 @@ impl<T, const N: usize> From<[T; N]> for Vec<T> {
29292929
}
29302930
}
29312931

2932+
#[cfg(not(no_global_oom_handling))]
2933+
#[stable(feature = "vec_from_array_ref", since = "1.61.0")]
2934+
impl<T: Clone, const N: usize> From<&[T; N]> for Vec<T> {
2935+
/// Allocate a `Vec<T>` and fill it by cloning `s`'s items.
2936+
///
2937+
/// # Examples
2938+
///
2939+
/// ```
2940+
/// assert_eq!(Vec::from(b"raw"), vec![b'r', b'a', b'w']);
2941+
/// ```
2942+
#[cfg(not(test))]
2943+
fn from(s: &[T; N]) -> Vec<T> {
2944+
s.to_vec()
2945+
}
2946+
2947+
#[cfg(test)]
2948+
fn from(s: &[T; N]) -> Vec<T> {
2949+
crate::slice::to_vec(s, Global)
2950+
}
2951+
}
2952+
2953+
#[cfg(not(no_global_oom_handling))]
2954+
#[stable(feature = "vec_from_array_ref", since = "1.61.0")]
2955+
impl<T: Clone, const N: usize> From<&mut [T; N]> for Vec<T> {
2956+
/// Allocate a `Vec<T>` and fill it by cloning `s`'s items.
2957+
///
2958+
/// # Examples
2959+
///
2960+
/// ```
2961+
/// assert_eq!(Vec::from(&mut [1, 2, 3]), vec![1, 2, 3]);
2962+
/// ```
2963+
#[cfg(not(test))]
2964+
fn from(s: &mut [T; N]) -> Vec<T> {
2965+
s.to_vec()
2966+
}
2967+
2968+
#[cfg(test)]
2969+
fn from(s: &mut [T; N]) -> Vec<T> {
2970+
crate::slice::to_vec(s, Global)
2971+
}
2972+
}
2973+
29322974
#[stable(feature = "vec_from_cow_slice", since = "1.14.0")]
29332975
impl<'a, T> From<Cow<'a, [T]>> for Vec<T>
29342976
where

0 commit comments

Comments
 (0)