Skip to content

Commit

Permalink
feature(strict_provenance) in doc examples
Browse files Browse the repository at this point in the history
and unsafe
and ptr methods
and cleaning up unused uses
and intra-doc links...
  • Loading branch information
workingjubilee committed Mar 23, 2022
1 parent 81b7942 commit 8cc6326
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 28 deletions.
1 change: 1 addition & 0 deletions library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,7 @@ extern "rust-intrinsic" {
/// Turning a pointer into a `usize`:
///
/// ```
/// #![feature(strict_provenance)]
/// let ptr = &0;
/// let ptr_num_transmute = unsafe {
/// std::mem::transmute::<&i32, usize>(ptr)
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
#![feature(ptr_metadata)]
#![feature(slice_ptr_get)]
#![feature(str_internals)]
#![feature(strict_provenance)]
#![feature(utf16_extra)]
#![feature(utf16_extra_const)]
#![feature(variant_count)]
Expand Down
28 changes: 17 additions & 11 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ impl<T: ?Sized> *const T {
/// and cannot be created from one without additional context.
///
/// If you would like to treat a pointer like an integer anyway,
/// see [`addr`][#method.addr-1] and [`with_addr`][#method.with_addr-1] for the responsible
/// way to do that.
/// see [`addr`] and [`with_addr`] for the responsible way to do that.
///
/// [`addr`]: pointer::addr
/// [`with_addr`]: pointer::with_addr
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
pub fn to_bits(self) -> [u8; core::mem::size_of::<*const ()>()]
where
Expand Down Expand Up @@ -107,8 +109,10 @@ impl<T: ?Sized> *const T {
/// and is equivalent to the deprecated `ptr as usize` cast.
///
/// On more complicated platforms like CHERI and segmented architectures,
/// this may remove some important metadata. See [`with_addr`][#method.with_addr-1] for
/// this may remove some important metadata. See [`with_addr`] for
/// details on this distinction and why it's important.
///
/// [`with_addr`]: pointer::with_addr
#[unstable(feature = "strict_provenance", issue = "99999999")]
pub fn addr(self) -> usize
where
Expand Down Expand Up @@ -151,6 +155,7 @@ impl<T: ?Sized> *const T {
/// with tagged pointers. Here we have a tag in the lowest bit:
///
/// ```text
/// #![feature(strict_provenance)]
/// let my_tagged_ptr: *const T = ...;
///
/// // Get the address and do whatever bit tricks we like
Expand Down Expand Up @@ -342,7 +347,7 @@ impl<T: ?Sized> *const T {
/// enables more aggressive compiler optimizations.
///
/// [`wrapping_offset`]: #method.wrapping_offset
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Examples
///
Expand Down Expand Up @@ -429,7 +434,7 @@ impl<T: ?Sized> *const T {
/// platform-specific and not at all portable.
///
/// [`offset`]: #method.offset
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Examples
///
Expand Down Expand Up @@ -505,7 +510,7 @@ impl<T: ?Sized> *const T {
/// such large allocations either.)
///
/// [`add`]: #method.add
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Panics
///
Expand All @@ -530,6 +535,7 @@ impl<T: ?Sized> *const T {
/// *Incorrect* usage:
///
/// ```rust,no_run
/// # #![feature(strict_provenance)]
/// let ptr1 = Box::into_raw(Box::new(0u8)) as *const u8;
/// let ptr2 = Box::into_raw(Box::new(1u8)) as *const u8;
/// let diff = (ptr2.addr() as isize).wrapping_sub(ptr1.addr() as isize);
Expand Down Expand Up @@ -654,7 +660,7 @@ impl<T: ?Sized> *const T {
/// enables more aggressive compiler optimizations.
///
/// [`wrapping_add`]: #method.wrapping_add
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Examples
///
Expand Down Expand Up @@ -718,7 +724,7 @@ impl<T: ?Sized> *const T {
/// enables more aggressive compiler optimizations.
///
/// [`wrapping_sub`]: #method.wrapping_sub
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Examples
///
Expand Down Expand Up @@ -775,7 +781,7 @@ impl<T: ?Sized> *const T {
/// allocated object and then re-entering it later is permitted.
///
/// [`add`]: #method.add
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Examples
///
Expand Down Expand Up @@ -837,7 +843,7 @@ impl<T: ?Sized> *const T {
/// allocated object and then re-entering it later is permitted.
///
/// [`sub`]: #method.sub
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Examples
///
Expand Down Expand Up @@ -1183,7 +1189,7 @@ impl<T> *const [T] {
/// See also [`slice::from_raw_parts`][].
///
/// [valid]: crate::ptr#safety
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
#[inline]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
Expand Down
9 changes: 5 additions & 4 deletions library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
//! has size 0, i.e., even if memory is not actually touched. Consider using
//! [`NonNull::dangling`] in such cases.
//!
//! ## Allocated Object and Provenance
//! ## Allocated Objects and Provenance
//!
//! For several operations, such as [`offset`] or field projections (`expr.field`), the notion of an
//! "allocated object" becomes relevant. An allocated object is a contiguous region of memory.
Expand Down Expand Up @@ -297,14 +297,15 @@ pub const fn null_mut<T>() -> *mut T {
/// # Example
///
/// ```
/// use core::{ptr, mem};
/// #![feature(strict_provenance)]
/// use core::ptr;
///
/// // I store my ZSTs at the *coolest* address
/// let my_good_ptr = ptr::zst_exists::<()>(0xc001_add7);
///
/// // "store" and then "load" a ZST at this cool address.
/// my_good_ptr.write(());
/// let output = my_good_ptr.read();
/// unsafe { my_good_ptr.write(()); }
/// let output = unsafe { my_good_ptr.read() };
/// ```
#[inline(always)]
#[must_use]
Expand Down
30 changes: 18 additions & 12 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ impl<T: ?Sized> *mut T {
/// and cannot be created from one without additional context.
///
/// If you would like to treat a pointer like an integer anyway,
/// see [`addr`][#method.addr-1] and [`with_addr`][#method.with_addr-1] for
/// the responsible way to do that.
/// see [`addr`] and [`with_addr`] for the responsible way to do that.
///
/// [`addr`]: pointer::addr
/// [`with_addr`]: pointer::with_addr
#[unstable(feature = "ptr_to_from_bits", issue = "91126")]
pub fn to_bits(self) -> [u8; core::mem::size_of::<*mut ()>()]
where
Expand Down Expand Up @@ -110,8 +112,10 @@ impl<T: ?Sized> *mut T {
/// and is equivalent to the deprecated `ptr as usize` cast.
///
/// On more complicated platforms like CHERI and segmented architectures,
/// this may remove some important metadata. See [`with_addr`][#method.with_addr-1] for
/// this may remove some important metadata. See [`with_addr`] for
/// details on this distinction and why it's important.
///
/// [`with_addr`]: pointer::with_addr
#[unstable(feature = "strict_provenance", issue = "99999999")]
pub fn addr(self) -> usize
where
Expand Down Expand Up @@ -154,6 +158,7 @@ impl<T: ?Sized> *mut T {
/// with tagged pointers. Here we have a tag in the lowest bit:
///
/// ```text
/// #![feature(strict_provenance)]
/// let my_tagged_ptr: *mut T = ...;
///
/// // Get the address and do whatever bit tricks we like
Expand Down Expand Up @@ -352,7 +357,7 @@ impl<T: ?Sized> *mut T {
/// enables more aggressive compiler optimizations.
///
/// [`wrapping_offset`]: #method.wrapping_offset
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Examples
///
Expand Down Expand Up @@ -440,7 +445,7 @@ impl<T: ?Sized> *mut T {
/// platform-specific and not at all portable.
///
/// [`offset`]: #method.offset
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Examples
///
Expand Down Expand Up @@ -683,7 +688,7 @@ impl<T: ?Sized> *mut T {
/// such large allocations either.)
///
/// [`add`]: #method.add
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Panics
///
Expand All @@ -708,6 +713,7 @@ impl<T: ?Sized> *mut T {
/// *Incorrect* usage:
///
/// ```rust,no_run
/// #![feature(strict_provenance)]
/// let ptr1 = Box::into_raw(Box::new(0u8));
/// let ptr2 = Box::into_raw(Box::new(1u8));
/// let diff = (ptr2.addr() as isize).wrapping_sub(ptr1.addr() as isize);
Expand Down Expand Up @@ -768,7 +774,7 @@ impl<T: ?Sized> *mut T {
/// enables more aggressive compiler optimizations.
///
/// [`wrapping_add`]: #method.wrapping_add
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Examples
///
Expand Down Expand Up @@ -832,7 +838,7 @@ impl<T: ?Sized> *mut T {
/// enables more aggressive compiler optimizations.
///
/// [`wrapping_sub`]: #method.wrapping_sub
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Examples
///
Expand Down Expand Up @@ -889,7 +895,7 @@ impl<T: ?Sized> *mut T {
/// allocated object and then re-entering it later is permitted.
///
/// [`add`]: #method.add
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Examples
///
Expand Down Expand Up @@ -951,7 +957,7 @@ impl<T: ?Sized> *mut T {
/// allocated object and then re-entering it later is permitted.
///
/// [`sub`]: #method.sub
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
///
/// # Examples
///
Expand Down Expand Up @@ -1456,7 +1462,7 @@ impl<T> *mut [T] {
/// See also [`slice::from_raw_parts`][].
///
/// [valid]: crate::ptr#safety
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
#[inline]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
Expand Down Expand Up @@ -1508,7 +1514,7 @@ impl<T> *mut [T] {
/// See also [`slice::from_raw_parts_mut`][].
///
/// [valid]: crate::ptr#safety
/// [allocated object]: crate::ptr#allocated-object
/// [allocated object]: crate::ptr#allocated-objects-and-provenance
#[inline]
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ impl<T> NonNull<[T]> {
/// use std::ptr::NonNull;
///
/// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
/// assert_eq!(slice.as_mut_ptr(), NonNull::<i8>::dangling());
/// assert_eq!(slice.as_mut_ptr(), NonNull::<i8>::dangling().as_ptr());
/// ```
#[inline]
#[must_use]
Expand Down

0 comments on commit 8cc6326

Please sign in to comment.