|
19 | 19 | //! Many functions in this module take raw pointers as arguments and dereference
|
20 | 20 | //! them. For this to be safe, these pointers must be valid. However, because
|
21 | 21 | //! rust does not yet have a formal memory model, determining whether an
|
22 |
| -//! arbitrary pointer is a valid one can be tricky. One thing is certain: |
23 |
| -//! creating a raw pointer from a reference (e.g. `&x as *const _`) *always* |
24 |
| -//! results in a valid pointer. By exploiting this—and by taking care when |
25 |
| -//! using [pointer arithmetic]—users can be confident in the correctness of |
26 |
| -//! their unsafe code. |
| 22 | +//! arbitrary pointer is valid for a given operation can be tricky. |
27 | 23 | //!
|
28 |
| -//! For more information on dereferencing raw pointers, see the both the [book] |
29 |
| -//! and the section in the reference devoted to [undefined behavior][ub]. |
| 24 | +//! There are two types of operations on memory, reads and writes. It is |
| 25 | +//! possible for a `*mut` to be valid for one operation and not the other. Since |
| 26 | +//! a `*const` can only be read and not written, it has no such ambiguity. For |
| 27 | +//! example, a `*mut` is not valid for writes if a a reference exists which |
| 28 | +//! [refers to the same memory][aliasing]. Therefore, each function in this |
| 29 | +//! module will document which operations must be valid on any `*mut` arguments. |
| 30 | +//! |
| 31 | +//! Additionally, some functions (e.g. [`copy`]) take a single pointer but |
| 32 | +//! operate on many values. In this case, the function will state the size of |
| 33 | +//! the operation for which the pointer must be valid. For example, |
| 34 | +//! `copy::<T>(&src, &mut dst, 3)` requires `dst` to be valid for writes of |
| 35 | +//! `size_of::<T>() * 3` bytes. When the documentation requires that a pointer |
| 36 | +//! be valid for an operation but omits the size of that operation, the size is |
| 37 | +//! implied to be `size_of::<T>()` bytes. |
| 38 | +//! |
| 39 | +//! For more information on the safety implications of dereferencing raw |
| 40 | +//! pointers, see the both the [book] and the section in the reference devoted |
| 41 | +//! to [undefined behavior][ub]. |
30 | 42 | //!
|
31 | 43 | //! ## Alignment
|
32 | 44 | //!
|
33 | 45 | //! Valid pointers are not necessarily properly aligned. However, most functions
|
34 | 46 | //! require their arguments to be properly aligned, and will explicitly state
|
35 |
| -//! this requirement in the `Safety` section. Notable exceptions to this are |
| 47 | +//! this requirement in their documentation. Notable exceptions to this are |
36 | 48 | //! [`read_unaligned`] and [`write_unaligned`].
|
37 | 49 | //!
|
38 |
| -//! [ub]: ../../reference/behavior-considered-undefined.html |
| 50 | +//! [aliasing]: ../../nomicon/aliasing.html |
39 | 51 | //! [book]: ../../book/second-edition/ch19-01-unsafe-rust.html#dereferencing-a-raw-pointer
|
40 |
| -//! [pointer arithmetic]: ../../std/primitive.pointer.html#method.offset |
| 52 | +//! [ub]: ../../reference/behavior-considered-undefined.html |
| 53 | +//! [`copy`]: ./fn.copy.html |
41 | 54 | //! [`read_unaligned`]: ./fn.read_unaligned.html
|
42 | 55 | //! [`write_unaligned`]: ./fn.write_unaligned.html
|
43 | 56 |
|
@@ -83,7 +96,7 @@ pub use intrinsics::write_bytes;
|
83 | 96 | ///
|
84 | 97 | /// Behavior is undefined if any of the following conditions are violated:
|
85 | 98 | ///
|
86 |
| -/// * `to_drop` must be [valid]. |
| 99 | +/// * `to_drop` must be [valid] for reads. |
87 | 100 | ///
|
88 | 101 | /// * `to_drop` must be properly aligned.
|
89 | 102 | ///
|
@@ -178,7 +191,7 @@ pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
|
178 | 191 | ///
|
179 | 192 | /// Behavior is undefined if any of the following conditions are violated:
|
180 | 193 | ///
|
181 |
| -/// * Both `x` and `y` must be [valid]. |
| 194 | +/// * Both `x` and `y` must be [valid] for reads and writes. |
182 | 195 | ///
|
183 | 196 | /// * Both `x` and `y` must be properly aligned.
|
184 | 197 | ///
|
@@ -240,17 +253,14 @@ pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
|
240 | 253 | ///
|
241 | 254 | /// Behavior is undefined if any of the following conditions are violated:
|
242 | 255 | ///
|
243 |
| -/// * Both `x` and `y` must be properly aligned. |
244 |
| -/// |
245 |
| -/// * `x.offset(i)` must be [valid] for all `i` in `0..count`. In other words, |
246 |
| -/// the region of memory which begins at `x` and has a length of `count * |
247 |
| -/// size_of::<T>()` bytes must belong to a single, live allocation. |
| 256 | +/// * Both `x` and `y` must be [valid] for reads and writes of `count * |
| 257 | +/// size_of::<T>()` bytes. |
248 | 258 | ///
|
249 |
| -/// * `y.offset(i)` must be [valid] for all `i` in `0..count`. In other words, |
250 |
| -/// the region of memory which begins at `y` and has a length of `count * |
251 |
| -/// size_of::<T>()` bytes must belong to a single, live allocation. |
| 259 | +/// * Both `x` and `y` must be properly aligned. |
252 | 260 | ///
|
253 |
| -/// * The two regions of memory must *not* overlap. |
| 261 | +/// * The region of memory beginning at `x` with a size of `count * |
| 262 | +/// size_of::<T>()` bytes must *not* overlap with the region of memory |
| 263 | +/// beginning at `y` with the same size. |
254 | 264 | ///
|
255 | 265 | /// [valid]: ../ptr/index.html#safety
|
256 | 266 | ///
|
@@ -359,7 +369,7 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
|
359 | 369 | ///
|
360 | 370 | /// Behavior is undefined if any of the following conditions are violated:
|
361 | 371 | ///
|
362 |
| -/// * `dest` must be [valid]. |
| 372 | +/// * `dest` must be [valid] for writes. |
363 | 373 | ///
|
364 | 374 | /// * `dest` must be properly aligned.
|
365 | 375 | ///
|
@@ -395,7 +405,7 @@ pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T {
|
395 | 405 | ///
|
396 | 406 | /// Behavior is undefined if any of the following conditions are violated:
|
397 | 407 | ///
|
398 |
| -/// * `src` must be [valid]. |
| 408 | +/// * `src` must be [valid] for reads. |
399 | 409 | ///
|
400 | 410 | /// * `src` must be properly aligned. Use [`read_unaligned`] if this is not the
|
401 | 411 | /// case.
|
@@ -508,7 +518,7 @@ pub unsafe fn read<T>(src: *const T) -> T {
|
508 | 518 | ///
|
509 | 519 | /// Behavior is undefined if any of the following conditions are violated:
|
510 | 520 | ///
|
511 |
| -/// * `src` must be [valid]. |
| 521 | +/// * `src` must be [valid] for reads. |
512 | 522 | ///
|
513 | 523 | /// Like [`read`], `read_unaligned` creates a bitwise copy of `T`, regardless of
|
514 | 524 | /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the returned
|
@@ -585,7 +595,7 @@ pub unsafe fn read_unaligned<T>(src: *const T) -> T {
|
585 | 595 | ///
|
586 | 596 | /// Behavior is undefined if any of the following conditions are violated:
|
587 | 597 | ///
|
588 |
| -/// * `dst` must be [valid]. |
| 598 | +/// * `dst` must be [valid] for writes. |
589 | 599 | ///
|
590 | 600 | /// * `dst` must be properly aligned. Use [`write_unaligned`] if this is not the
|
591 | 601 | /// case.
|
@@ -659,7 +669,7 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
|
659 | 669 | ///
|
660 | 670 | /// Behavior is undefined if any of the following conditions are violated:
|
661 | 671 | ///
|
662 |
| -/// * `dst` must be [valid]. |
| 672 | +/// * `dst` must be [valid] for writes. |
663 | 673 | ///
|
664 | 674 | /// [valid]: ../ptr/index.html#safety
|
665 | 675 | ///
|
@@ -734,7 +744,7 @@ pub unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
|
734 | 744 | ///
|
735 | 745 | /// Behavior is undefined if any of the following conditions are violated:
|
736 | 746 | ///
|
737 |
| -/// * `src` must be [valid]. |
| 747 | +/// * `src` must be [valid] for reads. |
738 | 748 | ///
|
739 | 749 | /// * `src` must be properly aligned.
|
740 | 750 | ///
|
@@ -809,7 +819,7 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
|
809 | 819 | ///
|
810 | 820 | /// Behavior is undefined if any of the following conditions are violated:
|
811 | 821 | ///
|
812 |
| -/// * `dst` must be [valid]. |
| 822 | +/// * `dst` must be [valid] for writes. |
813 | 823 | ///
|
814 | 824 | /// * `dst` must be properly aligned.
|
815 | 825 | ///
|
|
0 commit comments