@@ -33,13 +33,29 @@ pub use intrinsics::transmute;
33
33
pub use intrinsics:: forget;
34
34
35
35
/// 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
+ /// ```
36
44
#[ inline]
37
45
#[ stable]
38
46
pub fn size_of < T > ( ) -> uint {
39
47
unsafe { intrinsics:: size_of :: < T > ( ) }
40
48
}
41
49
42
50
/// 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
+ /// ```
43
59
#[ inline]
44
60
#[ stable]
45
61
pub fn size_of_val < T > ( _val : & T ) -> uint {
@@ -48,16 +64,30 @@ pub fn size_of_val<T>(_val: &T) -> uint {
48
64
49
65
/// Returns the ABI-required minimum alignment of a type
50
66
///
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
+ /// ```
53
76
#[ inline]
54
77
#[ stable]
55
78
pub fn min_align_of < T > ( ) -> uint {
56
79
unsafe { intrinsics:: min_align_of :: < T > ( ) }
57
80
}
58
81
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
+ /// ```
61
91
#[ inline]
62
92
#[ stable]
63
93
pub fn min_align_of_val < T > ( _val : & T ) -> uint {
@@ -66,9 +96,16 @@ pub fn min_align_of_val<T>(_val: &T) -> uint {
66
96
67
97
/// Returns the alignment in memory for a type.
68
98
///
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
+ /// ```
72
109
#[ inline]
73
110
#[ stable]
74
111
pub fn align_of < T > ( ) -> uint {
@@ -81,9 +118,16 @@ pub fn align_of<T>() -> uint {
81
118
82
119
/// Returns the alignment of the type of the value that `_val` points to.
83
120
///
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
+ /// ```
87
131
#[ inline]
88
132
#[ stable]
89
133
pub fn align_of_val < T > ( _val : & T ) -> uint {
@@ -92,15 +136,22 @@ pub fn align_of_val<T>(_val: &T) -> uint {
92
136
93
137
/// Create a value initialized to zero.
94
138
///
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).
97
141
///
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.
102
145
///
103
146
/// 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
+ /// ```
104
155
#[ inline]
105
156
#[ stable]
106
157
pub unsafe fn zeroed < T > ( ) -> T {
@@ -109,20 +160,41 @@ pub unsafe fn zeroed<T>() -> T {
109
160
110
161
/// Create an uninitialized value.
111
162
///
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.
116
166
///
117
167
/// 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
+ /// ```
118
176
#[ inline]
119
177
#[ stable]
120
178
pub unsafe fn uninitialized < T > ( ) -> T {
121
179
intrinsics:: uninit ( )
122
180
}
123
181
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
+ /// ```
126
198
#[ inline]
127
199
#[ stable]
128
200
pub fn swap < T > ( x : & mut T , y : & mut T ) {
@@ -141,13 +213,26 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
141
213
}
142
214
}
143
215
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
+ /// ```
146
233
///
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:
151
236
///
152
237
/// ```rust,ignore
153
238
/// struct Buffer<T> { buf: Vec<T> }
@@ -162,16 +247,16 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
162
247
/// }
163
248
/// ```
164
249
///
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:
168
253
///
169
254
/// ```rust
255
+ /// use std::mem;
170
256
/// # struct Buffer<T> { buf: Vec<T> }
171
257
/// impl<T> Buffer<T> {
172
258
/// 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())
175
260
/// }
176
261
/// }
177
262
/// ```
@@ -184,10 +269,10 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
184
269
185
270
/// Disposes of a value.
186
271
///
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.
189
274
///
190
- /// # Example
275
+ /// # Examples
191
276
///
192
277
/// ```
193
278
/// use std::cell::RefCell;
@@ -196,6 +281,7 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
196
281
///
197
282
/// let mut mutable_borrow = x.borrow_mut();
198
283
/// *mutable_borrow = 1;
284
+ ///
199
285
/// drop(mutable_borrow); // relinquish the mutable borrow on this slot
200
286
///
201
287
/// let borrow = x.borrow();
@@ -205,18 +291,25 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
205
291
#[ stable]
206
292
pub fn drop < T > ( _x : T ) { }
207
293
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`.
210
303
///
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
215
305
///
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
+ /// ```
220
313
#[ inline]
221
314
#[ stable]
222
315
pub unsafe fn transmute_copy < T , U > ( src : & T ) -> U {
0 commit comments