Skip to content

Commit b961a03

Browse files
committed
transmute: improve Vec example
1 parent 04e7f96 commit b961a03

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/libcore/intrinsics.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,8 @@ extern "rust-intrinsic" {
881881
/// // clone the vector as we will reuse them later
882882
/// let v_clone = v_orig.clone();
883883
///
884-
/// // Using transmute: this is Undefined Behavior, and a bad idea.
884+
/// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
885+
/// // bad idea and could cause Undefined Behavior
885886
/// // However, it is no-copy.
886887
/// let v_transmuted = unsafe {
887888
/// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
@@ -897,13 +898,14 @@ extern "rust-intrinsic" {
897898
///
898899
/// let v_clone = v_orig.clone();
899900
///
900-
/// // The no-copy, unsafe way, still using transmute, but not UB.
901-
/// // This is equivalent to the original, but safer, and reuses the
902-
/// // same `Vec` internals. Therefore, the new inner type must have the
903-
/// // exact same size, and the same alignment, as the old type.
901+
/// // The no-copy, unsafe way, still using transmute, but not relying on the data layout.
902+
/// // Like the first approach, this reuses the `Vec` internals.
903+
/// // Therefore, the new inner type must have the
904+
/// // exact same size, *and the same alignment*, as the old type.
904905
/// // The same caveats exist for this method as transmute, for
905906
/// // the original inner type (`&i32`) to the converted inner type
906-
/// // (`Option<&i32>`), so read the nomicon pages linked above.
907+
/// // (`Option<&i32>`), so read the nomicon pages linked above and also
908+
/// // consult the [`from_raw_parts`] documentation.
907909
/// let v_from_raw = unsafe {
908910
// FIXME Update this when vec_into_raw_parts is stabilized
909911
/// // Ensure the original vector is not dropped.
@@ -914,6 +916,8 @@ extern "rust-intrinsic" {
914916
/// };
915917
/// ```
916918
///
919+
/// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
920+
///
917921
/// Implementing `split_at_mut`:
918922
///
919923
/// ```

0 commit comments

Comments
 (0)