Skip to content

Commit 7494224

Browse files
committed
clarify language around non-null ptrs in slice::raw
1 parent 9b82580 commit 7494224

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

library/core/src/slice/raw.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ use crate::{array, ptr, ub_checks};
1111
///
1212
/// Behavior is undefined if any of the following conditions are violated:
1313
///
14-
/// * `data` must be [valid] for reads for `len * mem::size_of::<T>()` many bytes,
14+
/// * `data` must be non-null, [valid] for reads for `len * mem::size_of::<T>()` many bytes,
1515
/// and it must be properly aligned. This means in particular:
1616
///
1717
/// * The entire memory range of this slice must be contained within a single allocated object!
1818
/// Slices can never span across multiple allocated objects. See [below](#incorrect-usage)
1919
/// for an example incorrectly not taking this into account.
20-
/// * `data` must be non-null and aligned even for zero-length slices. One
20+
/// * `data` must be non-null and aligned even for zero-length slices or slices of ZSTs. One
2121
/// reason for this is that enum layout optimizations may rely on references
2222
/// (including slices of any length) being aligned and non-null to distinguish
2323
/// them from other data. You can obtain a pointer that is usable as `data`
@@ -146,12 +146,12 @@ pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T]
146146
///
147147
/// Behavior is undefined if any of the following conditions are violated:
148148
///
149-
/// * `data` must be [valid] for both reads and writes for `len * mem::size_of::<T>()` many bytes,
149+
/// * `data` must be non-null, [valid] for both reads and writes for `len * mem::size_of::<T>()` many bytes,
150150
/// and it must be properly aligned. This means in particular:
151151
///
152152
/// * The entire memory range of this slice must be contained within a single allocated object!
153153
/// Slices can never span across multiple allocated objects.
154-
/// * `data` must be non-null and aligned even for zero-length slices. One
154+
/// * `data` must be non-null and aligned even for zero-length slices or slices of ZSTs. One
155155
/// reason for this is that enum layout optimizations may rely on references
156156
/// (including slices of any length) being aligned and non-null to distinguish
157157
/// them from other data. You can obtain a pointer that is usable as `data`
@@ -219,7 +219,7 @@ pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
219219
///
220220
/// Behavior is undefined if any of the following conditions are violated:
221221
///
222-
/// * The `start` pointer of the range must be a [valid] and properly aligned pointer
222+
/// * The `start` pointer of the range must be a non-null, [valid] and properly aligned pointer
223223
/// to the first element of a slice.
224224
///
225225
/// * The `end` pointer must be a [valid] and properly aligned pointer to *one past*
@@ -235,7 +235,7 @@ pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
235235
/// of lifetime `'a`, except inside an `UnsafeCell`.
236236
///
237237
/// * The total length of the range must be no larger than `isize::MAX`,
238-
/// and adding that size to `data` must not "wrap around" the address space.
238+
/// and adding that size to `start` must not "wrap around" the address space.
239239
/// See the safety documentation of [`pointer::offset`].
240240
///
241241
/// Note that a range created from [`slice::as_ptr_range`] fulfills these requirements.
@@ -288,7 +288,7 @@ pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
288288
///
289289
/// Behavior is undefined if any of the following conditions are violated:
290290
///
291-
/// * The `start` pointer of the range must be a [valid] and properly aligned pointer
291+
/// * The `start` pointer of the range must be a non-null, [valid] and properly aligned pointer
292292
/// to the first element of a slice.
293293
///
294294
/// * The `end` pointer must be a [valid] and properly aligned pointer to *one past*
@@ -305,7 +305,7 @@ pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
305305
/// Both read and write accesses are forbidden.
306306
///
307307
/// * The total length of the range must be no larger than `isize::MAX`,
308-
/// and adding that size to `data` must not "wrap around" the address space.
308+
/// and adding that size to `start` must not "wrap around" the address space.
309309
/// See the safety documentation of [`pointer::offset`].
310310
///
311311
/// Note that a range created from [`slice::as_mut_ptr_range`] fulfills these requirements.

0 commit comments

Comments
 (0)