Skip to content

Commit 1d5ea88

Browse files
authored
Rollup merge of rust-lang#62822 - RalfJung:pointers, r=Centril
Improve some pointer-related documentation
2 parents cb1b6c9 + 9196781 commit 1d5ea88

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/libstd/primitive_docs.rs

+17-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`] when unused. 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,10 @@ 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 valid (initialized) memory.
902+
/// In fact, `Option<&T>` has the same memory representation as a
903+
/// nullable but aligned pointer, and can be passed across FFI boundaries as such.
897904
///
898905
/// In most cases, references can be used much like the original value. Field access, method
899906
/// calling, and indexing work the same (save for mutability rules, of course). In addition, the
@@ -1036,6 +1043,11 @@ mod prim_ref { }
10361043
/// [`FnMut`]: ops/trait.FnMut.html
10371044
/// [`FnOnce`]: ops/trait.FnOnce.html
10381045
///
1046+
/// Function pointers are pointers that point to *code*, not data. They can be called
1047+
/// just like functions. Like references, function pointers are, among other things, assumed to
1048+
/// not be null, so if you want to pass a function pointer over FFI and be able to accommodate null
1049+
/// pointers, make your type `Option<fn()>` with your required signature.
1050+
///
10391051
/// Plain function pointers are obtained by casting either plain functions, or closures that don't
10401052
/// capture an environment:
10411053
///
@@ -1091,10 +1103,6 @@ mod prim_ref { }
10911103
///
10921104
/// These markers can be combined, so `unsafe extern "stdcall" fn()` is a valid type.
10931105
///
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-
///
10981106
/// Function pointers implement the following traits:
10991107
///
11001108
/// * [`Clone`]

0 commit comments

Comments
 (0)