@@ -97,13 +97,20 @@ use cmp::{PartialEq, Eq, Ord, PartialOrd, Equiv};
97
97
use cmp:: Ordering ;
98
98
use cmp:: Ordering :: { Less , Equal , Greater } ;
99
99
100
- pub use intrinsics:: copy_memory;
100
+ // FIXME #19649: instrinsic docs don't render, so these have no docs :(
101
+
102
+ #[ unstable]
101
103
pub use intrinsics:: copy_nonoverlapping_memory;
104
+
105
+ #[ unstable]
106
+ pub use intrinsics:: copy_memory;
107
+
108
+ #[ experimental = "uncertain about naming and semantics" ]
102
109
pub use intrinsics:: set_memory;
103
110
104
- /// Create a null pointer.
111
+ /// Creates a null raw pointer.
105
112
///
106
- /// # Example
113
+ /// # Examples
107
114
///
108
115
/// ```
109
116
/// use std::ptr;
@@ -115,9 +122,9 @@ pub use intrinsics::set_memory;
115
122
#[ unstable = "may need a different name after pending changes to pointer types" ]
116
123
pub fn null < T > ( ) -> * const T { 0 as * const T }
117
124
118
- /// Create an unsafe mutable null pointer.
125
+ /// Creates a null mutable raw pointer.
119
126
///
120
- /// # Example
127
+ /// # Examples
121
128
///
122
129
/// ```
123
130
/// use std::ptr;
@@ -129,16 +136,26 @@ pub fn null<T>() -> *const T { 0 as *const T }
129
136
#[ unstable = "may need a different name after pending changes to pointer types" ]
130
137
pub fn null_mut < T > ( ) -> * mut T { 0 as * mut T }
131
138
132
- /// Zeroes out `count * size_of::<T>` bytes of memory at `dst`
139
+ /// Zeroes out `count * size_of::<T>` bytes of memory at `dst`. `count` may be `0`.
140
+ ///
141
+ /// # Safety
142
+ ///
143
+ /// Beyond accepting a raw pointer, this is unsafe because it will not drop the contents of `dst`,
144
+ /// and may be used to create invalid instances of `T`.
133
145
#[ inline]
134
146
#[ experimental = "uncertain about naming and semantics" ]
135
147
#[ allow( experimental) ]
136
148
pub unsafe fn zero_memory < T > ( dst : * mut T , count : uint ) {
137
149
set_memory ( dst, 0 , count) ;
138
150
}
139
151
140
- /// Swap the values at two mutable locations of the same type, without
141
- /// deinitialising either. They may overlap.
152
+ /// Swaps the values at two mutable locations of the same type, without
153
+ /// deinitialising either. They may overlap, unlike `mem::swap` which is otherwise
154
+ /// equivalent.
155
+ ///
156
+ /// # Safety
157
+ ///
158
+ /// This is only unsafe because it accepts a raw pointer.
142
159
#[ inline]
143
160
#[ unstable]
144
161
pub unsafe fn swap < T > ( x : * mut T , y : * mut T ) {
@@ -156,16 +173,31 @@ pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
156
173
mem:: forget ( tmp) ;
157
174
}
158
175
159
- /// Replace the value at a mutable location with a new one, returning the old
160
- /// value, without deinitialising either.
176
+ /// Replaces the value at `dest` with `src`, returning the old
177
+ /// value, without dropping either.
178
+ ///
179
+ /// # Safety
180
+ ///
181
+ /// This is only unsafe because it accepts a raw pointer.
182
+ /// Otherwise, this operation is identical to `mem::replace`.
161
183
#[ inline]
162
184
#[ unstable]
163
185
pub unsafe fn replace < T > ( dest : * mut T , mut src : T ) -> T {
164
186
mem:: swap ( mem:: transmute ( dest) , & mut src) ; // cannot overlap
165
187
src
166
188
}
167
189
168
- /// Reads the value from `*src` and returns it.
190
+ /// Reads the value from `src` without dropping it. This leaves the
191
+ /// memory in `src` unchanged.
192
+ ///
193
+ /// # Safety
194
+ ///
195
+ /// Beyond accepting a raw pointer, this is unsafe because it semantically
196
+ /// moves the value out of `src` without preventing further usage of `src`.
197
+ /// If `T` is not `Copy`, then care must be taken to ensure that the value at
198
+ /// `src` is not used before the data is overwritten again (e.g. with `write`,
199
+ /// `zero_memory`, or `copy_memory`). Note that `*src = foo` counts as a use
200
+ /// because it will attempt to drop the value previously at `*src`.
169
201
#[ inline( always) ]
170
202
#[ unstable]
171
203
pub unsafe fn read < T > ( src : * const T ) -> T {
@@ -174,8 +206,11 @@ pub unsafe fn read<T>(src: *const T) -> T {
174
206
tmp
175
207
}
176
208
177
- /// Reads the value from `*src` and nulls it out.
178
- /// This currently prevents destructors from executing.
209
+ /// Reads the value from `src` and nulls it out without dropping it.
210
+ ///
211
+ /// # Safety
212
+ ///
213
+ /// This is unsafe for the same reasons that `read` is unsafe.
179
214
#[ inline( always) ]
180
215
#[ experimental]
181
216
#[ allow( experimental) ]
@@ -189,12 +224,17 @@ pub unsafe fn read_and_zero<T>(dest: *mut T) -> T {
189
224
tmp
190
225
}
191
226
192
- /// Unsafely overwrite a memory location with the given value without destroying
227
+ /// Overwrites a memory location with the given value without reading or dropping
193
228
/// the old value.
194
229
///
195
- /// This operation is unsafe because it does not destroy the previous value
196
- /// contained at the location `dst`. This could leak allocations or resources,
197
- /// so care must be taken to previously deallocate the value at `dst`.
230
+ /// # Safety
231
+ ///
232
+ /// Beyond accepting a raw pointer, this operation is unsafe because it does
233
+ /// not drop the contents of `dst`. This could leak allocations or resources,
234
+ /// so care must be taken not to overwrite an object that should be dropped.
235
+ ///
236
+ /// This is appropriate for initializing uninitialized memory, or overwritting memory
237
+ /// that has previously been `read` from.
198
238
#[ inline]
199
239
#[ unstable]
200
240
pub unsafe fn write < T > ( dst : * mut T , src : T ) {
@@ -203,39 +243,47 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
203
243
204
244
/// Methods on raw pointers
205
245
pub trait RawPtr < T > {
206
- /// Returns the null pointer.
246
+ /// Returns a null raw pointer.
207
247
fn null ( ) -> Self ;
208
248
209
- /// Returns true if the pointer is equal to the null pointer .
249
+ /// Returns true if the pointer is null.
210
250
fn is_null ( & self ) -> bool ;
211
251
212
- /// Returns true if the pointer is not equal to the null pointer .
252
+ /// Returns true if the pointer is not null.
213
253
fn is_not_null ( & self ) -> bool { !self . is_null ( ) }
214
254
215
- /// Returns the value of this pointer (ie, the address it points to)
255
+ /// Returns the address of the pointer.
216
256
fn to_uint ( & self ) -> uint ;
217
257
218
258
/// Returns `None` if the pointer is null, or else returns a reference to the
219
259
/// value wrapped in `Some`.
220
260
///
221
- /// # Safety Notes
261
+ /// # Safety
222
262
///
223
263
/// While this method and its mutable counterpart are useful for null-safety,
224
264
/// it is important to note that this is still an unsafe operation because
225
265
/// the returned value could be pointing to invalid memory.
226
266
unsafe fn as_ref < ' a > ( & self ) -> Option < & ' a T > ;
227
267
228
- /// Calculates the offset from a pointer. The offset *must* be in-bounds of
229
- /// the object, or one-byte-past-the-end. `count` is in units of T; e.g. a
268
+ /// Calculates the offset from a pointer. `count` is in units of T; e.g. a
230
269
/// `count` of 3 represents a pointer offset of `3 * sizeof::<T>()` bytes.
270
+ ///
271
+ /// # Safety
272
+ ///
273
+ /// The offset must be in-bounds of the object, or one-byte-past-the-end. Otherwise
274
+ /// `offset` invokes Undefined Behaviour, regardless of whether the pointer is used.
231
275
unsafe fn offset ( self , count : int ) -> Self ;
232
276
}
233
277
234
278
/// Methods on mutable raw pointers
235
279
pub trait RawMutPtr < T > {
236
280
/// Returns `None` if the pointer is null, or else returns a mutable reference
237
- /// to the value wrapped in `Some`. As with `as_ref`, this is unsafe because
238
- /// it cannot verify the validity of the returned pointer.
281
+ /// to the value wrapped in `Some`.
282
+ ///
283
+ /// # Safety
284
+ ///
285
+ /// As with `as_ref`, this is unsafe because it cannot verify the validity
286
+ /// of the returned pointer.
239
287
unsafe fn as_mut < ' a > ( & self ) -> Option < & ' a mut T > ;
240
288
}
241
289
0 commit comments