Skip to content

Commit 033a792

Browse files
committed
Document std::mem
1 parent 92e9e70 commit 033a792

File tree

1 file changed

+139
-46
lines changed

1 file changed

+139
-46
lines changed

src/libcore/mem.rs

+139-46
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,29 @@ pub use intrinsics::transmute;
2929
pub use intrinsics::forget;
3030

3131
/// Returns the size of a type in bytes.
32+
///
33+
/// # Examples
34+
///
35+
/// ```
36+
/// use std::mem;
37+
///
38+
/// assert_eq!(4, mem::size_of::<i32>());
39+
/// ```
3240
#[inline]
3341
#[stable]
3442
pub fn size_of<T>() -> uint {
3543
unsafe { intrinsics::size_of::<T>() }
3644
}
3745

3846
/// Returns the size of the type that `_val` points to in bytes.
47+
///
48+
/// # Examples
49+
///
50+
/// ```
51+
/// use std::mem;
52+
///
53+
/// assert_eq!(4, mem::size_of_val(&5i32));
54+
/// ```
3955
#[inline]
4056
#[stable]
4157
pub fn size_of_val<T>(_val: &T) -> uint {
@@ -44,16 +60,30 @@ pub fn size_of_val<T>(_val: &T) -> uint {
4460

4561
/// Returns the ABI-required minimum alignment of a type
4662
///
47-
/// This is the alignment used for struct fields. It may be smaller
48-
/// than the preferred alignment.
63+
/// This is the alignment used for struct fields. It may be smaller than the preferred alignment.
64+
///
65+
/// # Examples
66+
///
67+
/// ```
68+
/// use std::mem;
69+
///
70+
/// assert_eq!(4, mem::min_align_of::<i32>());
71+
/// ```
4972
#[inline]
5073
#[stable]
5174
pub fn min_align_of<T>() -> uint {
5275
unsafe { intrinsics::min_align_of::<T>() }
5376
}
5477

55-
/// Returns the ABI-required minimum alignment of the type of the value that
56-
/// `_val` points to
78+
/// Returns the ABI-required minimum alignment of the type of the value that `_val` points to
79+
///
80+
/// # Examples
81+
///
82+
/// ```
83+
/// use std::mem;
84+
///
85+
/// assert_eq!(4, mem::min_align_of_val(&5i32));
86+
/// ```
5787
#[inline]
5888
#[stable]
5989
pub fn min_align_of_val<T>(_val: &T) -> uint {
@@ -62,9 +92,16 @@ pub fn min_align_of_val<T>(_val: &T) -> uint {
6292

6393
/// Returns the alignment in memory for a type.
6494
///
65-
/// This function will return the alignment, in bytes, of a type in memory. If
66-
/// the alignment returned is adhered to, then the type is guaranteed to
67-
/// function properly.
95+
/// This function will return the alignment, in bytes, of a type in memory. If the alignment
96+
/// returned is adhered to, then the type is guaranteed to function properly.
97+
///
98+
/// # Examples
99+
///
100+
/// ```
101+
/// use std::mem;
102+
///
103+
/// assert_eq!(4, mem::align_of::<i32>());
104+
/// ```
68105
#[inline]
69106
#[stable]
70107
pub fn align_of<T>() -> uint {
@@ -77,9 +114,16 @@ pub fn align_of<T>() -> uint {
77114

78115
/// Returns the alignment of the type of the value that `_val` points to.
79116
///
80-
/// This is similar to `align_of`, but function will properly handle types such
81-
/// as trait objects (in the future), returning the alignment for an arbitrary
82-
/// value at runtime.
117+
/// This is similar to `align_of`, but function will properly handle types such as trait objects
118+
/// (in the future), returning the alignment for an arbitrary value at runtime.
119+
///
120+
/// # Examples
121+
///
122+
/// ```
123+
/// use std::mem;
124+
///
125+
/// assert_eq!(4, mem::align_of_val(&5i32));
126+
/// ```
83127
#[inline]
84128
#[stable]
85129
pub fn align_of_val<T>(_val: &T) -> uint {
@@ -88,15 +132,22 @@ pub fn align_of_val<T>(_val: &T) -> uint {
88132

89133
/// Create a value initialized to zero.
90134
///
91-
/// This function is similar to allocating space for a local variable and
92-
/// zeroing it out (an unsafe operation).
135+
/// This function is similar to allocating space for a local variable and zeroing it out (an unsafe
136+
/// operation).
93137
///
94-
/// Care must be taken when using this function, if the type `T` has a
95-
/// destructor and the value falls out of scope (due to unwinding or returning)
96-
/// before being initialized, then the destructor will run on zeroed
97-
/// data, likely leading to crashes.
138+
/// Care must be taken when using this function, if the type `T` has a destructor and the value
139+
/// falls out of scope (due to unwinding or returning) before being initialized, then the
140+
/// destructor will run on zeroed data, likely leading to crashes.
98141
///
99142
/// This is useful for FFI functions sometimes, but should generally be avoided.
143+
///
144+
/// # Examples
145+
///
146+
/// ```
147+
/// use std::mem;
148+
///
149+
/// let x: int = unsafe { mem::zeroed() };
150+
/// ```
100151
#[inline]
101152
#[stable]
102153
pub unsafe fn zeroed<T>() -> T {
@@ -105,20 +156,41 @@ pub unsafe fn zeroed<T>() -> T {
105156

106157
/// Create an uninitialized value.
107158
///
108-
/// Care must be taken when using this function, if the type `T` has a
109-
/// destructor and the value falls out of scope (due to unwinding or returning)
110-
/// before being initialized, then the destructor will run on uninitialized
111-
/// data, likely leading to crashes.
159+
/// Care must be taken when using this function, if the type `T` has a destructor and the value
160+
/// falls out of scope (due to unwinding or returning) before being initialized, then the
161+
/// destructor will run on uninitialized data, likely leading to crashes.
112162
///
113163
/// This is useful for FFI functions sometimes, but should generally be avoided.
164+
///
165+
/// # Examples
166+
///
167+
/// ```
168+
/// use std::mem;
169+
///
170+
/// let x: int = unsafe { mem::uninitialized() };
171+
/// ```
114172
#[inline]
115173
#[stable]
116174
pub unsafe fn uninitialized<T>() -> T {
117175
intrinsics::uninit()
118176
}
119177

120-
/// Swap the values at two mutable locations of the same type, without
121-
/// deinitialising or copying either one.
178+
/// Swap the values at two mutable locations of the same type, without deinitialising or copying
179+
/// either one.
180+
///
181+
/// # Examples
182+
///
183+
/// ```
184+
/// use std::mem;
185+
///
186+
/// let x = &mut 5i;
187+
/// let y = &mut 42i;
188+
///
189+
/// mem::swap(x, y);
190+
///
191+
/// assert_eq!(42i, *x);
192+
/// assert_eq!(5i, *y);
193+
/// ```
122194
#[inline]
123195
#[stable]
124196
pub fn swap<T>(x: &mut T, y: &mut T) {
@@ -137,13 +209,26 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
137209
}
138210
}
139211

140-
/// Replace the value at a mutable location with a new one, returning the old
141-
/// value, without deinitialising or copying either one.
212+
/// Replace the value at a mutable location with a new one, returning the old value, without
213+
/// deinitialising or copying either one.
214+
///
215+
/// This is primarily used for transferring and swapping ownership of a value in a mutable
216+
/// location.
217+
///
218+
/// # Examples
219+
///
220+
/// A simple example:
221+
///
222+
/// ```
223+
/// use std::mem;
224+
///
225+
/// let mut v: Vec<i32> = Vec::new();
226+
///
227+
/// mem::replace(&mut v, Vec::new());
228+
/// ```
142229
///
143-
/// This is primarily used for transferring and swapping ownership of a value
144-
/// in a mutable location. For example, this function allows consumption of
145-
/// one field of a struct by replacing it with another value. The normal approach
146-
/// doesn't always work:
230+
/// This function allows consumption of one field of a struct by replacing it with another value.
231+
/// The normal approach doesn't always work:
147232
///
148233
/// ```rust,ignore
149234
/// struct Buffer<T> { buf: Vec<T> }
@@ -158,16 +243,16 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
158243
/// }
159244
/// ```
160245
///
161-
/// Note that `T` does not necessarily implement `Clone`, so it can't even
162-
/// clone and reset `self.buf`. But `replace` can be used to disassociate
163-
/// the original value of `self.buf` from `self`, allowing it to be returned:
246+
/// Note that `T` does not necessarily implement `Clone`, so it can't even clone and reset
247+
/// `self.buf`. But `replace` can be used to disassociate the original value of `self.buf` from
248+
/// `self`, allowing it to be returned:
164249
///
165250
/// ```rust
251+
/// use std::mem;
166252
/// # struct Buffer<T> { buf: Vec<T> }
167253
/// impl<T> Buffer<T> {
168254
/// fn get_and_reset(&mut self) -> Vec<T> {
169-
/// use std::mem::replace;
170-
/// replace(&mut self.buf, Vec::new())
255+
/// mem::replace(&mut self.buf, Vec::new())
171256
/// }
172257
/// }
173258
/// ```
@@ -180,10 +265,10 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
180265

181266
/// Disposes of a value.
182267
///
183-
/// This function can be used to destroy any value by allowing `drop` to take
184-
/// ownership of its argument.
268+
/// This function can be used to destroy any value by allowing `drop` to take ownership of its
269+
/// argument.
185270
///
186-
/// # Example
271+
/// # Examples
187272
///
188273
/// ```
189274
/// use std::cell::RefCell;
@@ -192,6 +277,7 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
192277
///
193278
/// let mut mutable_borrow = x.borrow_mut();
194279
/// *mutable_borrow = 1;
280+
///
195281
/// drop(mutable_borrow); // relinquish the mutable borrow on this slot
196282
///
197283
/// let borrow = x.borrow();
@@ -201,18 +287,25 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
201287
#[stable]
202288
pub fn drop<T>(_x: T) { }
203289

204-
/// Interprets `src` as `&U`, and then reads `src` without moving the contained
205-
/// value.
290+
/// Interprets `src` as `&U`, and then reads `src` without moving the contained value.
291+
///
292+
/// This function will unsafely assume the pointer `src` is valid for `sizeof(U)` bytes by
293+
/// transmuting `&T` to `&U` and then reading the `&U`. It will also unsafely create a copy of the
294+
/// contained value instead of moving out of `src`.
295+
///
296+
/// It is not a compile-time error if `T` and `U` have different sizes, but it is highly encouraged
297+
/// to only invoke this function where `T` and `U` have the same size. This function triggers
298+
/// undefined behavior if `U` is larger than `T`.
206299
///
207-
/// This function will unsafely assume the pointer `src` is valid for
208-
/// `sizeof(U)` bytes by transmuting `&T` to `&U` and then reading the `&U`. It
209-
/// will also unsafely create a copy of the contained value instead of moving
210-
/// out of `src`.
300+
/// # Examples
211301
///
212-
/// It is not a compile-time error if `T` and `U` have different sizes, but it
213-
/// is highly encouraged to only invoke this function where `T` and `U` have the
214-
/// same size. This function triggers undefined behavior if `U` is larger than
215-
/// `T`.
302+
/// ```
303+
/// use std::mem;
304+
///
305+
/// let one = unsafe { mem::transmute_copy(&1i) };
306+
///
307+
/// assert_eq!(1u, one);
308+
/// ```
216309
#[inline]
217310
#[stable]
218311
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {

0 commit comments

Comments
 (0)