Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8bc8562

Browse files
authoredJul 25, 2019
Rollup merge of rust-lang#62822 - RalfJung:pointers, r=Centril
Improve some pointer-related documentation
2 parents ba07b39 + 65cf10d commit 8bc8562

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed
 

‎src/libstd/primitive_docs.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,13 @@ mod prim_unit { }
362362
///
363363
/// *[See also the `std::ptr` module](ptr/index.html).*
364364
///
365-
/// Working with raw pointers in Rust is uncommon,
366-
/// typically limited to a few patterns.
365+
/// Working with raw pointers in Rust is uncommon, typically limited to a few patterns.
366+
/// Raw pointers can be unaligned or [`null`]. However, when a raw pointer is
367+
/// dereferenced (using the `*` operator), it must be non-null and aligned.
368+
///
369+
/// Storing through a raw pointer using `*ptr = data` calls `drop` on the old value, so
370+
/// [`write`] must be used if the type has drop glue and memory is not already
371+
/// initialized - otherwise `drop` would be called on the uninitialized memory.
367372
///
368373
/// Use the [`null`] and [`null_mut`] functions to create null pointers, and the
369374
/// [`is_null`] method of the `*const T` and `*mut T` types to check for null.
@@ -442,6 +447,7 @@ mod prim_unit { }
442447
/// [`offset`]: ../std/primitive.pointer.html#method.offset
443448
/// [`into_raw`]: ../std/boxed/struct.Box.html#method.into_raw
444449
/// [`drop`]: ../std/mem/fn.drop.html
450+
/// [`write`]: ../std/ptr/fn.write.html
445451
#[stable(feature = "rust1", since = "1.0.0")]
446452
mod prim_pointer { }
447453

@@ -891,9 +897,13 @@ mod prim_usize { }
891897
/// A reference represents a borrow of some owned value. You can get one by using the `&` or `&mut`
892898
/// operators on a value, or by using a `ref` or `ref mut` pattern.
893899
///
894-
/// For those familiar with pointers, a reference is just a pointer that is assumed to not be null.
895-
/// In fact, `Option<&T>` has the same memory representation as a nullable pointer, and can be
896-
/// passed across FFI boundaries as such.
900+
/// For those familiar with pointers, a reference is just a pointer that is assumed to be
901+
/// aligned, not null, and pointing to memory containing a valid value of `T` - for example,
902+
/// `&bool` can only point to an allocation containing the integer values `1` (`true`) or `0`
903+
/// (`false`), but creating a `&bool` that points to an allocation containing
904+
/// the value `3` causes undefined behaviour.
905+
/// In fact, `Option<&T>` has the same memory representation as a
906+
/// nullable but aligned pointer, and can be passed across FFI boundaries as such.
897907
///
898908
/// In most cases, references can be used much like the original value. Field access, method
899909
/// calling, and indexing work the same (save for mutability rules, of course). In addition, the
@@ -1036,6 +1046,11 @@ mod prim_ref { }
10361046
/// [`FnMut`]: ops/trait.FnMut.html
10371047
/// [`FnOnce`]: ops/trait.FnOnce.html
10381048
///
1049+
/// Function pointers are pointers that point to *code*, not data. They can be called
1050+
/// just like functions. Like references, function pointers are, among other things, assumed to
1051+
/// not be null, so if you want to pass a function pointer over FFI and be able to accommodate null
1052+
/// pointers, make your type `Option<fn()>` with your required signature.
1053+
///
10391054
/// Plain function pointers are obtained by casting either plain functions, or closures that don't
10401055
/// capture an environment:
10411056
///
@@ -1091,10 +1106,6 @@ mod prim_ref { }
10911106
///
10921107
/// These markers can be combined, so `unsafe extern "stdcall" fn()` is a valid type.
10931108
///
1094-
/// Like references in rust, function pointers are assumed to not be null, so if you want to pass a
1095-
/// function pointer over FFI and be able to accommodate null pointers, make your type
1096-
/// `Option<fn()>` with your required signature.
1097-
///
10981109
/// Function pointers implement the following traits:
10991110
///
11001111
/// * [`Clone`]

0 commit comments

Comments
 (0)
Please sign in to comment.