@@ -36,16 +36,48 @@ pub fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata {
3636 unsafe { PtrRepr { const_ptr : ptr } . components . metadata }
3737}
3838
39+ /// Forms a raw pointer from a data address and metadata.
40+ #[ unstable( feature = "ptr_metadata" , issue = /* FIXME */ "none" ) ]
41+ #[ rustc_const_unstable( feature = "ptr_metadata" , issue = /* FIXME */ "none" ) ]
42+ #[ inline]
43+ pub const fn from_raw_parts < T : ?Sized > (
44+ data_address : * const ( ) ,
45+ metadata : <T as Pointee >:: Metadata ,
46+ ) -> * const T {
47+ // SAFETY: Accessing the value from the `PtrRepr` union is safe since *const T
48+ // and PtrComponents<T> have the same memory layouts. Only std can make this
49+ // guarantee.
50+ unsafe { PtrRepr { components : PtrComponents { data_address, metadata } } . const_ptr }
51+ }
52+
53+ /// Performs the same functionality as [`from_raw_parts`], except that a
54+ /// raw `*mut` pointer is returned, as opposed to a raw `*const` pointer.
55+ ///
56+ /// See the documentation of [`from_raw_parts`] for more details.
57+ #[ unstable( feature = "ptr_metadata" , issue = /* FIXME */ "none" ) ]
58+ #[ rustc_const_unstable( feature = "ptr_metadata" , issue = /* FIXME */ "none" ) ]
59+ #[ inline]
60+ pub const fn from_raw_parts_mut < T : ?Sized > (
61+ data_address : * mut ( ) ,
62+ metadata : <T as Pointee >:: Metadata ,
63+ ) -> * mut T {
64+ // SAFETY: Accessing the value from the `PtrRepr` union is safe since *const T
65+ // and PtrComponents<T> have the same memory layouts. Only std can make this
66+ // guarantee.
67+ unsafe { PtrRepr { components : PtrComponents { data_address, metadata } } . mut_ptr }
68+ }
69+
3970#[ repr( C ) ]
40- union PtrRepr < T : ?Sized > {
41- const_ptr : * const T ,
42- components : PtrComponents < T > ,
71+ pub ( crate ) union PtrRepr < T : ?Sized > {
72+ pub ( crate ) const_ptr : * const T ,
73+ pub ( crate ) mut_ptr : * mut T ,
74+ pub ( crate ) components : PtrComponents < T > ,
4375}
4476
4577#[ repr( C ) ]
46- struct PtrComponents < T : ?Sized > {
47- data_address : usize ,
48- metadata : <T as Pointee >:: Metadata ,
78+ pub ( crate ) struct PtrComponents < T : ?Sized > {
79+ pub ( crate ) data_address : * const ( ) ,
80+ pub ( crate ) metadata : <T as Pointee >:: Metadata ,
4981}
5082
5183// Manual impl needed to avoid `T: Copy` bound.
0 commit comments