2014-02-18 14:30:21 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-10-16 20:34:01 -05: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.
|
|
|
|
|
2014-02-09 02:23:04 -06:00
|
|
|
//! Basic functions for dealing with memory
|
|
|
|
//!
|
|
|
|
//! This module contains functions for querying the size and alignment of
|
|
|
|
//! types, initializing and manipulating memory.
|
2013-10-16 20:34:01 -05:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2014-12-15 21:40:25 -06:00
|
|
|
|
2015-01-06 16:33:42 -06:00
|
|
|
use marker::Sized;
|
2014-02-16 01:49:08 -06:00
|
|
|
use intrinsics;
|
2014-06-16 13:25:47 -05:00
|
|
|
use ptr;
|
2013-10-16 20:34:01 -05:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-06-12 16:08:44 -05:00
|
|
|
pub use intrinsics::transmute;
|
|
|
|
|
2014-08-14 01:15:09 -05:00
|
|
|
/// Moves a thing into the void.
|
|
|
|
///
|
|
|
|
/// The forget function will take ownership of the provided value but neglect
|
|
|
|
/// to run any required cleanup or memory management operations on it.
|
|
|
|
///
|
|
|
|
/// This function is the unsafe version of the `drop` function because it does
|
|
|
|
/// not run any destructors.
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-08-14 01:15:09 -05:00
|
|
|
pub use intrinsics::forget;
|
|
|
|
|
2014-02-18 14:30:21 -06:00
|
|
|
/// Returns the size of a type in bytes.
|
2014-12-16 17:23:55 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
|
|
|
/// assert_eq!(4, mem::size_of::<i32>());
|
|
|
|
/// ```
|
2013-10-16 20:34:01 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-16 07:38:50 -06:00
|
|
|
pub fn size_of<T>() -> usize {
|
2013-10-16 20:34:01 -05:00
|
|
|
unsafe { intrinsics::size_of::<T>() }
|
|
|
|
}
|
|
|
|
|
2014-02-18 14:30:21 -06:00
|
|
|
/// Returns the size of the type that `_val` points to in bytes.
|
2014-12-16 17:23:55 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
|
|
|
/// assert_eq!(4, mem::size_of_val(&5i32));
|
|
|
|
/// ```
|
2013-10-16 20:34:01 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-16 07:38:50 -06:00
|
|
|
pub fn size_of_val<T>(_val: &T) -> usize {
|
2013-10-16 20:34:01 -05:00
|
|
|
size_of::<T>()
|
|
|
|
}
|
|
|
|
|
2013-10-21 14:41:32 -05:00
|
|
|
/// Returns the ABI-required minimum alignment of a type
|
|
|
|
///
|
2014-12-16 17:23:55 -06:00
|
|
|
/// This is the alignment used for struct fields. It may be smaller than the preferred alignment.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
|
|
|
/// assert_eq!(4, mem::min_align_of::<i32>());
|
|
|
|
/// ```
|
2013-10-16 20:34:01 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-16 07:38:50 -06:00
|
|
|
pub fn min_align_of<T>() -> usize {
|
2013-10-16 20:34:01 -05:00
|
|
|
unsafe { intrinsics::min_align_of::<T>() }
|
|
|
|
}
|
|
|
|
|
2014-12-16 17:23:55 -06:00
|
|
|
/// Returns the ABI-required minimum alignment of the type of the value that `_val` points to
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
|
|
|
/// assert_eq!(4, mem::min_align_of_val(&5i32));
|
|
|
|
/// ```
|
2013-10-16 20:34:01 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-16 07:38:50 -06:00
|
|
|
pub fn min_align_of_val<T>(_val: &T) -> usize {
|
2013-10-16 20:34:01 -05:00
|
|
|
min_align_of::<T>()
|
|
|
|
}
|
|
|
|
|
2014-05-17 02:56:00 -05:00
|
|
|
/// Returns the alignment in memory for a type.
|
|
|
|
///
|
2014-12-16 17:23:55 -06:00
|
|
|
/// This function will return the alignment, in bytes, of a type in memory. If the alignment
|
|
|
|
/// returned is adhered to, then the type is guaranteed to function properly.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
|
|
|
/// assert_eq!(4, mem::align_of::<i32>());
|
|
|
|
/// ```
|
2013-10-16 20:34:01 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-16 07:38:50 -06:00
|
|
|
pub fn align_of<T>() -> usize {
|
2014-05-17 02:56:00 -05:00
|
|
|
// We use the preferred alignment as the default alignment for a type. This
|
|
|
|
// appears to be what clang migrated towards as well:
|
|
|
|
//
|
|
|
|
// http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20110725/044411.html
|
2013-10-16 20:34:01 -05:00
|
|
|
unsafe { intrinsics::pref_align_of::<T>() }
|
|
|
|
}
|
|
|
|
|
2014-05-17 02:56:00 -05:00
|
|
|
/// Returns the alignment of the type of the value that `_val` points to.
|
|
|
|
///
|
2014-12-16 17:23:55 -06:00
|
|
|
/// This is similar to `align_of`, but function will properly handle types such as trait objects
|
|
|
|
/// (in the future), returning the alignment for an arbitrary value at runtime.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
|
|
|
/// assert_eq!(4, mem::align_of_val(&5i32));
|
|
|
|
/// ```
|
2013-10-16 20:34:01 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-16 07:38:50 -06:00
|
|
|
pub fn align_of_val<T>(_val: &T) -> usize {
|
2014-05-17 02:56:00 -05:00
|
|
|
align_of::<T>()
|
2013-10-16 20:34:01 -05:00
|
|
|
}
|
|
|
|
|
2014-02-08 04:46:55 -06:00
|
|
|
/// Create a value initialized to zero.
|
|
|
|
///
|
2014-12-16 17:23:55 -06:00
|
|
|
/// This function is similar to allocating space for a local variable and zeroing it out (an unsafe
|
|
|
|
/// operation).
|
2014-05-17 02:56:00 -05:00
|
|
|
///
|
2014-12-16 17:23:55 -06:00
|
|
|
/// Care must be taken when using this function, if the type `T` has a destructor and the value
|
|
|
|
/// falls out of scope (due to unwinding or returning) before being initialized, then the
|
|
|
|
/// destructor will run on zeroed data, likely leading to crashes.
|
2014-05-17 02:56:00 -05:00
|
|
|
///
|
|
|
|
/// This is useful for FFI functions sometimes, but should generally be avoided.
|
2014-12-16 17:23:55 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
2015-02-16 07:38:50 -06:00
|
|
|
/// let x: i32 = unsafe { mem::zeroed() };
|
2014-12-16 17:23:55 -06:00
|
|
|
/// ```
|
2014-02-08 04:46:55 -06:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-17 02:56:00 -05:00
|
|
|
pub unsafe fn zeroed<T>() -> T {
|
2014-02-08 04:46:55 -06:00
|
|
|
intrinsics::init()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create an uninitialized value.
|
2014-05-17 02:56:00 -05:00
|
|
|
///
|
2014-12-16 17:23:55 -06:00
|
|
|
/// Care must be taken when using this function, if the type `T` has a destructor and the value
|
|
|
|
/// falls out of scope (due to unwinding or returning) before being initialized, then the
|
|
|
|
/// destructor will run on uninitialized data, likely leading to crashes.
|
2014-05-17 02:56:00 -05:00
|
|
|
///
|
|
|
|
/// This is useful for FFI functions sometimes, but should generally be avoided.
|
2014-12-16 17:23:55 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
2015-02-16 07:38:50 -06:00
|
|
|
/// let x: i32 = unsafe { mem::uninitialized() };
|
2014-12-16 17:23:55 -06:00
|
|
|
/// ```
|
2014-02-08 04:46:55 -06:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-23 22:53:56 -05:00
|
|
|
pub unsafe fn uninitialized<T>() -> T {
|
|
|
|
intrinsics::uninit()
|
|
|
|
}
|
|
|
|
|
2014-12-16 17:23:55 -06:00
|
|
|
/// Swap the values at two mutable locations of the same type, without deinitialising or copying
|
|
|
|
/// either one.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let x = &mut 5;
|
|
|
|
/// let y = &mut 42;
|
2014-12-16 17:23:55 -06:00
|
|
|
///
|
|
|
|
/// mem::swap(x, y);
|
|
|
|
///
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert_eq!(42, *x);
|
|
|
|
/// assert_eq!(5, *y);
|
2014-12-16 17:23:55 -06:00
|
|
|
/// ```
|
2014-01-31 14:35:36 -06:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-01-31 14:35:36 -06:00
|
|
|
pub fn swap<T>(x: &mut T, y: &mut T) {
|
|
|
|
unsafe {
|
|
|
|
// Give ourselves some scratch space to work with
|
2014-05-23 22:53:56 -05:00
|
|
|
let mut t: T = uninitialized();
|
2014-01-31 14:35:36 -06:00
|
|
|
|
|
|
|
// Perform the swap, `&mut` pointers never alias
|
2015-02-23 13:39:16 -06:00
|
|
|
ptr::copy_nonoverlapping(&mut t, &*x, 1);
|
|
|
|
ptr::copy_nonoverlapping(x, &*y, 1);
|
|
|
|
ptr::copy_nonoverlapping(y, &t, 1);
|
2014-01-31 14:35:36 -06:00
|
|
|
|
2014-04-16 03:53:09 -05:00
|
|
|
// y and t now point to the same thing, but we need to completely forget `t`
|
2014-01-31 14:35:36 -06:00
|
|
|
// because it's no longer relevant.
|
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 12:34:51 -05:00
|
|
|
forget(t);
|
2014-01-31 14:35:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 17:23:55 -06:00
|
|
|
/// Replace the value at a mutable location with a new one, returning the old value, without
|
|
|
|
/// deinitialising or copying either one.
|
|
|
|
///
|
|
|
|
/// This is primarily used for transferring and swapping ownership of a value in a mutable
|
|
|
|
/// location.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// A simple example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
|
|
|
/// let mut v: Vec<i32> = Vec::new();
|
|
|
|
///
|
|
|
|
/// mem::replace(&mut v, Vec::new());
|
|
|
|
/// ```
|
2014-06-16 02:22:51 -05:00
|
|
|
///
|
2014-12-16 17:23:55 -06:00
|
|
|
/// This function allows consumption of one field of a struct by replacing it with another value.
|
|
|
|
/// The normal approach doesn't always work:
|
2014-06-16 02:22:51 -05:00
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// 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
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2014-12-16 17:23:55 -06:00
|
|
|
/// Note that `T` does not necessarily implement `Clone`, so it can't even clone and reset
|
|
|
|
/// `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 02:22:51 -05:00
|
|
|
///
|
2015-03-12 21:42:38 -05:00
|
|
|
/// ```
|
2014-12-16 17:23:55 -06:00
|
|
|
/// use std::mem;
|
2014-06-16 02:22:51 -05:00
|
|
|
/// # struct Buffer<T> { buf: Vec<T> }
|
|
|
|
/// impl<T> Buffer<T> {
|
|
|
|
/// fn get_and_reset(&mut self) -> Vec<T> {
|
2014-12-16 17:23:55 -06:00
|
|
|
/// mem::replace(&mut self.buf, Vec::new())
|
2014-06-16 02:22:51 -05:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2014-01-31 14:35:36 -06:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-01-31 14:35:36 -06: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 12:34:51 -05:00
|
|
|
///
|
2014-12-16 17:23:55 -06:00
|
|
|
/// This function can be used to destroy any value by allowing `drop` to take ownership of its
|
|
|
|
/// argument.
|
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 12:34:51 -05:00
|
|
|
///
|
2014-12-16 17:23:55 -06: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 12:34:51 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::cell::RefCell;
|
|
|
|
///
|
2015-01-22 08:08:56 -06: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 12:34:51 -05:00
|
|
|
///
|
|
|
|
/// let mut mutable_borrow = x.borrow_mut();
|
|
|
|
/// *mutable_borrow = 1;
|
2014-12-16 17:23:55 -06: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 12:34:51 -05:00
|
|
|
/// drop(mutable_borrow); // relinquish the mutable borrow on this slot
|
|
|
|
///
|
|
|
|
/// let borrow = x.borrow();
|
|
|
|
/// println!("{}", *borrow);
|
|
|
|
/// ```
|
2014-01-31 14:35:36 -06:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-01-31 14:35:36 -06:00
|
|
|
pub fn drop<T>(_x: T) { }
|
|
|
|
|
2014-12-16 17:23:55 -06:00
|
|
|
/// Interprets `src` as `&U`, and then reads `src` without moving the contained value.
|
|
|
|
///
|
|
|
|
/// This function will unsafely assume the pointer `src` is valid for `sizeof(U)` bytes by
|
|
|
|
/// transmuting `&T` to `&U` and then reading the `&U`. It will also unsafely create a copy of the
|
|
|
|
/// contained value instead of moving out of `src`.
|
|
|
|
///
|
|
|
|
/// 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 same size. This function triggers
|
|
|
|
/// undefined behavior if `U` is larger than `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 12:34:51 -05:00
|
|
|
///
|
2014-12-16 17:23:55 -06: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 12:34:51 -05:00
|
|
|
///
|
2014-12-16 17:23:55 -06:00
|
|
|
/// ```
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
2015-01-22 08:08:56 -06:00
|
|
|
/// let one = unsafe { mem::transmute_copy(&1) };
|
2014-12-16 17:23:55 -06:00
|
|
|
///
|
2015-01-22 08:08:56 -06:00
|
|
|
/// assert_eq!(1, one);
|
2014-12-16 17:23:55 -06: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 12:34:51 -05:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06: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 12:34:51 -05:00
|
|
|
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
|
2014-06-25 14:47:34 -05: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 12:34:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Transforms lifetime of the second pointer to match the first.
|
|
|
|
#[inline]
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "core",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "this function may be removed in the future due to its \
|
|
|
|
questionable utility")]
|
2015-01-05 15:16:49 -06:00
|
|
|
pub unsafe fn copy_lifetime<'a, S: ?Sized, T: ?Sized + 'a>(_ptr: &'a S,
|
2014-12-15 21:40:25 -06:00
|
|
|
ptr: &T) -> &'a 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 12:34:51 -05:00
|
|
|
transmute(ptr)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transforms lifetime of the second mutable pointer to match the first.
|
|
|
|
#[inline]
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "core",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "this function may be removed in the future due to its \
|
|
|
|
questionable utility")]
|
2015-02-05 14:19:11 -06:00
|
|
|
pub unsafe fn copy_mut_lifetime<'a, S: ?Sized, T: ?Sized + 'a>(_ptr: &'a S,
|
|
|
|
ptr: &mut T)
|
|
|
|
-> &'a mut 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 12:34:51 -05:00
|
|
|
transmute(ptr)
|
|
|
|
}
|