1
Fork 0

rollup merge of #19942: steveklabnik/doc_std_mem

This commit is contained in:
Alex Crichton 2014-12-17 08:35:50 -08:00
commit cbc3cf7a9e

View file

@ -33,6 +33,14 @@ pub use intrinsics::transmute;
pub use intrinsics::forget; pub use intrinsics::forget;
/// Returns the size of a type in bytes. /// Returns the size of a type in bytes.
///
/// # Examples
///
/// ```
/// use std::mem;
///
/// assert_eq!(4, mem::size_of::<i32>());
/// ```
#[inline] #[inline]
#[stable] #[stable]
pub fn size_of<T>() -> uint { pub fn size_of<T>() -> uint {
@ -40,6 +48,14 @@ pub fn size_of<T>() -> uint {
} }
/// Returns the size of the type that `_val` points to in bytes. /// Returns the size of the type that `_val` points to in bytes.
///
/// # Examples
///
/// ```
/// use std::mem;
///
/// assert_eq!(4, mem::size_of_val(&5i32));
/// ```
#[inline] #[inline]
#[stable] #[stable]
pub fn size_of_val<T>(_val: &T) -> uint { pub fn size_of_val<T>(_val: &T) -> uint {
@ -48,16 +64,30 @@ pub fn size_of_val<T>(_val: &T) -> uint {
/// Returns the ABI-required minimum alignment of a type /// Returns the ABI-required minimum alignment of a type
/// ///
/// This is the alignment used for struct fields. It may be smaller /// This is the alignment used for struct fields. It may be smaller than the preferred alignment.
/// than the preferred alignment. ///
/// # Examples
///
/// ```
/// use std::mem;
///
/// assert_eq!(4, mem::min_align_of::<i32>());
/// ```
#[inline] #[inline]
#[stable] #[stable]
pub fn min_align_of<T>() -> uint { pub fn min_align_of<T>() -> uint {
unsafe { intrinsics::min_align_of::<T>() } unsafe { intrinsics::min_align_of::<T>() }
} }
/// Returns the ABI-required minimum alignment of the type of the value that /// Returns the ABI-required minimum alignment of the type of the value that `_val` points to
/// `_val` points to ///
/// # Examples
///
/// ```
/// use std::mem;
///
/// assert_eq!(4, mem::min_align_of_val(&5i32));
/// ```
#[inline] #[inline]
#[stable] #[stable]
pub fn min_align_of_val<T>(_val: &T) -> uint { pub fn min_align_of_val<T>(_val: &T) -> uint {
@ -66,9 +96,16 @@ pub fn min_align_of_val<T>(_val: &T) -> uint {
/// Returns the alignment in memory for a type. /// Returns the alignment in memory for a type.
/// ///
/// This function will return the alignment, in bytes, of a type in memory. If /// This function will return the alignment, in bytes, of a type in memory. If the alignment
/// the alignment returned is adhered to, then the type is guaranteed to /// returned is adhered to, then the type is guaranteed to function properly.
/// function properly. ///
/// # Examples
///
/// ```
/// use std::mem;
///
/// assert_eq!(4, mem::align_of::<i32>());
/// ```
#[inline] #[inline]
#[stable] #[stable]
pub fn align_of<T>() -> uint { pub fn align_of<T>() -> uint {
@ -81,9 +118,16 @@ pub fn align_of<T>() -> uint {
/// Returns the alignment of the type of the value that `_val` points to. /// Returns the alignment of the type of the value that `_val` points to.
/// ///
/// This is similar to `align_of`, but function will properly handle types such /// This is similar to `align_of`, but function will properly handle types such as trait objects
/// as trait objects (in the future), returning the alignment for an arbitrary /// (in the future), returning the alignment for an arbitrary value at runtime.
/// value at runtime. ///
/// # Examples
///
/// ```
/// use std::mem;
///
/// assert_eq!(4, mem::align_of_val(&5i32));
/// ```
#[inline] #[inline]
#[stable] #[stable]
pub fn align_of_val<T>(_val: &T) -> uint { pub fn align_of_val<T>(_val: &T) -> uint {
@ -92,15 +136,22 @@ pub fn align_of_val<T>(_val: &T) -> uint {
/// Create a value initialized to zero. /// Create a value initialized to zero.
/// ///
/// This function is similar to allocating space for a local variable and /// This function is similar to allocating space for a local variable and zeroing it out (an unsafe
/// zeroing it out (an unsafe operation). /// operation).
/// ///
/// Care must be taken when using this function, if the type `T` has a /// Care must be taken when using this function, if the type `T` has a destructor and the value
/// destructor and the value falls out of scope (due to unwinding or returning) /// falls out of scope (due to unwinding or returning) before being initialized, then the
/// before being initialized, then the destructor will run on zeroed /// destructor will run on zeroed data, likely leading to crashes.
/// data, likely leading to crashes.
/// ///
/// This is useful for FFI functions sometimes, but should generally be avoided. /// This is useful for FFI functions sometimes, but should generally be avoided.
///
/// # Examples
///
/// ```
/// use std::mem;
///
/// let x: int = unsafe { mem::zeroed() };
/// ```
#[inline] #[inline]
#[stable] #[stable]
pub unsafe fn zeroed<T>() -> T { pub unsafe fn zeroed<T>() -> T {
@ -109,20 +160,41 @@ pub unsafe fn zeroed<T>() -> T {
/// Create an uninitialized value. /// Create an uninitialized value.
/// ///
/// Care must be taken when using this function, if the type `T` has a /// Care must be taken when using this function, if the type `T` has a destructor and the value
/// destructor and the value falls out of scope (due to unwinding or returning) /// falls out of scope (due to unwinding or returning) before being initialized, then the
/// before being initialized, then the destructor will run on uninitialized /// destructor will run on uninitialized data, likely leading to crashes.
/// data, likely leading to crashes.
/// ///
/// This is useful for FFI functions sometimes, but should generally be avoided. /// This is useful for FFI functions sometimes, but should generally be avoided.
///
/// # Examples
///
/// ```
/// use std::mem;
///
/// let x: int = unsafe { mem::uninitialized() };
/// ```
#[inline] #[inline]
#[stable] #[stable]
pub unsafe fn uninitialized<T>() -> T { pub unsafe fn uninitialized<T>() -> T {
intrinsics::uninit() intrinsics::uninit()
} }
/// Swap the values at two mutable locations of the same type, without /// Swap the values at two mutable locations of the same type, without deinitialising or copying
/// deinitialising or copying either one. /// either one.
///
/// # Examples
///
/// ```
/// use std::mem;
///
/// let x = &mut 5i;
/// let y = &mut 42i;
///
/// mem::swap(x, y);
///
/// assert_eq!(42i, *x);
/// assert_eq!(5i, *y);
/// ```
#[inline] #[inline]
#[stable] #[stable]
pub fn swap<T>(x: &mut T, y: &mut T) { pub fn swap<T>(x: &mut T, y: &mut T) {
@ -141,13 +213,26 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
} }
} }
/// Replace the value at a mutable location with a new one, returning the old /// Replace the value at a mutable location with a new one, returning the old value, without
/// value, without deinitialising or copying either one. /// deinitialising or copying either one.
/// ///
/// This is primarily used for transferring and swapping ownership of a value /// This is primarily used for transferring and swapping ownership of a value in a mutable
/// in a mutable location. For example, this function allows consumption of /// location.
/// one field of a struct by replacing it with another value. The normal approach ///
/// doesn't always work: /// # Examples
///
/// A simple example:
///
/// ```
/// use std::mem;
///
/// let mut v: Vec<i32> = Vec::new();
///
/// mem::replace(&mut v, Vec::new());
/// ```
///
/// This function allows consumption of one field of a struct by replacing it with another value.
/// The normal approach doesn't always work:
/// ///
/// ```rust,ignore /// ```rust,ignore
/// struct Buffer<T> { buf: Vec<T> } /// struct Buffer<T> { buf: Vec<T> }
@ -162,16 +247,16 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
/// } /// }
/// ``` /// ```
/// ///
/// Note that `T` does not necessarily implement `Clone`, so it can't even /// Note that `T` does not necessarily implement `Clone`, so it can't even clone and reset
/// clone and reset `self.buf`. But `replace` can be used to disassociate /// `self.buf`. But `replace` can be used to disassociate the original value of `self.buf` from
/// the original value of `self.buf` from `self`, allowing it to be returned: /// `self`, allowing it to be returned:
/// ///
/// ```rust /// ```rust
/// use std::mem;
/// # struct Buffer<T> { buf: Vec<T> } /// # struct Buffer<T> { buf: Vec<T> }
/// impl<T> Buffer<T> { /// impl<T> Buffer<T> {
/// fn get_and_reset(&mut self) -> Vec<T> { /// fn get_and_reset(&mut self) -> Vec<T> {
/// use std::mem::replace; /// mem::replace(&mut self.buf, Vec::new())
/// replace(&mut self.buf, Vec::new())
/// } /// }
/// } /// }
/// ``` /// ```
@ -184,10 +269,10 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
/// Disposes of a value. /// Disposes of a value.
/// ///
/// This function can be used to destroy any value by allowing `drop` to take /// This function can be used to destroy any value by allowing `drop` to take ownership of its
/// ownership of its argument. /// argument.
/// ///
/// # Example /// # Examples
/// ///
/// ``` /// ```
/// use std::cell::RefCell; /// use std::cell::RefCell;
@ -196,6 +281,7 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
/// ///
/// let mut mutable_borrow = x.borrow_mut(); /// let mut mutable_borrow = x.borrow_mut();
/// *mutable_borrow = 1; /// *mutable_borrow = 1;
///
/// drop(mutable_borrow); // relinquish the mutable borrow on this slot /// drop(mutable_borrow); // relinquish the mutable borrow on this slot
/// ///
/// let borrow = x.borrow(); /// let borrow = x.borrow();
@ -205,18 +291,25 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
#[stable] #[stable]
pub fn drop<T>(_x: T) { } pub fn drop<T>(_x: T) { }
/// Interprets `src` as `&U`, and then reads `src` without moving the contained /// Interprets `src` as `&U`, and then reads `src` without moving the contained value.
/// value.
/// ///
/// This function will unsafely assume the pointer `src` is valid for /// This function will unsafely assume the pointer `src` is valid for `sizeof(U)` bytes by
/// `sizeof(U)` bytes by transmuting `&T` to `&U` and then reading the `&U`. It /// transmuting `&T` to `&U` and then reading the `&U`. It will also unsafely create a copy of the
/// will also unsafely create a copy of the contained value instead of moving /// contained value instead of moving out of `src`.
/// out of `src`.
/// ///
/// It is not a compile-time error if `T` and `U` have different sizes, but it /// It is not a compile-time error if `T` and `U` have different sizes, but it is highly encouraged
/// is highly encouraged to only invoke this function where `T` and `U` have the /// to only invoke this function where `T` and `U` have the same size. This function triggers
/// same size. This function triggers undefined behavior if `U` is larger than /// undefined behavior if `U` is larger than `T`.
/// `T`. ///
/// # Examples
///
/// ```
/// use std::mem;
///
/// let one = unsafe { mem::transmute_copy(&1i) };
///
/// assert_eq!(1u, one);
/// ```
#[inline] #[inline]
#[stable] #[stable]
pub unsafe fn transmute_copy<T, U>(src: &T) -> U { pub unsafe fn transmute_copy<T, U>(src: &T) -> U {