use crate::ptr::Unique;
use crate::slice::{self, SliceIndex};
-/// `*mut T` but non-zero and covariant.
+/// `*mut T` but non-zero and [covariant].
///
/// This is often the correct thing to use when building data structures using
/// raw pointers, but is ultimately more dangerous to use because of its additional
/// it is your responsibility to ensure that `as_mut` is never called, and `as_ptr`
/// is never used for mutation.
///
+/// [covariant]: https://doc.rust-lang.org/reference/subtyping.html
/// [`PhantomData`]: crate::marker::PhantomData
/// [`UnsafeCell<T>`]: crate::cell::UnsafeCell
#[stable(feature = "nonnull", since = "1.25.0")]
///
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
- /// In particular, for the duration of this lifetime, the memory the pointer points to must
+ /// In particular, while this reference exists, the memory the pointer points to must
/// not get mutated (except inside `UnsafeCell`).
///
/// This applies even if the result of this method is unused!
#[must_use]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
- pub const unsafe fn as_uninit_ref<'a>(&self) -> &'a MaybeUninit<T> {
+ pub const unsafe fn as_uninit_ref<'a>(self) -> &'a MaybeUninit<T> {
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference.
unsafe { &*self.cast().as_ptr() }
///
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
- /// In particular, for the duration of this lifetime, the memory the pointer points to must
+ /// In particular, while this reference exists, the memory the pointer points to must
/// not get accessed (read or written) through any other pointer.
///
/// This applies even if the result of this method is unused!
#[must_use]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
- pub const unsafe fn as_uninit_mut<'a>(&mut self) -> &'a mut MaybeUninit<T> {
+ pub const unsafe fn as_uninit_mut<'a>(self) -> &'a mut MaybeUninit<T> {
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference.
unsafe { &mut *self.cast().as_ptr() }
/// Gets the "address" portion of the pointer.
///
+ /// For more details see the equivalent method on a raw pointer, [`pointer::addr`].
+ ///
/// This API and its claimed semantics are part of the Strict Provenance experiment,
- /// see the [module documentation][crate::ptr] for details.
+ /// see the [`ptr` module documentation][crate::ptr].
#[must_use]
#[inline]
#[unstable(feature = "strict_provenance", issue = "95228")]
/// Creates a new pointer with the given address.
///
+ /// For more details see the equivalent method on a raw pointer, [`pointer::with_addr`].
+ ///
/// This API and its claimed semantics are part of the Strict Provenance experiment,
- /// see the [module documentation][crate::ptr] for details.
+ /// see the [`ptr` module documentation][crate::ptr].
#[must_use]
#[inline]
#[unstable(feature = "strict_provenance", issue = "95228")]
/// Creates a new pointer by mapping `self`'s address to a new one.
///
- /// This is a convenience for [`with_addr`][Self::with_addr], see that method for details.
+ /// For more details see the equivalent method on a raw pointer, [`pointer::map_addr`].
///
/// This API and its claimed semantics are part of the Strict Provenance experiment,
- /// see the [module documentation][crate::ptr] for details.
+ /// see the [`ptr` module documentation][crate::ptr].
#[must_use]
#[inline]
#[unstable(feature = "strict_provenance", issue = "95228")]
///
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
- /// In particular, for the duration of this lifetime, the memory the pointer points to must
+ /// In particular, while this reference exists, the memory the pointer points to must
/// not get mutated (except inside `UnsafeCell`).
///
/// This applies even if the result of this method is unused!
///
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
- /// In particular, for the duration of this lifetime, the memory the pointer points to must
+ /// In particular, while this reference exists, the memory the pointer points to must
/// not get accessed (read or written) through any other pointer.
///
/// This applies even if the result of this method is unused!
/// # Examples
///
/// ```rust
- /// #![feature(slice_ptr_len, nonnull_slice_from_raw_parts)]
+ /// #![feature(nonnull_slice_from_raw_parts)]
/// use std::ptr::NonNull;
///
/// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
/// assert_eq!(slice.len(), 3);
/// ```
- #[unstable(feature = "slice_ptr_len", issue = "71146")]
- #[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
+ #[stable(feature = "slice_ptr_len_nonnull", since = "1.63.0")]
+ #[rustc_const_stable(feature = "const_slice_ptr_len_nonnull", since = "1.63.0")]
+ #[rustc_allow_const_fn_unstable(const_slice_ptr_len)]
#[must_use]
#[inline]
pub const fn len(self) -> usize {
///
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
- /// In particular, for the duration of this lifetime, the memory the pointer points to must
+ /// In particular, while this reference exists, the memory the pointer points to must
/// not get mutated (except inside `UnsafeCell`).
///
/// This applies even if the result of this method is unused!
#[must_use]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
- pub const unsafe fn as_uninit_slice<'a>(&self) -> &'a [MaybeUninit<T>] {
+ pub const unsafe fn as_uninit_slice<'a>(self) -> &'a [MaybeUninit<T>] {
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice`.
unsafe { slice::from_raw_parts(self.cast().as_ptr(), self.len()) }
}
///
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
- /// In particular, for the duration of this lifetime, the memory the pointer points to must
+ /// In particular, while this reference exists, the memory the pointer points to must
/// not get accessed (read or written) through any other pointer.
///
/// This applies even if the result of this method is unused!
#[must_use]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
- pub const unsafe fn as_uninit_slice_mut<'a>(&self) -> &'a mut [MaybeUninit<T>] {
+ pub const unsafe fn as_uninit_slice_mut<'a>(self) -> &'a mut [MaybeUninit<T>] {
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice_mut`.
unsafe { slice::from_raw_parts_mut(self.cast().as_ptr(), self.len()) }
}