diff options
| author | Ralf Jung <post@ralfj.de> | 2020-02-29 14:03:25 +0100 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2020-02-29 14:03:25 +0100 |
| commit | b961a03060aaac336269eb1703391a32f4de9dfd (patch) | |
| tree | 263e18a258af7c552a980ecb13c61543f1c5e78c /src | |
| parent | 04e7f96dd89b1f0ad615dff1c85d11d4c4c64cb4 (diff) | |
| download | rust-b961a03060aaac336269eb1703391a32f4de9dfd.tar.gz rust-b961a03060aaac336269eb1703391a32f4de9dfd.zip | |
transmute: improve Vec example
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/intrinsics.rs | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs index 94928211a97..7f4d52a87b0 100644 --- a/src/libcore/intrinsics.rs +++ b/src/libcore/intrinsics.rs @@ -881,7 +881,8 @@ extern "rust-intrinsic" { /// // clone the vector as we will reuse them later /// let v_clone = v_orig.clone(); /// - /// // Using transmute: this is Undefined Behavior, and a bad idea. + /// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a + /// // bad idea and could cause Undefined Behavior /// // However, it is no-copy. /// let v_transmuted = unsafe { /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone) @@ -897,13 +898,14 @@ extern "rust-intrinsic" { /// /// let v_clone = v_orig.clone(); /// - /// // 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 alignment, as the old type. + /// // The no-copy, unsafe way, still using transmute, but not relying on the data layout. + /// // Like the first approach, this reuses the `Vec` internals. + /// // Therefore, the new inner type must have the + /// // 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. + /// // (`Option<&i32>`), so read the nomicon pages linked above and also + /// // consult the [`from_raw_parts`] documentation. /// let v_from_raw = unsafe { // FIXME Update this when vec_into_raw_parts is stabilized /// // Ensure the original vector is not dropped. @@ -914,6 +916,8 @@ extern "rust-intrinsic" { /// }; /// ``` /// + /// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts + /// /// Implementing `split_at_mut`: /// /// ``` |
