From 9ae26c86c202bd8fdada1141055e1e4bc026a60b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 27 Nov 2022 13:02:34 +0100 Subject: [PATCH 1/2] add ptr::from_{ref,mut} --- library/core/src/ptr/mod.rs | 52 ++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 89b11637ecabe..2d793d43e62e7 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -516,6 +516,27 @@ pub const fn null() -> *const T { from_raw_parts(invalid(0), ()) } +/// Creates a null mutable raw pointer. +/// +/// # Examples +/// +/// ``` +/// use std::ptr; +/// +/// let p: *mut i32 = ptr::null_mut(); +/// assert!(p.is_null()); +/// ``` +#[inline(always)] +#[must_use] +#[stable(feature = "rust1", since = "1.0.0")] +#[rustc_promotable] +#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")] +#[rustc_allow_const_fn_unstable(ptr_metadata)] +#[rustc_diagnostic_item = "ptr_null_mut"] +pub const fn null_mut() -> *mut T { + from_raw_parts_mut(invalid_mut(0), ()) +} + /// Creates an invalid pointer with the given address. /// /// This is different from `addr as *const T`, which creates a pointer that picks up a previously @@ -663,25 +684,26 @@ where addr as *mut T } -/// Creates a null mutable raw pointer. +/// Convert a reference to a raw pointer. /// -/// # Examples -/// -/// ``` -/// use std::ptr; +/// This is equivalent to `r as *const T`, but is a bit safer since it will never silently change +/// type or mutability, in particular if the code is refactored. +#[inline(always)] +#[must_use] +#[unstable(feature = "ptr_from_ref", issue = "999999")] // FIXME +pub fn from_ref(r: &T) -> *const T { + r +} + +/// Convert a mutble reference to a raw pointer. /// -/// let p: *mut i32 = ptr::null_mut(); -/// assert!(p.is_null()); -/// ``` +/// This is equivalent to `r as *mut T`, but is a bit safer since it will never silently change +/// type or mutability, in particular if the code is refactored. #[inline(always)] #[must_use] -#[stable(feature = "rust1", since = "1.0.0")] -#[rustc_promotable] -#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")] -#[rustc_allow_const_fn_unstable(ptr_metadata)] -#[rustc_diagnostic_item = "ptr_null_mut"] -pub const fn null_mut() -> *mut T { - from_raw_parts_mut(invalid_mut(0), ()) +#[unstable(feature = "ptr_from_ref", issue = "999999")] // FIXME +pub fn from_mut(r: &mut T) -> *mut T { + r } /// Forms a raw slice from a pointer and a length. From 15f72dd29d083bfcade93ec2b57350c2b37feed6 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 24 Dec 2022 10:47:31 +0100 Subject: [PATCH 2/2] add tracking issue, fix typo --- library/core/src/ptr/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 2d793d43e62e7..eaa421d187adf 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -690,18 +690,18 @@ where /// type or mutability, in particular if the code is refactored. #[inline(always)] #[must_use] -#[unstable(feature = "ptr_from_ref", issue = "999999")] // FIXME +#[unstable(feature = "ptr_from_ref", issue = "106116")] pub fn from_ref(r: &T) -> *const T { r } -/// Convert a mutble reference to a raw pointer. +/// Convert a mutable reference to a raw pointer. /// /// This is equivalent to `r as *mut T`, but is a bit safer since it will never silently change /// type or mutability, in particular if the code is refactored. #[inline(always)] #[must_use] -#[unstable(feature = "ptr_from_ref", issue = "999999")] // FIXME +#[unstable(feature = "ptr_from_ref", issue = "106116")] pub fn from_mut(r: &mut T) -> *mut T { r }