@@ -362,8 +362,13 @@ mod prim_unit { }
362
362
///
363
363
/// *[See also the `std::ptr` module](ptr/index.html).*
364
364
///
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.
367
372
///
368
373
/// Use the [`null`] and [`null_mut`] functions to create null pointers, and the
369
374
/// [`is_null`] method of the `*const T` and `*mut T` types to check for null.
@@ -442,6 +447,7 @@ mod prim_unit { }
442
447
/// [`offset`]: ../std/primitive.pointer.html#method.offset
443
448
/// [`into_raw`]: ../std/boxed/struct.Box.html#method.into_raw
444
449
/// [`drop`]: ../std/mem/fn.drop.html
450
+ /// [`write`]: ../std/ptr/fn.write.html
445
451
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
446
452
mod prim_pointer { }
447
453
@@ -891,9 +897,13 @@ mod prim_usize { }
891
897
/// A reference represents a borrow of some owned value. You can get one by using the `&` or `&mut`
892
898
/// operators on a value, or by using a `ref` or `ref mut` pattern.
893
899
///
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.
897
907
///
898
908
/// In most cases, references can be used much like the original value. Field access, method
899
909
/// calling, and indexing work the same (save for mutability rules, of course). In addition, the
@@ -1036,6 +1046,11 @@ mod prim_ref { }
1036
1046
/// [`FnMut`]: ops/trait.FnMut.html
1037
1047
/// [`FnOnce`]: ops/trait.FnOnce.html
1038
1048
///
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
+ ///
1039
1054
/// Plain function pointers are obtained by casting either plain functions, or closures that don't
1040
1055
/// capture an environment:
1041
1056
///
@@ -1091,10 +1106,6 @@ mod prim_ref { }
1091
1106
///
1092
1107
/// These markers can be combined, so `unsafe extern "stdcall" fn()` is a valid type.
1093
1108
///
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
- ///
1098
1109
/// Function pointers implement the following traits:
1099
1110
///
1100
1111
/// * [`Clone`]
0 commit comments