Skip to content

Commit ebf0e96

Browse files
committed
rollup merge of #19651: Gankro/ptr-docs
r? @steveklabnik @thestinger
2 parents c141f22 + ce10c01 commit ebf0e96

File tree

2 files changed

+94
-40
lines changed

2 files changed

+94
-40
lines changed

src/libcore/intrinsics.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ extern "rust-intrinsic" {
222222
/// Both types must have the same size and alignment, and this guarantee
223223
/// is enforced at compile-time.
224224
///
225-
/// # Example
225+
/// # Examples
226226
///
227227
/// ```rust
228228
/// use std::mem;
@@ -253,14 +253,20 @@ extern "rust-intrinsic" {
253253
/// integer, since the conversion would throw away aliasing information.
254254
pub fn offset<T>(dst: *const T, offset: int) -> *const T;
255255

256-
/// Copies data from one location to another.
257-
///
258-
/// Copies `count` elements (not bytes) from `src` to `dst`. The source
256+
/// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
259257
/// and destination may *not* overlap.
260258
///
261259
/// `copy_nonoverlapping_memory` is semantically equivalent to C's `memcpy`.
262260
///
263-
/// # Example
261+
/// # Safety
262+
///
263+
/// Beyond requiring that both regions of memory be allocated, it is Undefined Behaviour
264+
/// for source and destination to overlap. Care must also be taken with the ownership of
265+
/// `src` and `dst`. This method semantically moves the values of `src` into `dst`.
266+
/// However it does not drop the contents of `dst`, or prevent the contents of `src`
267+
/// from being dropped or used.
268+
///
269+
/// # Examples
264270
///
265271
/// A safe swap function:
266272
///
@@ -284,22 +290,22 @@ extern "rust-intrinsic" {
284290
/// }
285291
/// }
286292
/// ```
287-
///
288-
/// # Safety Note
289-
///
290-
/// If the source and destination overlap then the behavior of this
291-
/// function is undefined.
292293
#[unstable]
293294
pub fn copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: uint);
294295

295-
/// Copies data from one location to another.
296-
///
297-
/// Copies `count` elements (not bytes) from `src` to `dst`. The source
296+
/// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
298297
/// and destination may overlap.
299298
///
300299
/// `copy_memory` is semantically equivalent to C's `memmove`.
301300
///
302-
/// # Example
301+
/// # Safety
302+
///
303+
/// Care must be taken with the ownership of `src` and `dst`.
304+
/// This method semantically moves the values of `src` into `dst`.
305+
/// However it does not drop the contents of `dst`, or prevent the contents of `src`
306+
/// from being dropped or used.
307+
///
308+
/// # Examples
303309
///
304310
/// Efficiently create a Rust vector from an unsafe buffer:
305311
///

src/libcore/ptr.rs

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,20 @@ use cmp::{PartialEq, Eq, Ord, PartialOrd, Equiv};
9797
use cmp::Ordering;
9898
use cmp::Ordering::{Less, Equal, Greater};
9999

100-
pub use intrinsics::copy_memory;
100+
// FIXME #19649: instrinsic docs don't render, so these have no docs :(
101+
102+
#[unstable]
101103
pub use intrinsics::copy_nonoverlapping_memory;
104+
105+
#[unstable]
106+
pub use intrinsics::copy_memory;
107+
108+
#[experimental = "uncertain about naming and semantics"]
102109
pub use intrinsics::set_memory;
103110

104-
/// Create a null pointer.
111+
/// Creates a null raw pointer.
105112
///
106-
/// # Example
113+
/// # Examples
107114
///
108115
/// ```
109116
/// use std::ptr;
@@ -115,9 +122,9 @@ pub use intrinsics::set_memory;
115122
#[unstable = "may need a different name after pending changes to pointer types"]
116123
pub fn null<T>() -> *const T { 0 as *const T }
117124

118-
/// Create an unsafe mutable null pointer.
125+
/// Creates a null mutable raw pointer.
119126
///
120-
/// # Example
127+
/// # Examples
121128
///
122129
/// ```
123130
/// use std::ptr;
@@ -129,16 +136,26 @@ pub fn null<T>() -> *const T { 0 as *const T }
129136
#[unstable = "may need a different name after pending changes to pointer types"]
130137
pub fn null_mut<T>() -> *mut T { 0 as *mut T }
131138

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`.
133145
#[inline]
134146
#[experimental = "uncertain about naming and semantics"]
135147
#[allow(experimental)]
136148
pub unsafe fn zero_memory<T>(dst: *mut T, count: uint) {
137149
set_memory(dst, 0, count);
138150
}
139151

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.
142159
#[inline]
143160
#[unstable]
144161
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) {
156173
mem::forget(tmp);
157174
}
158175

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`.
161183
#[inline]
162184
#[unstable]
163185
pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T {
164186
mem::swap(mem::transmute(dest), &mut src); // cannot overlap
165187
src
166188
}
167189

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`.
169201
#[inline(always)]
170202
#[unstable]
171203
pub unsafe fn read<T>(src: *const T) -> T {
@@ -174,8 +206,11 @@ pub unsafe fn read<T>(src: *const T) -> T {
174206
tmp
175207
}
176208

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.
179214
#[inline(always)]
180215
#[experimental]
181216
#[allow(experimental)]
@@ -189,12 +224,17 @@ pub unsafe fn read_and_zero<T>(dest: *mut T) -> T {
189224
tmp
190225
}
191226

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
193228
/// the old value.
194229
///
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.
198238
#[inline]
199239
#[unstable]
200240
pub unsafe fn write<T>(dst: *mut T, src: T) {
@@ -203,39 +243,47 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
203243

204244
/// Methods on raw pointers
205245
pub trait RawPtr<T> {
206-
/// Returns the null pointer.
246+
/// Returns a null raw pointer.
207247
fn null() -> Self;
208248

209-
/// Returns true if the pointer is equal to the null pointer.
249+
/// Returns true if the pointer is null.
210250
fn is_null(&self) -> bool;
211251

212-
/// Returns true if the pointer is not equal to the null pointer.
252+
/// Returns true if the pointer is not null.
213253
fn is_not_null(&self) -> bool { !self.is_null() }
214254

215-
/// Returns the value of this pointer (ie, the address it points to)
255+
/// Returns the address of the pointer.
216256
fn to_uint(&self) -> uint;
217257

218258
/// Returns `None` if the pointer is null, or else returns a reference to the
219259
/// value wrapped in `Some`.
220260
///
221-
/// # Safety Notes
261+
/// # Safety
222262
///
223263
/// While this method and its mutable counterpart are useful for null-safety,
224264
/// it is important to note that this is still an unsafe operation because
225265
/// the returned value could be pointing to invalid memory.
226266
unsafe fn as_ref<'a>(&self) -> Option<&'a T>;
227267

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
230269
/// `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.
231275
unsafe fn offset(self, count: int) -> Self;
232276
}
233277

234278
/// Methods on mutable raw pointers
235279
pub trait RawMutPtr<T>{
236280
/// 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.
239287
unsafe fn as_mut<'a>(&self) -> Option<&'a mut T>;
240288
}
241289

0 commit comments

Comments
 (0)