Skip to content

Commit 9dd2ee1

Browse files
committed
Fix example in transmute; add safety requirement to Vec::from_raw_parts
1 parent 7eefed3 commit 9dd2ee1

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/liballoc/vec.rs

+1
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ impl<T> Vec<T> {
370370
///
371371
/// * `ptr` needs to have been previously allocated via [`String`]/`Vec<T>`
372372
/// (at least, it's highly likely to be incorrect if it wasn't).
373+
/// * `ptr`'s `T` needs to have the same size and alignment as it was allocated with.
373374
/// * `length` needs to be less than or equal to `capacity`.
374375
/// * `capacity` needs to be the capacity that the pointer was allocated with.
375376
///

src/libcore/intrinsics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -848,12 +848,12 @@ extern "rust-intrinsic" {
848848
/// // The no-copy, unsafe way, still using transmute, but not UB.
849849
/// // This is equivalent to the original, but safer, and reuses the
850850
/// // same Vec internals. Therefore the new inner type must have the
851-
/// // exact same size, and the same or lesser alignment, as the old
852-
/// // type. The same caveats exist for this method as transmute, for
851+
/// // exact same size, and the same alignment, as the old type.
852+
/// // The same caveats exist for this method as transmute, for
853853
/// // the original inner type (`&i32`) to the converted inner type
854854
/// // (`Option<&i32>`), so read the nomicon pages linked above.
855855
/// let v_from_raw = unsafe {
856-
/// Vec::from_raw_parts(v_orig.as_mut_ptr(),
856+
/// Vec::from_raw_parts(v_orig.as_mut_ptr() as *mut Option<&i32>,
857857
/// v_orig.len(),
858858
/// v_orig.capacity())
859859
/// };

0 commit comments

Comments
 (0)