Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve some pointer-related documentation #62822

Merged
merged 8 commits into from
Jul 26, 2019
23 changes: 14 additions & 9 deletions src/libstd/primitive_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,11 @@ mod prim_unit { }
///
/// *[See also the `std::ptr` module](ptr/index.html).*
///
/// Working with raw pointers in Rust is uncommon,
/// typically limited to a few patterns.
/// Working with raw pointers in Rust is uncommon, typically limited to a few patterns.
/// Raw pointers can be unaligned or null when unused. However, when a raw pointer is used to
RalfJung marked this conversation as resolved.
Show resolved Hide resolved
/// load/store data from/to memory, they must be non-null and aligned.
RalfJung marked this conversation as resolved.
Show resolved Hide resolved
/// Storing through a raw pointer (`*ptr = data`) calls `drop` on the old value, so
RalfJung marked this conversation as resolved.
Show resolved Hide resolved
/// [`write`] must be used if memory is not already initialized.
///
/// Use the [`null`] and [`null_mut`] functions to create null pointers, and the
/// [`is_null`] method of the `*const T` and `*mut T` types to check for null.
Expand Down Expand Up @@ -442,6 +445,7 @@ mod prim_unit { }
/// [`offset`]: ../std/primitive.pointer.html#method.offset
/// [`into_raw`]: ../std/boxed/struct.Box.html#method.into_raw
/// [`drop`]: ../std/mem/fn.drop.html
/// [`write`]: ../std/ptr/fn.write.html
#[stable(feature = "rust1", since = "1.0.0")]
mod prim_pointer { }

Expand Down Expand Up @@ -891,9 +895,9 @@ mod prim_usize { }
/// A reference represents a borrow of some owned value. You can get one by using the `&` or `&mut`
/// operators on a value, or by using a `ref` or `ref mut` pattern.
///
/// For those familiar with pointers, a reference is just a pointer that is assumed to not be null.
/// In fact, `Option<&T>` has the same memory representation as a nullable pointer, and can be
/// passed across FFI boundaries as such.
/// For those familiar with pointers, a reference is just a pointer that is assumed to be
Centril marked this conversation as resolved.
Show resolved Hide resolved
/// aligned and not null. In fact, `Option<&T>` has the same memory representation as a
/// nullable but aligned pointer, and can be passed across FFI boundaries as such.
///
/// In most cases, references can be used much like the original value. Field access, method
/// calling, and indexing work the same (save for mutability rules, of course). In addition, the
Expand Down Expand Up @@ -1036,6 +1040,11 @@ mod prim_ref { }
/// [`FnMut`]: ops/trait.FnMut.html
/// [`FnOnce`]: ops/trait.FnOnce.html
///
/// Function pointers are pointers that point to *code*, not data. They can be called
RalfJung marked this conversation as resolved.
Show resolved Hide resolved
/// just like functions. Like references, function pointers are assumed to not be null,
RalfJung marked this conversation as resolved.
Show resolved Hide resolved
/// so if you want to pass a function pointer over FFI and be able to accommodate null pointers,
/// make your type `Option<fn()>` with your required signature.
///
/// Plain function pointers are obtained by casting either plain functions, or closures that don't
/// capture an environment:
///
Expand Down Expand Up @@ -1091,10 +1100,6 @@ mod prim_ref { }
///
/// These markers can be combined, so `unsafe extern "stdcall" fn()` is a valid type.
///
/// Like references in rust, function pointers are assumed to not be null, so if you want to pass a
/// function pointer over FFI and be able to accommodate null pointers, make your type
/// `Option<fn()>` with your required signature.
///
/// Function pointers implement the following traits:
///
/// * [`Clone`]
Expand Down