@@ -29,13 +29,29 @@ pub use intrinsics::transmute;
29
29
pub use intrinsics:: forget;
30
30
31
31
/// 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
+ /// ```
32
40
#[ inline]
33
41
#[ stable]
34
42
pub fn size_of < T > ( ) -> uint {
35
43
unsafe { intrinsics:: size_of :: < T > ( ) }
36
44
}
37
45
38
46
/// 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
+ /// ```
39
55
#[ inline]
40
56
#[ stable]
41
57
pub fn size_of_val < T > ( _val : & T ) -> uint {
@@ -44,16 +60,30 @@ pub fn size_of_val<T>(_val: &T) -> uint {
44
60
45
61
/// Returns the ABI-required minimum alignment of a type
46
62
///
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
+ /// ```
49
72
#[ inline]
50
73
#[ stable]
51
74
pub fn min_align_of < T > ( ) -> uint {
52
75
unsafe { intrinsics:: min_align_of :: < T > ( ) }
53
76
}
54
77
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
+ /// ```
57
87
#[ inline]
58
88
#[ stable]
59
89
pub fn min_align_of_val < T > ( _val : & T ) -> uint {
@@ -62,9 +92,16 @@ pub fn min_align_of_val<T>(_val: &T) -> uint {
62
92
63
93
/// Returns the alignment in memory for a type.
64
94
///
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
+ /// ```
68
105
#[ inline]
69
106
#[ stable]
70
107
pub fn align_of < T > ( ) -> uint {
@@ -77,9 +114,16 @@ pub fn align_of<T>() -> uint {
77
114
78
115
/// Returns the alignment of the type of the value that `_val` points to.
79
116
///
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
+ /// ```
83
127
#[ inline]
84
128
#[ stable]
85
129
pub fn align_of_val < T > ( _val : & T ) -> uint {
@@ -88,15 +132,22 @@ pub fn align_of_val<T>(_val: &T) -> uint {
88
132
89
133
/// Create a value initialized to zero.
90
134
///
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).
93
137
///
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.
98
141
///
99
142
/// 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
+ /// ```
100
151
#[ inline]
101
152
#[ stable]
102
153
pub unsafe fn zeroed < T > ( ) -> T {
@@ -105,20 +156,41 @@ pub unsafe fn zeroed<T>() -> T {
105
156
106
157
/// Create an uninitialized value.
107
158
///
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.
112
162
///
113
163
/// 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
+ /// ```
114
172
#[ inline]
115
173
#[ stable]
116
174
pub unsafe fn uninitialized < T > ( ) -> T {
117
175
intrinsics:: uninit ( )
118
176
}
119
177
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
+ /// ```
122
194
#[ inline]
123
195
#[ stable]
124
196
pub fn swap < T > ( x : & mut T , y : & mut T ) {
@@ -137,13 +209,26 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
137
209
}
138
210
}
139
211
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
+ /// ```
142
229
///
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:
147
232
///
148
233
/// ```rust,ignore
149
234
/// struct Buffer<T> { buf: Vec<T> }
@@ -158,16 +243,16 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
158
243
/// }
159
244
/// ```
160
245
///
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:
164
249
///
165
250
/// ```rust
251
+ /// use std::mem;
166
252
/// # struct Buffer<T> { buf: Vec<T> }
167
253
/// impl<T> Buffer<T> {
168
254
/// 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())
171
256
/// }
172
257
/// }
173
258
/// ```
@@ -180,10 +265,10 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
180
265
181
266
/// Disposes of a value.
182
267
///
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.
185
270
///
186
- /// # Example
271
+ /// # Examples
187
272
///
188
273
/// ```
189
274
/// use std::cell::RefCell;
@@ -192,6 +277,7 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
192
277
///
193
278
/// let mut mutable_borrow = x.borrow_mut();
194
279
/// *mutable_borrow = 1;
280
+ ///
195
281
/// drop(mutable_borrow); // relinquish the mutable borrow on this slot
196
282
///
197
283
/// let borrow = x.borrow();
@@ -201,18 +287,25 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
201
287
#[ stable]
202
288
pub fn drop < T > ( _x : T ) { }
203
289
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`.
206
299
///
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
211
301
///
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
+ /// ```
216
309
#[ inline]
217
310
#[ stable]
218
311
pub unsafe fn transmute_copy < T , U > ( src : & T ) -> U {
0 commit comments