Skip to content

Commit 7e4f466

Browse files
committed
Auto merge of rust-lang#104977 - RalfJung:ptr-from-ref, r=dtolnay
add ptr::from_{ref,mut} We have methods to avoid almost all `as` casts around raw pointer handling, except for the initial cast from reference to raw pointer. These new methods close that gap. (I also moved `null_mut` next to `null` to keep the file consistently organized.) r? libs-api Tracking issue: rust-lang#106116
2 parents f5c3dfd + 15f72dd commit 7e4f466

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

library/core/src/ptr/mod.rs

+37-15
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,27 @@ pub const fn null<T: ?Sized + Thin>() -> *const T {
516516
from_raw_parts(invalid(0), ())
517517
}
518518

519+
/// Creates a null mutable raw pointer.
520+
///
521+
/// # Examples
522+
///
523+
/// ```
524+
/// use std::ptr;
525+
///
526+
/// let p: *mut i32 = ptr::null_mut();
527+
/// assert!(p.is_null());
528+
/// ```
529+
#[inline(always)]
530+
#[must_use]
531+
#[stable(feature = "rust1", since = "1.0.0")]
532+
#[rustc_promotable]
533+
#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
534+
#[rustc_allow_const_fn_unstable(ptr_metadata)]
535+
#[rustc_diagnostic_item = "ptr_null_mut"]
536+
pub const fn null_mut<T: ?Sized + Thin>() -> *mut T {
537+
from_raw_parts_mut(invalid_mut(0), ())
538+
}
539+
519540
/// Creates an invalid pointer with the given address.
520541
///
521542
/// This is different from `addr as *const T`, which creates a pointer that picks up a previously
@@ -663,25 +684,26 @@ where
663684
addr as *mut T
664685
}
665686

666-
/// Creates a null mutable raw pointer.
687+
/// Convert a reference to a raw pointer.
667688
///
668-
/// # Examples
669-
///
670-
/// ```
671-
/// use std::ptr;
689+
/// This is equivalent to `r as *const T`, but is a bit safer since it will never silently change
690+
/// type or mutability, in particular if the code is refactored.
691+
#[inline(always)]
692+
#[must_use]
693+
#[unstable(feature = "ptr_from_ref", issue = "106116")]
694+
pub fn from_ref<T: ?Sized>(r: &T) -> *const T {
695+
r
696+
}
697+
698+
/// Convert a mutable reference to a raw pointer.
672699
///
673-
/// let p: *mut i32 = ptr::null_mut();
674-
/// assert!(p.is_null());
675-
/// ```
700+
/// This is equivalent to `r as *mut T`, but is a bit safer since it will never silently change
701+
/// type or mutability, in particular if the code is refactored.
676702
#[inline(always)]
677703
#[must_use]
678-
#[stable(feature = "rust1", since = "1.0.0")]
679-
#[rustc_promotable]
680-
#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
681-
#[rustc_allow_const_fn_unstable(ptr_metadata)]
682-
#[rustc_diagnostic_item = "ptr_null_mut"]
683-
pub const fn null_mut<T: ?Sized + Thin>() -> *mut T {
684-
from_raw_parts_mut(invalid_mut(0), ())
704+
#[unstable(feature = "ptr_from_ref", issue = "106116")]
705+
pub fn from_mut<T: ?Sized>(r: &mut T) -> *mut T {
706+
r
685707
}
686708

687709
/// Forms a raw slice from a pointer and a length.

0 commit comments

Comments
 (0)