From 9b7f9c4328dde77396f815dc929de6cdbc700531 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 26 Sep 2023 16:41:06 +0200 Subject: [PATCH] take more clarifying text from Gankra's PR original source: https://github.com/rust-lang/rust/pull/95851 --- library/core/src/ptr/const_ptr.rs | 7 +++++++ library/core/src/ptr/mut_ptr.rs | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 3e68216e40eda..9af8f1228f0b6 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -611,6 +611,13 @@ impl *const T { /// except that it has a lot more opportunities for UB, in exchange for the compiler /// better understanding what you are doing. /// + /// The primary motivation of this method is for computing the `len` of an array/slice + /// of `T` that you are currently representing as a "start" and "end" pointer + /// (and "end" is "one past the end" of the array). + /// In that case, `end.offset_from(start)` gets you the length of the array. + /// + /// All of the following safety requirements are trivially satisfied for this usecase. + /// /// [`offset`]: #method.offset /// /// # Safety diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index c89ce52292a3d..109c286929b4e 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -785,6 +785,13 @@ impl *mut T { /// except that it has a lot more opportunities for UB, in exchange for the compiler /// better understanding what you are doing. /// + /// The primary motivation of this method is for computing the `len` of an array/slice + /// of `T` that you are currently representing as a "start" and "end" pointer + /// (and "end" is "one past the end" of the array). + /// In that case, `end.offset_from(start)` gets you the length of the array. + /// + /// All of the following safety requirements are trivially satisfied for this usecase. + /// /// [`offset`]: pointer#method.offset-1 /// /// # Safety