about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTrevor Spiteri <tspiteri@ieee.org>2020-01-09 14:51:58 +0100
committerTrevor Spiteri <tspiteri@ieee.org>2020-01-09 14:51:58 +0100
commitf8428cf8d8193c1a6268a046afea9bea85d4d9fe (patch)
tree0cd44e049b1ee9b98f2551dd667310a480b880d6
parentadc65725004c8aac16392fe4052c3e347181157d (diff)
downloadrust-f8428cf8d8193c1a6268a046afea9bea85d4d9fe.tar.gz
rust-f8428cf8d8193c1a6268a046afea9bea85d4d9fe.zip
doc: add Null-unchecked version section to mut pointer as_mut method
The as_ref method already has a Null-unchecked version section, its
example is a modification of the example in the main as_ref section.
Similarly the example in this commit is a modification of the example
in main as_mut section.
-rw-r--r--src/libcore/ptr/mut_ptr.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/libcore/ptr/mut_ptr.rs b/src/libcore/ptr/mut_ptr.rs
index b3bb2f179b1..4bc0a3e9faa 100644
--- a/src/libcore/ptr/mut_ptr.rs
+++ b/src/libcore/ptr/mut_ptr.rs
@@ -250,6 +250,20 @@ impl<T: ?Sized> *mut T {
     /// *first_value = 4;
     /// println!("{:?}", s); // It'll print: "[4, 2, 3]".
     /// ```
+    ///
+    /// # Null-unchecked version
+    ///
+    /// If you are sure the pointer can never be null and are looking for some kind of
+    /// `as_mut_unchecked` that returns the `&mut T` instead of `Option<&mut T>`, know that
+    /// you can dereference the pointer directly.
+    ///
+    /// ```
+    /// let mut s = [1, 2, 3];
+    /// let ptr: *mut u32 = s.as_mut_ptr();
+    /// let first_value = unsafe { &mut *ptr };
+    /// *first_value = 4;
+    /// println!("{:?}", s); // It'll print: "[4, 2, 3]".
+    /// ```
     #[stable(feature = "ptr_as_ref", since = "1.9.0")]
     #[inline]
     pub unsafe fn as_mut<'a>(self) -> Option<&'a mut T> {