Skip to content

Commit 3374a7d

Browse files
authored
Rollup merge of #102607 - WaffleLapkin:docky_docky_slice_from_ptr_range, r=joshtriplett
Improve documentation of `slice::{from_ptr_range, from_ptr_range_mut}` Document panic conditions (`T` is a ZST) and sync docs of shared/unique version. cc `@wx-csy`
2 parents 8ede234 + 2cd5faf commit 3374a7d

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

library/core/src/slice/raw.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
188188
///
189189
/// Note that a range created from [`slice::as_ptr_range`] fulfills these requirements.
190190
///
191+
/// # Panics
192+
///
193+
/// This function panics if `T` is a Zero-Sized Type (“ZST”).
194+
///
191195
/// # Caveat
192196
///
193197
/// The lifetime for the returned slice is inferred from its usage. To
@@ -219,9 +223,15 @@ pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
219223
unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) }
220224
}
221225

222-
/// Performs the same functionality as [`from_ptr_range`], except that a
226+
/// Forms a mutable slice from a pointer range.
227+
///
228+
/// This is the same functionality as [`from_ptr_range`], except that a
223229
/// mutable slice is returned.
224230
///
231+
/// This function is useful for interacting with foreign interfaces which
232+
/// use two pointers to refer to a range of elements in memory, as is
233+
/// common in C++.
234+
///
225235
/// # Safety
226236
///
227237
/// Behavior is undefined if any of the following conditions are violated:
@@ -247,6 +257,18 @@ pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
247257
///
248258
/// Note that a range created from [`slice::as_mut_ptr_range`] fulfills these requirements.
249259
///
260+
/// # Panics
261+
///
262+
/// This function panics if `T` is a Zero-Sized Type (“ZST”).
263+
///
264+
/// # Caveat
265+
///
266+
/// The lifetime for the returned slice is inferred from its usage. To
267+
/// prevent accidental misuse, it's suggested to tie the lifetime to whichever
268+
/// source lifetime is safe in the context, such as by providing a helper
269+
/// function taking the lifetime of a host value for the slice, or by explicit
270+
/// annotation.
271+
///
250272
/// # Examples
251273
///
252274
/// ```

0 commit comments

Comments
 (0)