Rollup merge of #31720 - frewsxcv:std-mem-transmute-copy-example, r=steveklabnik
Prior to this commit, it was a trivial example that did not demonstrate the effects of using the function. Fixes https://github.com/rust-lang/rust/issues/31094
This commit is contained in:
commit
b85033a7eb
@ -571,9 +571,25 @@ macro_rules! repeat_u8_as_u64 {
|
||||
/// ```
|
||||
/// use std::mem;
|
||||
///
|
||||
/// let one = unsafe { mem::transmute_copy(&1) };
|
||||
/// #[repr(packed)]
|
||||
/// struct Foo {
|
||||
/// bar: u8,
|
||||
/// }
|
||||
///
|
||||
/// assert_eq!(1, one);
|
||||
/// let foo_slice = [10u8];
|
||||
///
|
||||
/// unsafe {
|
||||
/// // Copy the data from 'foo_slice' and treat it as a 'Foo'
|
||||
/// let mut foo_struct: Foo = mem::transmute_copy(&foo_slice);
|
||||
/// assert_eq!(foo_struct.bar, 10);
|
||||
///
|
||||
/// // Modify the copied data
|
||||
/// foo_struct.bar = 20;
|
||||
/// assert_eq!(foo_struct.bar, 20);
|
||||
/// }
|
||||
///
|
||||
/// // The contents of 'foo_slice' should not have changed
|
||||
/// assert_eq!(foo_slice, [10]);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
|
Loading…
Reference in New Issue
Block a user