Skip to content

Commit

Permalink
Add a new alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
ubsan committed Jul 2, 2016
1 parent 2413b52 commit 377bbfe
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/libcore/intrinsics.rs
Expand Up @@ -346,6 +346,29 @@ extern "rust-intrinsic" {
/// assert_eq!(b"Rust", [82, 117, 116, 116]);
///
///
/// // Turning a Vec<&T> into a Vec<Option<&T>>
/// let store = [0, 1, 2, 3];
/// let v_orig = store.iter().collect::<Vec<&i32>>();
/// // Using transmute; Undefined Behavior
/// let v_transmuted = mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(
/// v_orig);
/// // The suggested, safe way
/// let v_collected = v_orig.into_iter()
/// .map(|r| Some(r))
/// .collect::<Vec<Option<&i32>>>();
/// // The no-copy, unsafe way, still using transmute, but not UB
/// let v_no_copy = Vec::from_raw_parts(v_orig.as_mut_ptr(),
/// v_orig.len(),
/// v_orig.capacity());
/// mem::forget(v_orig);
/// // 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 the original
/// // inner type (`&i32`) to the converted inner type (`Option<&i32>`), so
/// // read the nomicon page linked above.
///
///
/// // Copying an `&mut T` to reslice:
/// fn split_at_mut_transmute<T>(slice: &mut [T], index: usize)
/// -> (&mut [T], &mut [T]) {
Expand Down

0 comments on commit 377bbfe

Please sign in to comment.