2014-02-18 20:30:21 +00:00
|
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-10-16 18:34:01 -07:00
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2016-03-04 17:37:11 -05:00
|
|
|
|
//! Basic functions for dealing with memory.
|
2014-02-09 00:23:04 -08:00
|
|
|
|
//!
|
|
|
|
|
//! This module contains functions for querying the size and alignment of
|
|
|
|
|
//! types, initializing and manipulating memory.
|
2013-10-16 18:34:01 -07:00
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2014-12-15 19:40:25 -08:00
|
|
|
|
|
2016-06-29 12:15:59 -04:00
|
|
|
|
use clone;
|
|
|
|
|
use cmp;
|
|
|
|
|
use fmt;
|
|
|
|
|
use hash;
|
2014-02-15 23:49:08 -08:00
|
|
|
|
use intrinsics;
|
2016-06-29 12:15:59 -04:00
|
|
|
|
use marker::{Copy, PhantomData, Sized};
|
2014-06-16 11:25:47 -07:00
|
|
|
|
use ptr;
|
2017-09-04 02:29:16 -07:00
|
|
|
|
use ops::{Deref, DerefMut};
|
2013-10-16 18:34:01 -07:00
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-06-12 14:08:44 -07:00
|
|
|
|
pub use intrinsics::transmute;
|
|
|
|
|
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// Leaks a value: takes ownership and "forgets" about the value **without running
|
|
|
|
|
/// its destructor**.
|
2014-08-14 02:15:09 -04:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// Any resources the value manages, such as heap memory or a file handle, will linger
|
|
|
|
|
/// forever in an unreachable state.
|
2014-08-14 02:15:09 -04:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// If you want to dispose of a value properly, running its destructor, see
|
|
|
|
|
/// [`mem::drop`][drop].
|
2015-08-04 15:57:54 -04:00
|
|
|
|
///
|
2015-05-07 10:49:39 -07:00
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// `forget` is not marked as `unsafe`, because Rust's safety guarantees
|
|
|
|
|
/// do not include a guarantee that destructors will always run. For example,
|
|
|
|
|
/// a program can create a reference cycle using [`Rc`][rc], or call
|
2017-04-29 14:56:35 -04:00
|
|
|
|
/// [`process::exit`][exit] to exit without running destructors. Thus, allowing
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// `mem::forget` from safe code does not fundamentally change Rust's safety
|
|
|
|
|
/// guarantees.
|
2015-05-07 10:49:39 -07:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// That said, leaking resources such as memory or I/O objects is usually undesirable,
|
|
|
|
|
/// so `forget` is only recommended for specialized use cases like those shown below.
|
2015-05-07 10:49:39 -07:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// Because forgetting a value is allowed, any `unsafe` code you write must
|
|
|
|
|
/// allow for this possibility. You cannot return a value and expect that the
|
|
|
|
|
/// caller will necessarily run the value's destructor.
|
2015-05-07 10:49:39 -07:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// [rc]: ../../std/rc/struct.Rc.html
|
|
|
|
|
/// [exit]: ../../std/process/fn.exit.html
|
2015-05-07 10:49:39 -07:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// # Examples
|
2015-05-07 10:49:39 -07:00
|
|
|
|
///
|
2015-08-04 15:57:54 -04:00
|
|
|
|
/// Leak some heap memory by never deallocating it:
|
2015-05-28 13:04:17 -05:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// ```
|
2015-05-07 10:49:39 -07:00
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
|
|
|
|
/// let heap_memory = Box::new(3);
|
|
|
|
|
/// mem::forget(heap_memory);
|
2015-05-28 13:04:17 -05:00
|
|
|
|
/// ```
|
|
|
|
|
///
|
2015-08-04 15:57:54 -04:00
|
|
|
|
/// Leak an I/O object, never closing the file:
|
2015-05-28 13:04:17 -05:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// ```no_run
|
2015-05-28 13:04:17 -05:00
|
|
|
|
/// use std::mem;
|
|
|
|
|
/// use std::fs::File;
|
2015-05-07 10:49:39 -07:00
|
|
|
|
///
|
|
|
|
|
/// let file = File::open("foo.txt").unwrap();
|
|
|
|
|
/// mem::forget(file);
|
|
|
|
|
/// ```
|
2015-05-28 13:04:17 -05:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// The practical use cases for `forget` are rather specialized and mainly come
|
|
|
|
|
/// up in unsafe or FFI code.
|
|
|
|
|
///
|
|
|
|
|
/// ## Use case 1
|
|
|
|
|
///
|
|
|
|
|
/// You have created an uninitialized value using [`mem::uninitialized`][uninit].
|
|
|
|
|
/// You must either initialize or `forget` it on every computation path before
|
|
|
|
|
/// Rust drops it automatically, like at the end of a scope or after a panic.
|
|
|
|
|
/// Running the destructor on an uninitialized value would be [undefined behavior][ub].
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
/// use std::ptr;
|
|
|
|
|
///
|
|
|
|
|
/// # let some_condition = false;
|
|
|
|
|
/// unsafe {
|
|
|
|
|
/// let mut uninit_vec: Vec<u32> = mem::uninitialized();
|
|
|
|
|
///
|
|
|
|
|
/// if some_condition {
|
|
|
|
|
/// // Initialize the variable.
|
|
|
|
|
/// ptr::write(&mut uninit_vec, Vec::new());
|
|
|
|
|
/// } else {
|
|
|
|
|
/// // Forget the uninitialized value so its destructor doesn't run.
|
|
|
|
|
/// mem::forget(uninit_vec);
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// ## Use case 2
|
|
|
|
|
///
|
|
|
|
|
/// You have duplicated the bytes making up a value, without doing a proper
|
|
|
|
|
/// [`Clone`][clone]. You need the value's destructor to run only once,
|
|
|
|
|
/// because a double `free` is undefined behavior.
|
2015-05-28 13:04:17 -05:00
|
|
|
|
///
|
2017-04-24 07:40:11 +01:00
|
|
|
|
/// An example is a possible implementation of [`mem::swap`][swap]:
|
2016-09-08 20:04:05 -07:00
|
|
|
|
///
|
|
|
|
|
/// ```
|
2015-05-28 13:04:17 -05:00
|
|
|
|
/// use std::mem;
|
|
|
|
|
/// use std::ptr;
|
|
|
|
|
///
|
2015-11-03 15:27:03 +00:00
|
|
|
|
/// # #[allow(dead_code)]
|
2015-05-28 13:04:17 -05:00
|
|
|
|
/// fn swap<T>(x: &mut T, y: &mut T) {
|
|
|
|
|
/// unsafe {
|
|
|
|
|
/// // Give ourselves some scratch space to work with
|
|
|
|
|
/// let mut t: T = mem::uninitialized();
|
|
|
|
|
///
|
|
|
|
|
/// // Perform the swap, `&mut` pointers never alias
|
|
|
|
|
/// ptr::copy_nonoverlapping(&*x, &mut t, 1);
|
|
|
|
|
/// ptr::copy_nonoverlapping(&*y, x, 1);
|
|
|
|
|
/// ptr::copy_nonoverlapping(&t, y, 1);
|
|
|
|
|
///
|
|
|
|
|
/// // y and t now point to the same thing, but we need to completely
|
|
|
|
|
/// // forget `t` because we do not want to run the destructor for `T`
|
|
|
|
|
/// // on its value, which is still owned somewhere outside this function.
|
|
|
|
|
/// mem::forget(t);
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-09-08 20:04:05 -07:00
|
|
|
|
///
|
|
|
|
|
/// ## Use case 3
|
|
|
|
|
///
|
|
|
|
|
/// You are transferring ownership across a [FFI] boundary to code written in
|
|
|
|
|
/// another language. You need to `forget` the value on the Rust side because Rust
|
|
|
|
|
/// code is no longer responsible for it.
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
|
|
|
|
/// extern "C" {
|
|
|
|
|
/// fn my_c_function(x: *const u32);
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// let x: Box<u32> = Box::new(3);
|
|
|
|
|
///
|
|
|
|
|
/// // Transfer ownership into C code.
|
|
|
|
|
/// unsafe {
|
|
|
|
|
/// my_c_function(&*x);
|
|
|
|
|
/// }
|
|
|
|
|
/// mem::forget(x);
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// In this case, C code must call back into Rust to free the object. Calling C's `free`
|
|
|
|
|
/// function on a [`Box`][box] is *not* safe! Also, `Box` provides an [`into_raw`][into_raw]
|
|
|
|
|
/// method which is the preferred way to do this in practice.
|
|
|
|
|
///
|
|
|
|
|
/// [drop]: fn.drop.html
|
|
|
|
|
/// [uninit]: fn.uninitialized.html
|
|
|
|
|
/// [clone]: ../clone/trait.Clone.html
|
|
|
|
|
/// [swap]: fn.swap.html
|
2017-03-15 12:13:55 -04:00
|
|
|
|
/// [FFI]: ../../book/first-edition/ffi.html
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// [box]: ../../std/boxed/struct.Box.html
|
|
|
|
|
/// [into_raw]: ../../std/boxed/struct.Box.html#method.into_raw
|
2017-02-21 01:15:29 -05:00
|
|
|
|
/// [ub]: ../../reference/behavior-considered-undefined.html
|
2016-05-02 13:57:53 -07:00
|
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-05-07 10:49:39 -07:00
|
|
|
|
pub fn forget<T>(t: T) {
|
2017-04-02 11:13:31 +03:00
|
|
|
|
ManuallyDrop::new(t);
|
2015-05-07 10:49:39 -07:00
|
|
|
|
}
|
2014-08-14 02:15:09 -04:00
|
|
|
|
|
2014-02-18 20:30:21 +00:00
|
|
|
|
/// Returns the size of a type in bytes.
|
2014-12-16 18:23:55 -05:00
|
|
|
|
///
|
2017-09-16 20:40:05 -07:00
|
|
|
|
/// More specifically, this is the offset in bytes between successive elements
|
|
|
|
|
/// in an array with that item type including alignment padding. Thus, for any
|
|
|
|
|
/// type `T` and length `n`, `[T; n]` has a size of `n * size_of::<T>()`.
|
|
|
|
|
///
|
|
|
|
|
/// In general, the size of a type is not stable across compilations, but
|
|
|
|
|
/// specific types such as primitives are.
|
|
|
|
|
///
|
|
|
|
|
/// The following table gives the size for primitives.
|
|
|
|
|
///
|
|
|
|
|
/// Type | size_of::\<Type>()
|
|
|
|
|
/// ---- | ---------------
|
|
|
|
|
/// () | 0
|
|
|
|
|
/// u8 | 1
|
|
|
|
|
/// u16 | 2
|
|
|
|
|
/// u32 | 4
|
|
|
|
|
/// u64 | 8
|
|
|
|
|
/// i8 | 1
|
|
|
|
|
/// i16 | 2
|
|
|
|
|
/// i32 | 4
|
|
|
|
|
/// i64 | 8
|
|
|
|
|
/// f32 | 4
|
|
|
|
|
/// f64 | 8
|
|
|
|
|
/// char | 4
|
|
|
|
|
///
|
|
|
|
|
/// Furthermore, `usize` and `isize` have the same size.
|
|
|
|
|
///
|
|
|
|
|
/// The types `*const T`, `&T`, `Box<T>`, `Option<&T>`, and `Option<Box<T>>` all have
|
|
|
|
|
/// the same size. If `T` is Sized, all of those types have the same size as `usize`.
|
|
|
|
|
///
|
|
|
|
|
/// The mutability of a pointer does not change its size. As such, `&T` and `&mut T`
|
|
|
|
|
/// have the same size. Likewise for `*const T` and `*mut T`.
|
2016-05-01 23:30:12 -07:00
|
|
|
|
///
|
2017-09-27 23:31:29 -07:00
|
|
|
|
/// # Size of `#[repr(C)]` items
|
2017-09-26 17:41:21 -07:00
|
|
|
|
///
|
|
|
|
|
/// The `C` representation for items has a defined layout. With this layout,
|
|
|
|
|
/// the size of items is also stable as long as all fields have a stable size.
|
|
|
|
|
///
|
2017-09-28 10:47:54 -07:00
|
|
|
|
/// ## Size of Structs
|
2017-09-26 17:41:21 -07:00
|
|
|
|
///
|
|
|
|
|
/// For `structs`, the size is determined by the following algorithm.
|
|
|
|
|
///
|
|
|
|
|
/// For each field in the struct ordered by declaration order:
|
|
|
|
|
///
|
|
|
|
|
/// 1. Add the size of the field.
|
|
|
|
|
/// 2. Round up the current size to the nearest multiple of the next field's [alignment].
|
|
|
|
|
///
|
|
|
|
|
/// Finally, round the size of the struct to the nearest multiple of its [alignment].
|
|
|
|
|
///
|
|
|
|
|
/// Unlike `C`, zero sized structs are not rounded up to one byte in size.
|
|
|
|
|
///
|
2017-09-28 10:47:54 -07:00
|
|
|
|
/// ## Size of Enums
|
2017-09-26 17:41:21 -07:00
|
|
|
|
///
|
|
|
|
|
/// Enums that carry no data other than the descriminant have the same size as C enums
|
|
|
|
|
/// on the platform they are compiled for.
|
|
|
|
|
///
|
2017-09-28 10:47:54 -07:00
|
|
|
|
/// ## Size of Unions
|
2017-09-26 17:41:21 -07:00
|
|
|
|
///
|
|
|
|
|
/// The size of a union is the size of its largest field.
|
|
|
|
|
///
|
|
|
|
|
/// Unlike `C`, zero sized unions are not rounded up to one byte in size.
|
|
|
|
|
///
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
2017-09-16 20:40:05 -07:00
|
|
|
|
/// // Some primitives
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// assert_eq!(4, mem::size_of::<i32>());
|
2017-09-16 20:40:05 -07:00
|
|
|
|
/// assert_eq!(8, mem::size_of::<f64>());
|
|
|
|
|
/// assert_eq!(0, mem::size_of::<()>());
|
|
|
|
|
///
|
|
|
|
|
/// // Some arrays
|
|
|
|
|
/// assert_eq!(8, mem::size_of::<[i32; 2]>());
|
|
|
|
|
/// assert_eq!(12, mem::size_of::<[i32; 3]>());
|
|
|
|
|
/// assert_eq!(0, mem::size_of::<[i32; 0]>());
|
|
|
|
|
///
|
|
|
|
|
///
|
|
|
|
|
/// // Pointer size equality
|
|
|
|
|
/// assert_eq!(mem::size_of::<&i32>(), mem::size_of::<*const i32>());
|
|
|
|
|
/// assert_eq!(mem::size_of::<&i32>(), mem::size_of::<Box<i32>>());
|
|
|
|
|
/// assert_eq!(mem::size_of::<&i32>(), mem::size_of::<Option<&i32>>());
|
|
|
|
|
/// assert_eq!(mem::size_of::<Box<i32>>(), mem::size_of::<Option<Box<i32>>>());
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// ```
|
2017-09-26 17:41:21 -07:00
|
|
|
|
///
|
|
|
|
|
/// Using `#[repr(C)]`.
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
|
|
|
|
/// #[repr(C)]
|
|
|
|
|
/// struct FieldStruct {
|
|
|
|
|
/// first: u8,
|
|
|
|
|
/// second: u16,
|
|
|
|
|
/// third: u8
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// // The size of the first field is 1, so add 1 to the size. Size is 1.
|
|
|
|
|
/// // The alignment of the second field is 2, so add 1 to the size for padding. Size is 2.
|
|
|
|
|
/// // The size of the second field is 2, so add 2 to the size. Size is 4.
|
|
|
|
|
/// // The alignment of the third field is 1, so add 0 to the size for padding. Size is 4.
|
|
|
|
|
/// // The size of the third field is 1, so add 1 to the size. Size is 5.
|
|
|
|
|
/// // Finally, the alignment of the struct is 2, so add 1 to the size for padding. Size is 6.
|
|
|
|
|
/// assert_eq!(6, mem::size_of::<FieldStruct>());
|
|
|
|
|
///
|
|
|
|
|
/// #[repr(C)]
|
|
|
|
|
/// struct TupleStruct(u8, u16, u8);
|
|
|
|
|
///
|
|
|
|
|
/// // Tuple structs follow the same rules.
|
|
|
|
|
/// assert_eq!(6, mem::size_of::<TupleStruct>());
|
|
|
|
|
///
|
|
|
|
|
/// // Note that reordering the fields can lower the size. We can remove both padding bytes
|
|
|
|
|
/// // by putting `third` before `second`.
|
|
|
|
|
/// #[repr(C)]
|
|
|
|
|
/// struct FieldStructOptimized {
|
|
|
|
|
/// first: u8,
|
|
|
|
|
/// third: u8,
|
|
|
|
|
/// second: u16
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(4, mem::size_of::<FieldStructOptimized>());
|
|
|
|
|
///
|
|
|
|
|
/// // Union size is the size of the largest field.
|
|
|
|
|
/// #[repr(C)]
|
|
|
|
|
/// union ExampleUnion {
|
|
|
|
|
/// smaller: u8,
|
|
|
|
|
/// larger: u16
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(2, mem::size_of::<ExampleUnion>());
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// [alignment]: ./fn.align_of.html
|
2013-10-16 18:34:01 -07:00
|
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2017-09-08 18:26:54 +00:00
|
|
|
|
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_size_of"))]
|
2017-06-23 17:48:29 +03:00
|
|
|
|
pub const fn size_of<T>() -> usize {
|
|
|
|
|
unsafe { intrinsics::size_of::<T>() }
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// Returns the size of the pointed-to value in bytes.
|
|
|
|
|
///
|
|
|
|
|
/// This is usually the same as `size_of::<T>()`. However, when `T` *has* no
|
|
|
|
|
/// statically known size, e.g. a slice [`[T]`][slice] or a [trait object],
|
|
|
|
|
/// then `size_of_val` can be used to get the dynamically-known size.
|
|
|
|
|
///
|
|
|
|
|
/// [slice]: ../../std/primitive.slice.html
|
2017-03-15 12:13:55 -04:00
|
|
|
|
/// [trait object]: ../../book/first-edition/trait-objects.html
|
2015-04-15 11:57:29 +12:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(4, mem::size_of_val(&5i32));
|
2016-09-08 20:04:05 -07:00
|
|
|
|
///
|
|
|
|
|
/// let x: [u8; 13] = [0; 13];
|
|
|
|
|
/// let y: &[u8] = &x;
|
|
|
|
|
/// assert_eq!(13, mem::size_of_val(y));
|
2015-04-15 11:57:29 +12:00
|
|
|
|
/// ```
|
|
|
|
|
#[inline]
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
pub fn size_of_val<T: ?Sized>(val: &T) -> usize {
|
|
|
|
|
unsafe { intrinsics::size_of_val(val) }
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// Returns the [ABI]-required minimum alignment of a type.
|
|
|
|
|
///
|
2017-05-30 21:34:50 +02:00
|
|
|
|
/// Every reference to a value of the type `T` must be a multiple of this number.
|
2013-10-21 21:41:32 +02:00
|
|
|
|
///
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// This is the alignment used for struct fields. It may be smaller than the preferred alignment.
|
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// [ABI]: https://en.wikipedia.org/wiki/Application_binary_interface
|
|
|
|
|
///
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2015-11-03 15:27:03 +00:00
|
|
|
|
/// # #![allow(deprecated)]
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(4, mem::min_align_of::<i32>());
|
|
|
|
|
/// ```
|
2013-10-16 18:34:01 -07:00
|
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-20 16:11:20 +03:00
|
|
|
|
#[rustc_deprecated(reason = "use `align_of` instead", since = "1.2.0")]
|
2015-02-16 14:38:50 +01:00
|
|
|
|
pub fn min_align_of<T>() -> usize {
|
2013-10-16 18:34:01 -07:00
|
|
|
|
unsafe { intrinsics::min_align_of::<T>() }
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// Returns the [ABI]-required minimum alignment of the type of the value that `val` points to.
|
|
|
|
|
///
|
2017-05-30 21:34:50 +02:00
|
|
|
|
/// Every reference to a value of the type `T` must be a multiple of this number.
|
2016-09-08 20:04:05 -07:00
|
|
|
|
///
|
|
|
|
|
/// [ABI]: https://en.wikipedia.org/wiki/Application_binary_interface
|
2015-04-15 11:57:29 +12:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2015-11-03 15:27:03 +00:00
|
|
|
|
/// # #![allow(deprecated)]
|
2015-04-15 11:57:29 +12:00
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(4, mem::min_align_of_val(&5i32));
|
|
|
|
|
/// ```
|
|
|
|
|
#[inline]
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-20 16:11:20 +03:00
|
|
|
|
#[rustc_deprecated(reason = "use `align_of_val` instead", since = "1.2.0")]
|
2015-04-15 11:57:29 +12:00
|
|
|
|
pub fn min_align_of_val<T: ?Sized>(val: &T) -> usize {
|
|
|
|
|
unsafe { intrinsics::min_align_of_val(val) }
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// Returns the [ABI]-required minimum alignment of a type.
|
|
|
|
|
///
|
2017-05-30 21:34:50 +02:00
|
|
|
|
/// Every reference to a value of the type `T` must be a multiple of this number.
|
2014-05-17 00:56:00 -07:00
|
|
|
|
///
|
2015-05-20 19:18:03 +10:00
|
|
|
|
/// This is the alignment used for struct fields. It may be smaller than the preferred alignment.
|
2014-12-16 18:23:55 -05:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// [ABI]: https://en.wikipedia.org/wiki/Application_binary_interface
|
|
|
|
|
///
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(4, mem::align_of::<i32>());
|
|
|
|
|
/// ```
|
2013-10-16 18:34:01 -07:00
|
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2017-09-08 18:26:54 +00:00
|
|
|
|
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_align_of"))]
|
2017-06-23 17:48:29 +03:00
|
|
|
|
pub const fn align_of<T>() -> usize {
|
|
|
|
|
unsafe { intrinsics::min_align_of::<T>() }
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// Returns the [ABI]-required minimum alignment of the type of the value that `val` points to.
|
|
|
|
|
///
|
2017-05-30 21:34:50 +02:00
|
|
|
|
/// Every reference to a value of the type `T` must be a multiple of this number.
|
2016-09-08 20:04:05 -07:00
|
|
|
|
///
|
|
|
|
|
/// [ABI]: https://en.wikipedia.org/wiki/Application_binary_interface
|
2014-12-16 18:23:55 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(4, mem::align_of_val(&5i32));
|
|
|
|
|
/// ```
|
2013-10-16 18:34:01 -07:00
|
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-05-20 19:18:03 +10:00
|
|
|
|
pub fn align_of_val<T: ?Sized>(val: &T) -> usize {
|
|
|
|
|
unsafe { intrinsics::min_align_of_val(val) }
|
2013-10-16 18:34:01 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-10 12:48:36 -04:00
|
|
|
|
/// Returns whether dropping values of type `T` matters.
|
|
|
|
|
///
|
|
|
|
|
/// This is purely an optimization hint, and may be implemented conservatively.
|
|
|
|
|
/// For instance, always returning `true` would be a valid implementation of
|
|
|
|
|
/// this function.
|
|
|
|
|
///
|
|
|
|
|
/// Low level implementations of things like collections, which need to manually
|
|
|
|
|
/// drop their data, should use this function to avoid unnecessarily
|
|
|
|
|
/// trying to drop all their contents when they are destroyed. This might not
|
|
|
|
|
/// make a difference in release builds (where a loop that has no side-effects
|
|
|
|
|
/// is easily detected and eliminated), but is often a big win for debug builds.
|
|
|
|
|
///
|
|
|
|
|
/// Note that `ptr::drop_in_place` already performs this check, so if your workload
|
|
|
|
|
/// can be reduced to some small number of drop_in_place calls, using this is
|
|
|
|
|
/// unnecessary. In particular note that you can drop_in_place a slice, and that
|
|
|
|
|
/// will do a single needs_drop check for all the values.
|
|
|
|
|
///
|
|
|
|
|
/// Types like Vec therefore just `drop_in_place(&mut self[..])` without using
|
|
|
|
|
/// needs_drop explicitly. Types like HashMap, on the other hand, have to drop
|
|
|
|
|
/// values one at a time and should use this API.
|
|
|
|
|
///
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// Here's an example of how a collection might make use of needs_drop:
|
|
|
|
|
///
|
2017-06-20 15:15:16 +08:00
|
|
|
|
/// ```
|
2017-05-10 12:48:36 -04:00
|
|
|
|
/// use std::{mem, ptr};
|
|
|
|
|
///
|
2017-06-20 15:15:16 +08:00
|
|
|
|
/// pub struct MyCollection<T> {
|
|
|
|
|
/// # data: [T; 1],
|
|
|
|
|
/// /* ... */
|
|
|
|
|
/// }
|
|
|
|
|
/// # impl<T> MyCollection<T> {
|
|
|
|
|
/// # fn iter_mut(&mut self) -> &mut [T] { &mut self.data }
|
|
|
|
|
/// # fn free_buffer(&mut self) {}
|
|
|
|
|
/// # }
|
2017-05-10 12:48:36 -04:00
|
|
|
|
///
|
|
|
|
|
/// impl<T> Drop for MyCollection<T> {
|
|
|
|
|
/// fn drop(&mut self) {
|
|
|
|
|
/// unsafe {
|
|
|
|
|
/// // drop the data
|
|
|
|
|
/// if mem::needs_drop::<T>() {
|
|
|
|
|
/// for x in self.iter_mut() {
|
|
|
|
|
/// ptr::drop_in_place(x);
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// self.free_buffer();
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
#[inline]
|
2017-09-24 22:23:26 -07:00
|
|
|
|
#[stable(feature = "needs_drop", since = "1.21.0")]
|
2017-05-10 12:48:36 -04:00
|
|
|
|
pub fn needs_drop<T>() -> bool {
|
|
|
|
|
unsafe { intrinsics::needs_drop::<T>() }
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// Creates a value whose bytes are all zero.
|
|
|
|
|
///
|
|
|
|
|
/// This has the same effect as allocating space with
|
|
|
|
|
/// [`mem::uninitialized`][uninit] and then zeroing it out. It is useful for
|
|
|
|
|
/// [FFI] sometimes, but should generally be avoided.
|
2014-02-08 02:46:55 -08:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// There is no guarantee that an all-zero byte-pattern represents a valid value of
|
|
|
|
|
/// some type `T`. If `T` has a destructor and the value is destroyed (due to
|
|
|
|
|
/// a panic or the end of a scope) before being initialized, then the destructor
|
|
|
|
|
/// will run on zeroed data, likely leading to [undefined behavior][ub].
|
2014-05-17 00:56:00 -07:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// See also the documentation for [`mem::uninitialized`][uninit], which has
|
|
|
|
|
/// many of the same caveats.
|
2014-05-17 00:56:00 -07:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// [uninit]: fn.uninitialized.html
|
2017-03-15 12:13:55 -04:00
|
|
|
|
/// [FFI]: ../../book/first-edition/ffi.html
|
2017-02-21 01:15:29 -05:00
|
|
|
|
/// [ub]: ../../reference/behavior-considered-undefined.html
|
2014-12-16 18:23:55 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
2015-02-16 14:38:50 +01:00
|
|
|
|
/// let x: i32 = unsafe { mem::zeroed() };
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// assert_eq!(0, x);
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// ```
|
2014-02-08 02:46:55 -08:00
|
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-17 00:56:00 -07:00
|
|
|
|
pub unsafe fn zeroed<T>() -> T {
|
2014-02-08 02:46:55 -08:00
|
|
|
|
intrinsics::init()
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-02 22:04:32 -07:00
|
|
|
|
/// Bypasses Rust's normal memory-initialization checks by pretending to
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// produce a value of type `T`, while doing nothing at all.
|
2014-05-17 00:56:00 -07:00
|
|
|
|
///
|
2016-11-11 18:49:16 +01:00
|
|
|
|
/// **This is incredibly dangerous and should not be done lightly. Deeply
|
2015-08-02 22:04:32 -07:00
|
|
|
|
/// consider initializing your memory with a default value instead.**
|
2014-05-17 00:56:00 -07:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// This is useful for [FFI] functions and initializing arrays sometimes,
|
2015-08-02 22:04:32 -07:00
|
|
|
|
/// but should generally be avoided.
|
|
|
|
|
///
|
2017-03-15 12:13:55 -04:00
|
|
|
|
/// [FFI]: ../../book/first-edition/ffi.html
|
2015-08-02 22:04:32 -07:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// # Undefined behavior
|
|
|
|
|
///
|
|
|
|
|
/// It is [undefined behavior][ub] to read uninitialized memory, even just an
|
2015-08-02 22:04:32 -07:00
|
|
|
|
/// uninitialized boolean. For instance, if you branch on the value of such
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// a boolean, your program may take one, both, or neither of the branches.
|
2015-08-02 22:04:32 -07:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// Writing to the uninitialized value is similarly dangerous. Rust believes the
|
2016-11-11 18:49:16 +01:00
|
|
|
|
/// value is initialized, and will therefore try to [`Drop`] the uninitialized
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// value and its fields if you try to overwrite it in a normal manner. The only way
|
|
|
|
|
/// to safely initialize an uninitialized value is with [`ptr::write`][write],
|
|
|
|
|
/// [`ptr::copy`][copy], or [`ptr::copy_nonoverlapping`][copy_no].
|
2015-08-02 22:04:32 -07:00
|
|
|
|
///
|
2016-11-11 18:49:16 +01:00
|
|
|
|
/// If the value does implement [`Drop`], it must be initialized before
|
2015-08-02 22:04:32 -07:00
|
|
|
|
/// it goes out of scope (and therefore would be dropped). Note that this
|
|
|
|
|
/// includes a `panic` occurring and unwinding the stack suddenly.
|
2014-12-16 18:23:55 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
2016-11-11 18:49:16 +01:00
|
|
|
|
/// Here's how to safely initialize an array of [`Vec`]s.
|
2015-08-02 22:04:32 -07:00
|
|
|
|
///
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// ```
|
|
|
|
|
/// use std::mem;
|
2015-08-02 22:04:32 -07:00
|
|
|
|
/// use std::ptr;
|
2014-12-16 18:23:55 -05:00
|
|
|
|
///
|
2015-08-02 22:04:32 -07:00
|
|
|
|
/// // Only declare the array. This safely leaves it
|
|
|
|
|
/// // uninitialized in a way that Rust will track for us.
|
|
|
|
|
/// // However we can't initialize it element-by-element
|
|
|
|
|
/// // safely, and we can't use the `[value; 1000]`
|
|
|
|
|
/// // constructor because it only works with `Copy` data.
|
|
|
|
|
/// let mut data: [Vec<u32>; 1000];
|
|
|
|
|
///
|
|
|
|
|
/// unsafe {
|
|
|
|
|
/// // So we need to do this to initialize it.
|
|
|
|
|
/// data = mem::uninitialized();
|
|
|
|
|
///
|
|
|
|
|
/// // DANGER ZONE: if anything panics or otherwise
|
|
|
|
|
/// // incorrectly reads the array here, we will have
|
2015-10-13 09:44:11 -04:00
|
|
|
|
/// // Undefined Behavior.
|
2015-08-02 22:04:32 -07:00
|
|
|
|
///
|
|
|
|
|
/// // It's ok to mutably iterate the data, since this
|
|
|
|
|
/// // doesn't involve reading it at all.
|
|
|
|
|
/// // (ptr and len are statically known for arrays)
|
|
|
|
|
/// for elem in &mut data[..] {
|
|
|
|
|
/// // *elem = Vec::new() would try to drop the
|
|
|
|
|
/// // uninitialized memory at `elem` -- bad!
|
|
|
|
|
/// //
|
|
|
|
|
/// // Vec::new doesn't allocate or do really
|
|
|
|
|
/// // anything. It's only safe to call here
|
|
|
|
|
/// // because we know it won't panic.
|
|
|
|
|
/// ptr::write(elem, Vec::new());
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// // SAFE ZONE: everything is initialized.
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// println!("{:?}", &data[0]);
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// ```
|
2015-08-02 22:04:32 -07:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// This example emphasizes exactly how delicate and dangerous using `mem::uninitialized`
|
2016-11-11 18:49:16 +01:00
|
|
|
|
/// can be. Note that the [`vec!`] macro *does* let you initialize every element with a
|
|
|
|
|
/// value that is only [`Clone`], so the following is semantically equivalent and
|
2015-08-03 21:52:20 +03:00
|
|
|
|
/// vastly less dangerous, as long as you can live with an extra heap
|
2015-08-02 22:04:32 -07:00
|
|
|
|
/// allocation:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// let data: Vec<Vec<u32>> = vec![Vec::new(); 1000];
|
|
|
|
|
/// println!("{:?}", &data[0]);
|
|
|
|
|
/// ```
|
2016-11-11 18:49:16 +01:00
|
|
|
|
///
|
|
|
|
|
/// [`Vec`]: ../../std/vec/struct.Vec.html
|
|
|
|
|
/// [`vec!`]: ../../std/macro.vec.html
|
|
|
|
|
/// [`Clone`]: ../../std/clone/trait.Clone.html
|
2017-02-21 01:15:29 -05:00
|
|
|
|
/// [ub]: ../../reference/behavior-considered-undefined.html
|
2016-11-11 18:49:16 +01:00
|
|
|
|
/// [write]: ../ptr/fn.write.html
|
|
|
|
|
/// [copy]: ../intrinsics/fn.copy.html
|
|
|
|
|
/// [copy_no]: ../intrinsics/fn.copy_nonoverlapping.html
|
|
|
|
|
/// [`Drop`]: ../ops/trait.Drop.html
|
2014-02-08 02:46:55 -08:00
|
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-23 20:53:56 -07:00
|
|
|
|
pub unsafe fn uninitialized<T>() -> T {
|
|
|
|
|
intrinsics::uninit()
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// Swaps the values at two mutable locations, without deinitializing either one.
|
2014-12-16 18:23:55 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// let mut x = 5;
|
|
|
|
|
/// let mut y = 42;
|
2014-12-16 18:23:55 -05:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// mem::swap(&mut x, &mut y);
|
2014-12-16 18:23:55 -05:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// assert_eq!(42, x);
|
|
|
|
|
/// assert_eq!(5, y);
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// ```
|
2014-02-01 04:35:36 +08:00
|
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-02-01 04:35:36 +08:00
|
|
|
|
pub fn swap<T>(x: &mut T, y: &mut T) {
|
|
|
|
|
unsafe {
|
2017-06-20 23:48:15 -07:00
|
|
|
|
ptr::swap_nonoverlapping(x, y, 1);
|
2014-02-01 04:35:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
|
/// Replaces the value at a mutable location with a new one, returning the old value, without
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// deinitializing either one.
|
2014-12-16 18:23:55 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// A simple example:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// let mut v: Vec<i32> = vec![1, 2];
|
2014-12-16 18:23:55 -05:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// let old_v = mem::replace(&mut v, vec![3, 4, 5]);
|
|
|
|
|
/// assert_eq!(2, old_v.len());
|
|
|
|
|
/// assert_eq!(3, v.len());
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// ```
|
2014-06-16 00:22:51 -07:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// `replace` allows consumption of a struct field by replacing it with another value.
|
|
|
|
|
/// Without `replace` you can run into issues like these:
|
2014-06-16 00:22:51 -07:00
|
|
|
|
///
|
2017-06-20 15:15:16 +08:00
|
|
|
|
/// ```compile_fail,E0507
|
2014-06-16 00:22:51 -07:00
|
|
|
|
/// struct Buffer<T> { buf: Vec<T> }
|
|
|
|
|
///
|
|
|
|
|
/// impl<T> Buffer<T> {
|
|
|
|
|
/// fn get_and_reset(&mut self) -> Vec<T> {
|
|
|
|
|
/// // error: cannot move out of dereference of `&mut`-pointer
|
|
|
|
|
/// let buf = self.buf;
|
|
|
|
|
/// self.buf = Vec::new();
|
|
|
|
|
/// buf
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2016-11-11 18:49:16 +01:00
|
|
|
|
/// Note that `T` does not necessarily implement [`Clone`], so it can't even clone and reset
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// `self.buf`. But `replace` can be used to disassociate the original value of `self.buf` from
|
|
|
|
|
/// `self`, allowing it to be returned:
|
2014-06-16 00:22:51 -07:00
|
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
|
/// ```
|
2015-11-03 15:27:03 +00:00
|
|
|
|
/// # #![allow(dead_code)]
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// use std::mem;
|
2016-09-08 20:04:05 -07:00
|
|
|
|
///
|
2014-06-16 00:22:51 -07:00
|
|
|
|
/// # struct Buffer<T> { buf: Vec<T> }
|
|
|
|
|
/// impl<T> Buffer<T> {
|
|
|
|
|
/// fn get_and_reset(&mut self) -> Vec<T> {
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// mem::replace(&mut self.buf, Vec::new())
|
2014-06-16 00:22:51 -07:00
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-11-11 18:49:16 +01:00
|
|
|
|
///
|
|
|
|
|
/// [`Clone`]: ../../std/clone/trait.Clone.html
|
2014-02-01 04:35:36 +08:00
|
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-02-01 04:35:36 +08:00
|
|
|
|
pub fn replace<T>(dest: &mut T, mut src: T) -> T {
|
|
|
|
|
swap(dest, &mut src);
|
|
|
|
|
src
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Disposes of a value.
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// While this does call the argument's implementation of [`Drop`][drop],
|
|
|
|
|
/// it will not release any borrows, as borrows are based on lexical scope.
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
|
///
|
2015-09-21 08:10:30 +05:30
|
|
|
|
/// This effectively does nothing for
|
2017-03-15 12:13:55 -04:00
|
|
|
|
/// [types which implement `Copy`](../../book/first-edition/ownership.html#copy-types),
|
2015-09-21 08:10:30 +05:30
|
|
|
|
/// e.g. integers. Such values are copied and _then_ moved into the function,
|
|
|
|
|
/// so the value persists after this function call.
|
|
|
|
|
///
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// This function is not magic; it is literally defined as
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// pub fn drop<T>(_x: T) { }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// Because `_x` is moved into the function, it is automatically dropped before
|
|
|
|
|
/// the function returns.
|
|
|
|
|
///
|
|
|
|
|
/// [drop]: ../ops/trait.Drop.html
|
|
|
|
|
///
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// # Examples
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
|
///
|
2015-06-29 16:31:06 -04:00
|
|
|
|
/// Basic usage:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// let v = vec![1, 2, 3];
|
|
|
|
|
///
|
|
|
|
|
/// drop(v); // explicitly drop the vector
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// Borrows are based on lexical scope, so this produces an error:
|
|
|
|
|
///
|
2017-06-20 15:15:16 +08:00
|
|
|
|
/// ```compile_fail,E0502
|
2015-06-29 16:31:06 -04:00
|
|
|
|
/// let mut v = vec![1, 2, 3];
|
|
|
|
|
/// let x = &v[0];
|
|
|
|
|
///
|
|
|
|
|
/// drop(x); // explicitly drop the reference, but the borrow still exists
|
|
|
|
|
///
|
|
|
|
|
/// v.push(4); // error: cannot borrow `v` as mutable because it is also
|
|
|
|
|
/// // borrowed as immutable
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// An inner scope is needed to fix this:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// let mut v = vec![1, 2, 3];
|
|
|
|
|
///
|
|
|
|
|
/// {
|
|
|
|
|
/// let x = &v[0];
|
|
|
|
|
///
|
|
|
|
|
/// drop(x); // this is now redundant, as `x` is going out of scope anyway
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// v.push(4); // no problems
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2016-11-11 18:49:16 +01:00
|
|
|
|
/// Since [`RefCell`] enforces the borrow rules at runtime, `drop` can
|
|
|
|
|
/// release a [`RefCell`] borrow:
|
2015-06-29 16:31:06 -04:00
|
|
|
|
///
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
|
/// ```
|
|
|
|
|
/// use std::cell::RefCell;
|
|
|
|
|
///
|
2015-01-22 14:08:56 +00:00
|
|
|
|
/// let x = RefCell::new(1);
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
|
///
|
|
|
|
|
/// let mut mutable_borrow = x.borrow_mut();
|
|
|
|
|
/// *mutable_borrow = 1;
|
2014-12-16 18:23:55 -05:00
|
|
|
|
///
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
|
/// drop(mutable_borrow); // relinquish the mutable borrow on this slot
|
|
|
|
|
///
|
|
|
|
|
/// let borrow = x.borrow();
|
|
|
|
|
/// println!("{}", *borrow);
|
|
|
|
|
/// ```
|
2015-09-21 08:10:30 +05:30
|
|
|
|
///
|
2016-11-11 18:49:16 +01:00
|
|
|
|
/// Integers and other types implementing [`Copy`] are unaffected by `drop`.
|
2015-09-21 08:10:30 +05:30
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #[derive(Copy, Clone)]
|
|
|
|
|
/// struct Foo(u8);
|
|
|
|
|
///
|
|
|
|
|
/// let x = 1;
|
|
|
|
|
/// let y = Foo(2);
|
|
|
|
|
/// drop(x); // a copy of `x` is moved and dropped
|
|
|
|
|
/// drop(y); // a copy of `y` is moved and dropped
|
|
|
|
|
///
|
|
|
|
|
/// println!("x: {}, y: {}", x, y.0); // still available
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2016-11-11 18:49:16 +01:00
|
|
|
|
/// [`RefCell`]: ../../std/cell/struct.RefCell.html
|
|
|
|
|
/// [`Copy`]: ../../std/marker/trait.Copy.html
|
2014-02-01 04:35:36 +08:00
|
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-02-01 04:35:36 +08:00
|
|
|
|
pub fn drop<T>(_x: T) { }
|
|
|
|
|
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// Interprets `src` as having type `&U`, and then reads `src` without moving
|
|
|
|
|
/// the contained value.
|
2015-03-31 10:26:25 -07:00
|
|
|
|
///
|
|
|
|
|
/// This function will unsafely assume the pointer `src` is valid for
|
2017-03-12 14:04:52 -04:00
|
|
|
|
/// [`size_of::<U>`][size_of] bytes by transmuting `&T` to `&U` and then reading
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// the `&U`. It will also unsafely create a copy of the contained value instead of
|
|
|
|
|
/// moving out of `src`.
|
2015-03-31 10:26:25 -07:00
|
|
|
|
///
|
|
|
|
|
/// It is not a compile-time error if `T` and `U` have different sizes, but it
|
|
|
|
|
/// is highly encouraged to only invoke this function where `T` and `U` have the
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// same size. This function triggers [undefined behavior][ub] if `U` is larger than
|
2015-03-31 10:26:25 -07:00
|
|
|
|
/// `T`.
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
|
///
|
2017-02-21 01:15:29 -05:00
|
|
|
|
/// [ub]: ../../reference/behavior-considered-undefined.html
|
2016-09-08 20:04:05 -07:00
|
|
|
|
/// [size_of]: fn.size_of.html
|
|
|
|
|
///
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// # Examples
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
|
///
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// ```
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
2016-02-16 21:43:49 -05:00
|
|
|
|
/// #[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);
|
|
|
|
|
/// }
|
2014-12-16 18:23:55 -05:00
|
|
|
|
///
|
2016-02-16 21:43:49 -05:00
|
|
|
|
/// // The contents of 'foo_slice' should not have changed
|
|
|
|
|
/// assert_eq!(foo_slice, [10]);
|
2014-12-16 18:23:55 -05:00
|
|
|
|
/// ```
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
|
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
|
2014-06-25 12:47:34 -07:00
|
|
|
|
ptr::read(src as *const T as *const U)
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
|
}
|
2016-06-29 12:15:59 -04:00
|
|
|
|
|
|
|
|
|
/// Opaque type representing the discriminant of an enum.
|
|
|
|
|
///
|
|
|
|
|
/// See the `discriminant` function in this module for more information.
|
2017-09-13 17:29:55 -04:00
|
|
|
|
#[stable(feature = "discriminant_value", since = "1.21.0")]
|
2016-06-29 12:15:59 -04:00
|
|
|
|
pub struct Discriminant<T>(u64, PhantomData<*const T>);
|
|
|
|
|
|
|
|
|
|
// N.B. These trait implementations cannot be derived because we don't want any bounds on T.
|
|
|
|
|
|
2017-09-13 17:29:55 -04:00
|
|
|
|
#[stable(feature = "discriminant_value", since = "1.21.0")]
|
2016-06-29 12:15:59 -04:00
|
|
|
|
impl<T> Copy for Discriminant<T> {}
|
|
|
|
|
|
2017-09-13 17:29:55 -04:00
|
|
|
|
#[stable(feature = "discriminant_value", since = "1.21.0")]
|
2016-06-29 12:15:59 -04:00
|
|
|
|
impl<T> clone::Clone for Discriminant<T> {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
*self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-13 17:29:55 -04:00
|
|
|
|
#[stable(feature = "discriminant_value", since = "1.21.0")]
|
2016-06-29 12:15:59 -04:00
|
|
|
|
impl<T> cmp::PartialEq for Discriminant<T> {
|
|
|
|
|
fn eq(&self, rhs: &Self) -> bool {
|
|
|
|
|
self.0 == rhs.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-13 17:29:55 -04:00
|
|
|
|
#[stable(feature = "discriminant_value", since = "1.21.0")]
|
2016-06-29 12:15:59 -04:00
|
|
|
|
impl<T> cmp::Eq for Discriminant<T> {}
|
|
|
|
|
|
2017-09-13 17:29:55 -04:00
|
|
|
|
#[stable(feature = "discriminant_value", since = "1.21.0")]
|
2016-06-29 12:15:59 -04:00
|
|
|
|
impl<T> hash::Hash for Discriminant<T> {
|
|
|
|
|
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
|
|
|
|
self.0.hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-13 17:29:55 -04:00
|
|
|
|
#[stable(feature = "discriminant_value", since = "1.21.0")]
|
2016-06-29 12:15:59 -04:00
|
|
|
|
impl<T> fmt::Debug for Discriminant<T> {
|
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
fmt.debug_tuple("Discriminant")
|
|
|
|
|
.field(&self.0)
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a value uniquely identifying the enum variant in `v`.
|
|
|
|
|
///
|
|
|
|
|
/// If `T` is not an enum, calling this function will not result in undefined behavior, but the
|
|
|
|
|
/// return value is unspecified.
|
|
|
|
|
///
|
|
|
|
|
/// # Stability
|
|
|
|
|
///
|
|
|
|
|
/// The discriminant of an enum variant may change if the enum definition changes. A discriminant
|
|
|
|
|
/// of some variant will not change between compilations with the same compiler.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// This can be used to compare enums that carry data, while disregarding
|
|
|
|
|
/// the actual data:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::mem;
|
|
|
|
|
///
|
|
|
|
|
/// enum Foo { A(&'static str), B(i32), C(i32) }
|
|
|
|
|
///
|
|
|
|
|
/// assert!(mem::discriminant(&Foo::A("bar")) == mem::discriminant(&Foo::A("baz")));
|
|
|
|
|
/// assert!(mem::discriminant(&Foo::B(1)) == mem::discriminant(&Foo::B(2)));
|
|
|
|
|
/// assert!(mem::discriminant(&Foo::B(3)) != mem::discriminant(&Foo::C(3)));
|
|
|
|
|
/// ```
|
2017-09-13 17:29:55 -04:00
|
|
|
|
#[stable(feature = "discriminant_value", since = "1.21.0")]
|
2016-06-29 12:15:59 -04:00
|
|
|
|
pub fn discriminant<T>(v: &T) -> Discriminant<T> {
|
|
|
|
|
unsafe {
|
|
|
|
|
Discriminant(intrinsics::discriminant_value(v), PhantomData)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-22 23:35:09 +02:00
|
|
|
|
|
|
|
|
|
/// A wrapper to inhibit compiler from automatically calling `T`’s destructor.
|
|
|
|
|
///
|
|
|
|
|
/// This wrapper is 0-cost.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// This wrapper helps with explicitly documenting the drop order dependencies between fields of
|
|
|
|
|
/// the type:
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// use std::mem::ManuallyDrop;
|
|
|
|
|
/// struct Peach;
|
|
|
|
|
/// struct Banana;
|
|
|
|
|
/// struct Melon;
|
|
|
|
|
/// struct FruitBox {
|
|
|
|
|
/// // Immediately clear there’s something non-trivial going on with these fields.
|
|
|
|
|
/// peach: ManuallyDrop<Peach>,
|
|
|
|
|
/// melon: Melon, // Field that’s independent of the other two.
|
|
|
|
|
/// banana: ManuallyDrop<Banana>,
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// impl Drop for FruitBox {
|
|
|
|
|
/// fn drop(&mut self) {
|
|
|
|
|
/// unsafe {
|
|
|
|
|
/// // Explicit ordering in which field destructors are run specified in the intuitive
|
|
|
|
|
/// // location – the destructor of the structure containing the fields.
|
|
|
|
|
/// // Moreover, one can now reorder fields within the struct however much they want.
|
|
|
|
|
/// ManuallyDrop::drop(&mut self.peach);
|
|
|
|
|
/// ManuallyDrop::drop(&mut self.banana);
|
|
|
|
|
/// }
|
|
|
|
|
/// // After destructor for `FruitBox` runs (this function), the destructor for Melon gets
|
|
|
|
|
/// // invoked in the usual manner, as it is not wrapped in `ManuallyDrop`.
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2017-07-20 15:48:15 -07:00
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
2017-03-22 23:35:09 +02:00
|
|
|
|
#[allow(unions_with_drop_fields)]
|
2017-09-03 23:27:20 -07:00
|
|
|
|
#[derive(Copy)]
|
2017-03-22 23:35:09 +02:00
|
|
|
|
pub union ManuallyDrop<T>{ value: T }
|
|
|
|
|
|
|
|
|
|
impl<T> ManuallyDrop<T> {
|
|
|
|
|
/// Wrap a value to be manually dropped.
|
2017-04-02 11:15:59 +03:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// use std::mem::ManuallyDrop;
|
|
|
|
|
/// ManuallyDrop::new(Box::new(()));
|
|
|
|
|
/// ```
|
2017-07-20 15:48:15 -07:00
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
2017-04-02 11:13:31 +03:00
|
|
|
|
#[inline]
|
2017-03-22 23:35:09 +02:00
|
|
|
|
pub fn new(value: T) -> ManuallyDrop<T> {
|
|
|
|
|
ManuallyDrop { value: value }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Extract the value from the ManuallyDrop container.
|
2017-04-02 11:15:59 +03:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// use std::mem::ManuallyDrop;
|
|
|
|
|
/// let x = ManuallyDrop::new(Box::new(()));
|
|
|
|
|
/// let _: Box<()> = ManuallyDrop::into_inner(x);
|
|
|
|
|
/// ```
|
2017-07-20 15:48:15 -07:00
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
2017-04-02 11:13:31 +03:00
|
|
|
|
#[inline]
|
2017-04-02 11:15:59 +03:00
|
|
|
|
pub fn into_inner(slot: ManuallyDrop<T>) -> T {
|
2017-03-22 23:35:09 +02:00
|
|
|
|
unsafe {
|
2017-04-02 11:15:59 +03:00
|
|
|
|
slot.value
|
2017-03-22 23:35:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Manually drops the contained value.
|
|
|
|
|
///
|
2017-08-24 17:33:36 +02:00
|
|
|
|
/// # Safety
|
2017-03-22 23:35:09 +02:00
|
|
|
|
///
|
|
|
|
|
/// This function runs the destructor of the contained value and thus the wrapped value
|
|
|
|
|
/// now represents uninitialized data. It is up to the user of this method to ensure the
|
|
|
|
|
/// uninitialized data is not actually used.
|
2017-07-20 15:48:15 -07:00
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
2017-04-02 11:13:31 +03:00
|
|
|
|
#[inline]
|
2017-03-22 23:35:09 +02:00
|
|
|
|
pub unsafe fn drop(slot: &mut ManuallyDrop<T>) {
|
|
|
|
|
ptr::drop_in_place(&mut slot.value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 15:48:15 -07:00
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
2017-09-04 02:29:16 -07:00
|
|
|
|
impl<T> Deref for ManuallyDrop<T> {
|
2017-03-22 23:35:09 +02:00
|
|
|
|
type Target = T;
|
2017-04-02 11:13:31 +03:00
|
|
|
|
#[inline]
|
2017-03-22 23:35:09 +02:00
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
unsafe {
|
|
|
|
|
&self.value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 15:48:15 -07:00
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
2017-09-04 02:29:16 -07:00
|
|
|
|
impl<T> DerefMut for ManuallyDrop<T> {
|
2017-04-02 11:13:31 +03:00
|
|
|
|
#[inline]
|
2017-03-22 23:35:09 +02:00
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
|
unsafe {
|
|
|
|
|
&mut self.value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 15:48:15 -07:00
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
2017-03-22 23:35:09 +02:00
|
|
|
|
impl<T: ::fmt::Debug> ::fmt::Debug for ManuallyDrop<T> {
|
|
|
|
|
fn fmt(&self, fmt: &mut ::fmt::Formatter) -> ::fmt::Result {
|
|
|
|
|
unsafe {
|
|
|
|
|
fmt.debug_tuple("ManuallyDrop").field(&self.value).finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-09 00:47:38 +02:00
|
|
|
|
|
2017-09-03 23:27:20 -07:00
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
|
|
|
|
impl<T: Clone> Clone for ManuallyDrop<T> {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
ManuallyDrop::new(self.deref().clone())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn clone_from(&mut self, source: &Self) {
|
|
|
|
|
self.deref_mut().clone_from(source);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-04 02:29:16 -07:00
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
|
|
|
|
impl<T: Default> Default for ManuallyDrop<T> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
ManuallyDrop::new(Default::default())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
|
|
|
|
impl<T: PartialEq> PartialEq for ManuallyDrop<T> {
|
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
self.deref().eq(other)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ne(&self, other: &Self) -> bool {
|
|
|
|
|
self.deref().ne(other)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
|
|
|
|
impl<T: Eq> Eq for ManuallyDrop<T> {}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
|
|
|
|
impl<T: PartialOrd> PartialOrd for ManuallyDrop<T> {
|
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<::cmp::Ordering> {
|
|
|
|
|
self.deref().partial_cmp(other)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn lt(&self, other: &Self) -> bool {
|
|
|
|
|
self.deref().lt(other)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn le(&self, other: &Self) -> bool {
|
|
|
|
|
self.deref().le(other)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn gt(&self, other: &Self) -> bool {
|
|
|
|
|
self.deref().gt(other)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ge(&self, other: &Self) -> bool {
|
|
|
|
|
self.deref().ge(other)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
|
|
|
|
impl<T: Ord> Ord for ManuallyDrop<T> {
|
|
|
|
|
fn cmp(&self, other: &Self) -> ::cmp::Ordering {
|
|
|
|
|
self.deref().cmp(other)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
|
|
|
|
impl<T: ::hash::Hash> ::hash::Hash for ManuallyDrop<T> {
|
|
|
|
|
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
|
|
|
|
|
self.deref().hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 00:47:38 +02:00
|
|
|
|
/// Tells LLVM that this point in the code is not reachable, enabling further
|
|
|
|
|
/// optimizations.
|
|
|
|
|
///
|
|
|
|
|
/// NB: This is very different from the `unreachable!()` macro: Unlike the
|
|
|
|
|
/// macro, which panics when it is executed, it is *undefined behavior* to
|
|
|
|
|
/// reach code marked with this function.
|
2017-08-11 03:42:36 +02:00
|
|
|
|
#[inline]
|
2017-08-09 01:03:50 +02:00
|
|
|
|
#[unstable(feature = "unreachable", issue = "43751")]
|
2017-08-09 00:47:38 +02:00
|
|
|
|
pub unsafe fn unreachable() -> ! {
|
|
|
|
|
intrinsics::unreachable()
|
|
|
|
|
}
|