@@ -479,20 +479,24 @@ impl<T> Vec<T> {
479
479
///
480
480
/// * `ptr` needs to have been previously allocated via [`String`]/`Vec<T>`
481
481
/// (at least, it's highly likely to be incorrect if it wasn't).
482
- /// * `T` needs to have the same size and alignment as what `ptr` was allocated with.
482
+ /// * `T` needs to have the same alignment as what `ptr` was allocated with.
483
483
/// (`T` having a less strict alignment is not sufficient, the alignment really
484
484
/// needs to be equal to satisfy the [`dealloc`] requirement that memory must be
485
485
/// allocated and deallocated with the same layout.)
486
+ /// * The size of `T` times the `capacity` (ie. the allocated size in bytes) needs
487
+ /// to be the same size as the pointer was allocated with. (Because similar to
488
+ /// alignment, [`dealloc`] must be called with the same layout `size`.)
486
489
/// * `length` needs to be less than or equal to `capacity`.
487
- /// * `capacity` needs to be the capacity that the pointer was allocated with.
488
490
///
489
491
/// Violating these may cause problems like corrupting the allocator's
490
492
/// internal data structures. For example it is **not** safe
491
493
/// to build a `Vec<u8>` from a pointer to a C `char` array with length `size_t`.
492
494
/// It's also not safe to build one from a `Vec<u16>` and its length, because
493
495
/// the allocator cares about the alignment, and these two types have different
494
496
/// alignments. The buffer was allocated with alignment 2 (for `u16`), but after
495
- /// turning it into a `Vec<u8>` it'll be deallocated with alignment 1.
497
+ /// turning it into a `Vec<u8>` it'll be deallocated with alignment 1. To avoid
498
+ /// these issues, it is often preferable to do casting/transmuting using
499
+ /// [`slice::from_raw_parts`] instead.
496
500
///
497
501
/// The ownership of `ptr` is effectively transferred to the
498
502
/// `Vec<T>` which may then deallocate, reallocate or change the
@@ -2929,6 +2933,48 @@ impl<T, const N: usize> From<[T; N]> for Vec<T> {
2929
2933
}
2930
2934
}
2931
2935
2936
+ #[ cfg( not( no_global_oom_handling) ) ]
2937
+ #[ stable( feature = "vec_from_array_ref" , since = "1.61.0" ) ]
2938
+ impl < T : Clone , const N : usize > From < & [ T ; N ] > for Vec < T > {
2939
+ /// Allocate a `Vec<T>` and fill it by cloning `s`'s items.
2940
+ ///
2941
+ /// # Examples
2942
+ ///
2943
+ /// ```
2944
+ /// assert_eq!(Vec::from(b"raw"), vec![b'r', b'a', b'w']);
2945
+ /// ```
2946
+ #[ cfg( not( test) ) ]
2947
+ fn from ( s : & [ T ; N ] ) -> Vec < T > {
2948
+ s. to_vec ( )
2949
+ }
2950
+
2951
+ #[ cfg( test) ]
2952
+ fn from ( s : & [ T ; N ] ) -> Vec < T > {
2953
+ crate :: slice:: to_vec ( s, Global )
2954
+ }
2955
+ }
2956
+
2957
+ #[ cfg( not( no_global_oom_handling) ) ]
2958
+ #[ stable( feature = "vec_from_array_ref" , since = "1.61.0" ) ]
2959
+ impl < T : Clone , const N : usize > From < & mut [ T ; N ] > for Vec < T > {
2960
+ /// Allocate a `Vec<T>` and fill it by cloning `s`'s items.
2961
+ ///
2962
+ /// # Examples
2963
+ ///
2964
+ /// ```
2965
+ /// assert_eq!(Vec::from(&mut [1, 2, 3]), vec![1, 2, 3]);
2966
+ /// ```
2967
+ #[ cfg( not( test) ) ]
2968
+ fn from ( s : & mut [ T ; N ] ) -> Vec < T > {
2969
+ s. to_vec ( )
2970
+ }
2971
+
2972
+ #[ cfg( test) ]
2973
+ fn from ( s : & mut [ T ; N ] ) -> Vec < T > {
2974
+ crate :: slice:: to_vec ( s, Global )
2975
+ }
2976
+ }
2977
+
2932
2978
#[ stable( feature = "vec_from_cow_slice" , since = "1.14.0" ) ]
2933
2979
impl < ' a , T > From < Cow < ' a , [ T ] > > for Vec < T >
2934
2980
where
0 commit comments