@@ -142,6 +142,65 @@ impl<T: ?Sized> NonNull<T> {
142
142
}
143
143
}
144
144
145
+ impl < T > NonNull < [ T ] > {
146
+ /// Creates a non-null raw slice from a thin pointer and a length.
147
+ ///
148
+ /// The `len` argument is the number of **elements**, not the number of bytes.
149
+ ///
150
+ /// This function is safe, but dereferencing the return value is unsafe.
151
+ /// See the documentation of [`slice::from_raw_parts`] for slice safety requirements.
152
+ ///
153
+ /// [`slice::from_raw_parts`]: ../../std/slice/fn.from_raw_parts.html
154
+ ///
155
+ /// # Examples
156
+ ///
157
+ /// ```rust
158
+ /// #![feature(nonnull_slice_from_raw_parts)]
159
+ ///
160
+ /// use std::ptr::NonNull;
161
+ ///
162
+ /// // create a slice pointer when starting out with a pointer to the first element
163
+ /// let mut x = [5, 6, 7];
164
+ /// let nonnull_pointer = NonNull::new(x.as_mut_ptr()).unwrap();
165
+ /// let slice = NonNull::slice_from_raw_parts(nonnull_pointer, 3);
166
+ /// assert_eq!(unsafe { slice.as_ref()[2] }, 7);
167
+ /// ```
168
+ ///
169
+ /// (Note that this example artifically demonstrates a use of this method,
170
+ /// but `let slice = NonNull::from(&x[..]);` would be a better way to write code like this.)
171
+ #[ unstable( feature = "nonnull_slice_from_raw_parts" , issue = "71941" ) ]
172
+ #[ rustc_const_unstable( feature = "const_nonnull_slice_from_raw_parts" , issue = "71941" ) ]
173
+ #[ inline]
174
+ pub const fn slice_from_raw_parts ( data : NonNull < T > , len : usize ) -> Self {
175
+ // SAFETY: `data` is a `NonNull` pointer which is necessarily non-null
176
+ unsafe { Self :: new_unchecked ( super :: slice_from_raw_parts_mut ( data. as_ptr ( ) , len) ) }
177
+ }
178
+
179
+ /// Returns the length of a non-null raw slice.
180
+ ///
181
+ /// The returned value is the number of **elements**, not the number of bytes.
182
+ ///
183
+ /// This function is safe, even when the non-null raw slice cannot be dereferenced to a slice
184
+ /// because the pointer does not have a valid address.
185
+ ///
186
+ /// # Examples
187
+ ///
188
+ /// ```rust
189
+ /// #![feature(slice_ptr_len, nonnull_slice_from_raw_parts)]
190
+ ///
191
+ /// use std::ptr::NonNull;
192
+ ///
193
+ /// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
194
+ /// assert_eq!(slice.len(), 3);
195
+ /// ```
196
+ #[ unstable( feature = "slice_ptr_len" , issue = "71146" ) ]
197
+ #[ rustc_const_unstable( feature = "const_slice_ptr_len" , issue = "71146" ) ]
198
+ #[ inline]
199
+ pub const fn len ( self ) -> usize {
200
+ self . as_ptr ( ) . len ( )
201
+ }
202
+ }
203
+
145
204
#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
146
205
impl < T : ?Sized > Clone for NonNull < T > {
147
206
#[ inline]
0 commit comments