diff options
| author | bors <bors@rust-lang.org> | 2020-01-09 19:21:58 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-01-09 19:21:58 +0000 |
| commit | 72b2bd55edbb1e63a930c5ddd08b25e4f9044786 (patch) | |
| tree | ce6695b7a17f751eb6c469151be83373687c6952 /src/libcore/ptr | |
| parent | 59eb49d0da83fff01ae3c63f2e282b953e5f88df (diff) | |
| parent | 9e83df0f69d0509c411808766c3472f11476bd9e (diff) | |
Auto merge of #68067 - JohnTitor:rollup-vsj5won, r=JohnTitor
Rollup of 10 pull requests Successful merges: - #66254 (Make Layout::new const) - #67122 (Do not deduplicate diagnostics in UI tests) - #67358 (Add HashSet::get_or_insert_owned) - #67725 (Simplify into_key_slice_mut) - #67935 (Relax the Sized bounds on Pin::map_unchecked(_mut)) - #67967 (Delay bug to prevent ICE in MIR borrowck) - #67975 (Export public scalar statics in wasm) - #68006 (Recognise riscv64 in compiletest) - #68040 (Cleanup) - #68054 (doc: add Null-unchecked version section to mut pointer as_mut method) Failed merges: - #67258 (Introduce `X..`, `..X`, and `..=X` range patterns) r? @ghost
Diffstat (limited to 'src/libcore/ptr')
| -rw-r--r-- | src/libcore/ptr/mut_ptr.rs | 14 |
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> { |
