Skip to content

Commit cbc3cf7

Browse files
committed
rollup merge of #19942: steveklabnik/doc_std_mem
2 parents 1b9b647 + 033a792 commit cbc3cf7

File tree

1 file changed

+139
-46
lines changed

1 file changed

+139
-46
lines changed

src/libcore/mem.rs

Lines changed: 139 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,29 @@ pub use intrinsics::transmute;
3333
pub use intrinsics::forget;
3434

3535
/// Returns the size of a type in bytes.
36+
///
37+
/// # Examples
38+
///
39+
/// ```
40+
/// use std::mem;
41+
///
42+
/// assert_eq!(4, mem::size_of::<i32>());
43+
/// ```
3644
#[inline]
3745
#[stable]
3846
pub fn size_of<T>() -> uint {
3947
unsafe { intrinsics::size_of::<T>() }
4048
}
4149

4250
/// Returns the size of the type that `_val` points to in bytes.
51+
///
52+
/// # Examples
53+
///
54+
/// ```
55+
/// use std::mem;
56+
///
57+
/// assert_eq!(4, mem::size_of_val(&5i32));
58+
/// ```
4359
#[inline]
4460
#[stable]
4561
pub fn size_of_val<T>(_val: &T) -> uint {
@@ -48,16 +64,30 @@ pub fn size_of_val<T>(_val: &T) -> uint {
4864

4965
/// Returns the ABI-required minimum alignment of a type
5066
///
51-
/// This is the alignment used for struct fields. It may be smaller
52-
/// than the preferred alignment.
67+
/// This is the alignment used for struct fields. It may be smaller than the preferred alignment.
68+
///
69+
/// # Examples
70+
///
71+
/// ```
72+
/// use std::mem;
73+
///
74+
/// assert_eq!(4, mem::min_align_of::<i32>());
75+
/// ```
5376
#[inline]
5477
#[stable]
5578
pub fn min_align_of<T>() -> uint {
5679
unsafe { intrinsics::min_align_of::<T>() }
5780
}
5881

59-
/// Returns the ABI-required minimum alignment of the type of the value that
60-
/// `_val` points to
82+
/// Returns the ABI-required minimum alignment of the type of the value that `_val` points to
83+
///
84+
/// # Examples
85+
///
86+
/// ```
87+
/// use std::mem;
88+
///
89+
/// assert_eq!(4, mem::min_align_of_val(&5i32));
90+
/// ```
6191
#[inline]
6292
#[stable]
6393
pub fn min_align_of_val<T>(_val: &T) -> uint {
@@ -66,9 +96,16 @@ pub fn min_align_of_val<T>(_val: &T) -> uint {
6696

6797
/// Returns the alignment in memory for a type.
6898
///
69-
/// This function will return the alignment, in bytes, of a type in memory. If
70-
/// the alignment returned is adhered to, then the type is guaranteed to
71-
/// function properly.
99+
/// This function will return the alignment, in bytes, of a type in memory. If the alignment
100+
/// returned is adhered to, then the type is guaranteed to function properly.
101+
///
102+
/// # Examples
103+
///
104+
/// ```
105+
/// use std::mem;
106+
///
107+
/// assert_eq!(4, mem::align_of::<i32>());
108+
/// ```
72109
#[inline]
73110
#[stable]
74111
pub fn align_of<T>() -> uint {
@@ -81,9 +118,16 @@ pub fn align_of<T>() -> uint {
81118

82119
/// Returns the alignment of the type of the value that `_val` points to.
83120
///
84-
/// This is similar to `align_of`, but function will properly handle types such
85-
/// as trait objects (in the future), returning the alignment for an arbitrary
86-
/// value at runtime.
121+
/// This is similar to `align_of`, but function will properly handle types such as trait objects
122+
/// (in the future), returning the alignment for an arbitrary value at runtime.
123+
///
124+
/// # Examples
125+
///
126+
/// ```
127+
/// use std::mem;
128+
///
129+
/// assert_eq!(4, mem::align_of_val(&5i32));
130+
/// ```
87131
#[inline]
88132
#[stable]
89133
pub fn align_of_val<T>(_val: &T) -> uint {
@@ -92,15 +136,22 @@ pub fn align_of_val<T>(_val: &T) -> uint {
92136

93137
/// Create a value initialized to zero.
94138
///
95-
/// This function is similar to allocating space for a local variable and
96-
/// zeroing it out (an unsafe operation).
139+
/// This function is similar to allocating space for a local variable and zeroing it out (an unsafe
140+
/// operation).
97141
///
98-
/// Care must be taken when using this function, if the type `T` has a
99-
/// destructor and the value falls out of scope (due to unwinding or returning)
100-
/// before being initialized, then the destructor will run on zeroed
101-
/// data, likely leading to crashes.
142+
/// Care must be taken when using this function, if the type `T` has a destructor and the value
143+
/// falls out of scope (due to unwinding or returning) before being initialized, then the
144+
/// destructor will run on zeroed data, likely leading to crashes.
102145
///
103146
/// This is useful for FFI functions sometimes, but should generally be avoided.
147+
///
148+
/// # Examples
149+
///
150+
/// ```
151+
/// use std::mem;
152+
///
153+
/// let x: int = unsafe { mem::zeroed() };
154+
/// ```
104155
#[inline]
105156
#[stable]
106157
pub unsafe fn zeroed<T>() -> T {
@@ -109,20 +160,41 @@ pub unsafe fn zeroed<T>() -> T {
109160

110161
/// Create an uninitialized value.
111162
///
112-
/// Care must be taken when using this function, if the type `T` has a
113-
/// destructor and the value falls out of scope (due to unwinding or returning)
114-
/// before being initialized, then the destructor will run on uninitialized
115-
/// data, likely leading to crashes.
163+
/// Care must be taken when using this function, if the type `T` has a destructor and the value
164+
/// falls out of scope (due to unwinding or returning) before being initialized, then the
165+
/// destructor will run on uninitialized data, likely leading to crashes.
116166
///
117167
/// This is useful for FFI functions sometimes, but should generally be avoided.
168+
///
169+
/// # Examples
170+
///
171+
/// ```
172+
/// use std::mem;
173+
///
174+
/// let x: int = unsafe { mem::uninitialized() };
175+
/// ```
118176
#[inline]
119177
#[stable]
120178
pub unsafe fn uninitialized<T>() -> T {
121179
intrinsics::uninit()
122180
}
123181

124-
/// Swap the values at two mutable locations of the same type, without
125-
/// deinitialising or copying either one.
182+
/// Swap the values at two mutable locations of the same type, without deinitialising or copying
183+
/// either one.
184+
///
185+
/// # Examples
186+
///
187+
/// ```
188+
/// use std::mem;
189+
///
190+
/// let x = &mut 5i;
191+
/// let y = &mut 42i;
192+
///
193+
/// mem::swap(x, y);
194+
///
195+
/// assert_eq!(42i, *x);
196+
/// assert_eq!(5i, *y);
197+
/// ```
126198
#[inline]
127199
#[stable]
128200
pub fn swap<T>(x: &mut T, y: &mut T) {
@@ -141,13 +213,26 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
141213
}
142214
}
143215

144-
/// Replace the value at a mutable location with a new one, returning the old
145-
/// value, without deinitialising or copying either one.
216+
/// Replace the value at a mutable location with a new one, returning the old value, without
217+
/// deinitialising or copying either one.
218+
///
219+
/// This is primarily used for transferring and swapping ownership of a value in a mutable
220+
/// location.
221+
///
222+
/// # Examples
223+
///
224+
/// A simple example:
225+
///
226+
/// ```
227+
/// use std::mem;
228+
///
229+
/// let mut v: Vec<i32> = Vec::new();
230+
///
231+
/// mem::replace(&mut v, Vec::new());
232+
/// ```
146233
///
147-
/// This is primarily used for transferring and swapping ownership of a value
148-
/// in a mutable location. For example, this function allows consumption of
149-
/// one field of a struct by replacing it with another value. The normal approach
150-
/// doesn't always work:
234+
/// This function allows consumption of one field of a struct by replacing it with another value.
235+
/// The normal approach doesn't always work:
151236
///
152237
/// ```rust,ignore
153238
/// struct Buffer<T> { buf: Vec<T> }
@@ -162,16 +247,16 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
162247
/// }
163248
/// ```
164249
///
165-
/// Note that `T` does not necessarily implement `Clone`, so it can't even
166-
/// clone and reset `self.buf`. But `replace` can be used to disassociate
167-
/// the original value of `self.buf` from `self`, allowing it to be returned:
250+
/// Note that `T` does not necessarily implement `Clone`, so it can't even clone and reset
251+
/// `self.buf`. But `replace` can be used to disassociate the original value of `self.buf` from
252+
/// `self`, allowing it to be returned:
168253
///
169254
/// ```rust
255+
/// use std::mem;
170256
/// # struct Buffer<T> { buf: Vec<T> }
171257
/// impl<T> Buffer<T> {
172258
/// fn get_and_reset(&mut self) -> Vec<T> {
173-
/// use std::mem::replace;
174-
/// replace(&mut self.buf, Vec::new())
259+
/// mem::replace(&mut self.buf, Vec::new())
175260
/// }
176261
/// }
177262
/// ```
@@ -184,10 +269,10 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
184269

185270
/// Disposes of a value.
186271
///
187-
/// This function can be used to destroy any value by allowing `drop` to take
188-
/// ownership of its argument.
272+
/// This function can be used to destroy any value by allowing `drop` to take ownership of its
273+
/// argument.
189274
///
190-
/// # Example
275+
/// # Examples
191276
///
192277
/// ```
193278
/// use std::cell::RefCell;
@@ -196,6 +281,7 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
196281
///
197282
/// let mut mutable_borrow = x.borrow_mut();
198283
/// *mutable_borrow = 1;
284+
///
199285
/// drop(mutable_borrow); // relinquish the mutable borrow on this slot
200286
///
201287
/// let borrow = x.borrow();
@@ -205,18 +291,25 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
205291
#[stable]
206292
pub fn drop<T>(_x: T) { }
207293

208-
/// Interprets `src` as `&U`, and then reads `src` without moving the contained
209-
/// value.
294+
/// Interprets `src` as `&U`, and then reads `src` without moving the contained value.
295+
///
296+
/// This function will unsafely assume the pointer `src` is valid for `sizeof(U)` bytes by
297+
/// transmuting `&T` to `&U` and then reading the `&U`. It will also unsafely create a copy of the
298+
/// contained value instead of moving out of `src`.
299+
///
300+
/// It is not a compile-time error if `T` and `U` have different sizes, but it is highly encouraged
301+
/// to only invoke this function where `T` and `U` have the same size. This function triggers
302+
/// undefined behavior if `U` is larger than `T`.
210303
///
211-
/// This function will unsafely assume the pointer `src` is valid for
212-
/// `sizeof(U)` bytes by transmuting `&T` to `&U` and then reading the `&U`. It
213-
/// will also unsafely create a copy of the contained value instead of moving
214-
/// out of `src`.
304+
/// # Examples
215305
///
216-
/// It is not a compile-time error if `T` and `U` have different sizes, but it
217-
/// is highly encouraged to only invoke this function where `T` and `U` have the
218-
/// same size. This function triggers undefined behavior if `U` is larger than
219-
/// `T`.
306+
/// ```
307+
/// use std::mem;
308+
///
309+
/// let one = unsafe { mem::transmute_copy(&1i) };
310+
///
311+
/// assert_eq!(1u, one);
312+
/// ```
220313
#[inline]
221314
#[stable]
222315
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {

0 commit comments

Comments
 (0)