]> git.lizzy.rs Git - rust.git/commitdiff
Fix example in transmute; add safety requirement to Vec::from_raw_parts
authorHavvy <ryan.havvy@gmail.com>
Wed, 13 Sep 2017 08:27:41 +0000 (01:27 -0700)
committerHavvy <ryan.havvy@gmail.com>
Wed, 13 Sep 2017 11:27:40 +0000 (04:27 -0700)
src/liballoc/vec.rs
src/libcore/intrinsics.rs

index 8141851b8c9af9cbd22567a94cc40309a55bccfd..87b2b3f7a40357cf5c5d8889195bf35ebf3c989c 100644 (file)
@@ -370,6 +370,7 @@ pub fn with_capacity(capacity: usize) -> Vec<T> {
     ///
     /// * `ptr` needs to have been previously allocated via [`String`]/`Vec<T>`
     ///   (at least, it's highly likely to be incorrect if it wasn't).
+    /// * `ptr`'s `T` needs to have the same size and alignment as it was allocated with.
     /// * `length` needs to be less than or equal to `capacity`.
     /// * `capacity` needs to be the capacity that the pointer was allocated with.
     ///
index 607f6f3701799c3337f0bad75fe5878f17c34fd9..f7f1dd12d28b150be07d9bd7b58a45ef5bc6e078 100644 (file)
     /// // The no-copy, unsafe way, still using transmute, but not UB.
     /// // This is equivalent to the original, but safer, and reuses the
     /// // same Vec internals. Therefore the new inner type must have the
-    /// // exact same size, and the same or lesser alignment, as the old
-    /// // type. The same caveats exist for this method as transmute, for
+    /// // exact same size, and the same alignment, as the old type.
+    /// // The same caveats exist for this method as transmute, for
     /// // the original inner type (`&i32`) to the converted inner type
     /// // (`Option<&i32>`), so read the nomicon pages linked above.
     /// let v_from_raw = unsafe {
-    ///     Vec::from_raw_parts(v_orig.as_mut_ptr(),
+    ///     Vec::from_raw_parts(v_orig.as_mut_ptr() as *mut Option<&i32>,
     ///                         v_orig.len(),
     ///                         v_orig.capacity())
     /// };