@@ -2727,6 +2727,24 @@ impl CompInfo {
2727
2727
. rust_features ( )
2728
2728
. ptr_metadata
2729
2729
{
2730
+ let flex_ref_inner = ctx. wrap_unsafe_ops ( quote ! {
2731
+ Self :: flex_ptr( self , len)
2732
+ } ) ;
2733
+ let flex_ref_mut_inner = ctx. wrap_unsafe_ops ( quote ! {
2734
+ Self :: flex_ptr_mut( self , len) . assume_init( )
2735
+ } ) ;
2736
+ let flex_ptr_inner = ctx. wrap_unsafe_ops ( quote ! {
2737
+ & * :: #prefix:: ptr:: from_raw_parts( ptr as * const ( ) , len)
2738
+ } ) ;
2739
+ let flex_ptr_mut_inner = ctx. wrap_unsafe_ops ( quote ! {
2740
+ // Initialize reference without ever exposing it, as its possibly uninitialized
2741
+ let mut uninit = :: #prefix:: mem:: MaybeUninit :: <& mut #dst_ty_for_impl>:: uninit( ) ;
2742
+ ( uninit. as_mut_ptr( ) as * mut * mut #dst_ty_for_impl)
2743
+ . write( :: #prefix:: ptr:: from_raw_parts_mut( ptr as * mut ( ) , len) ) ;
2744
+
2745
+ uninit
2746
+ } ) ;
2747
+
2730
2748
(
2731
2749
quote ! {
2732
2750
pub fn fixed( & self ) -> ( & #sized_ty_for_impl, usize ) {
@@ -2740,7 +2758,6 @@ impl CompInfo {
2740
2758
unsafe {
2741
2759
let ( ptr, len) = ( self as * mut Self ) . to_raw_parts( ) ;
2742
2760
( & mut * ( ptr as * mut #sized_ty_for_impl) , len)
2743
-
2744
2761
}
2745
2762
}
2746
2763
} ,
@@ -2750,23 +2767,23 @@ impl CompInfo {
2750
2767
/// SAFETY: Underlying storage is initialized up to at least `len` elements.
2751
2768
pub unsafe fn flex_ref( & self , len: usize ) -> & #dst_ty_for_impl {
2752
2769
// SAFETY: Reference is always valid as pointer. Caller is guaranteeing `len`.
2753
- unsafe { Self :: flex_ptr ( self , len ) }
2770
+ #flex_ref_inner
2754
2771
}
2755
2772
2756
2773
/// Convert a mutable sized prefix to an unsized structure with the given length.
2757
2774
///
2758
2775
/// SAFETY: Underlying storage is initialized up to at least `len` elements.
2759
- pub unsafe fn flex_mut_ref ( & mut self , len: usize ) -> & mut #dst_ty_for_impl {
2776
+ pub unsafe fn flex_ref_mut ( & mut self , len: usize ) -> & mut #dst_ty_for_impl {
2760
2777
// SAFETY: Reference is always valid as pointer. Caller is guaranteeing `len`.
2761
- unsafe { Self :: flex_ptr_mut ( self , len ) . assume_init ( ) }
2778
+ #flex_ref_mut_inner
2762
2779
}
2763
2780
2764
2781
/// Construct DST variant from a pointer and a size.
2765
2782
///
2766
2783
/// NOTE: lifetime of returned reference is not tied to any underlying storage.
2767
2784
/// SAFETY: `ptr` is valid. Underlying storage is fully initialized up to at least `len` elements.
2768
2785
pub unsafe fn flex_ptr<' unbounded>( ptr: * const Self , len: usize ) -> & ' unbounded #dst_ty_for_impl {
2769
- unsafe { & * :: #prefix :: ptr :: from_raw_parts ( ptr as * const ( ) , len ) }
2786
+ #flex_ptr_inner
2770
2787
}
2771
2788
2772
2789
/// Construct mutable DST variant from a pointer and a
@@ -2780,14 +2797,7 @@ impl CompInfo {
2780
2797
ptr: * mut Self ,
2781
2798
len: usize ,
2782
2799
) -> :: #prefix:: mem:: MaybeUninit <& ' unbounded mut #dst_ty_for_impl> {
2783
- unsafe {
2784
- // Initialize reference without ever exposing it, as its possibly uninitialized
2785
- let mut uninit = :: #prefix:: mem:: MaybeUninit :: <& mut #dst_ty_for_impl>:: uninit( ) ;
2786
- ( uninit. as_mut_ptr( ) as * mut * mut #dst_ty_for_impl)
2787
- . write( :: #prefix:: ptr:: from_raw_parts_mut( ptr as * mut ( ) , len) ) ;
2788
-
2789
- uninit
2790
- }
2800
+ #flex_ptr_mut_inner
2791
2801
}
2792
2802
} ,
2793
2803
)
0 commit comments