diff options
| author | Steve Klabnik <steve@steveklabnik.com> | 2016-02-17 18:14:36 -0500 |
|---|---|---|
| committer | Steve Klabnik <steve@steveklabnik.com> | 2016-02-17 18:14:36 -0500 |
| commit | b85033a7eb05b191e5b361f1a234f8db9fc446e4 (patch) | |
| tree | ef8e5bf629e4a9889e96049a8e9f22d12f567eb4 | |
| parent | 216081d048fb6bcdbc6cc8132f1753d2e6832e50 (diff) | |
| parent | 8f13f8752f925e376b26cb32382fe25acd2a8241 (diff) | |
| download | rust-b85033a7eb05b191e5b361f1a234f8db9fc446e4.tar.gz rust-b85033a7eb05b191e5b361f1a234f8db9fc446e4.zip | |
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
| -rw-r--r-- | src/libcore/mem.rs | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index a521700b84b..c36ad592ad3 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -571,9 +571,25 @@ pub const POST_DROP_USIZE: usize = POST_DROP_U64 as usize; /// ``` /// use std::mem; /// -/// let one = unsafe { mem::transmute_copy(&1) }; +/// #[repr(packed)] +/// struct Foo { +/// bar: u8, +/// } +/// +/// 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); +/// } /// -/// assert_eq!(1, one); +/// // The contents of 'foo_slice' should not have changed +/// assert_eq!(foo_slice, [10]); /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] |
