@@ -41,7 +41,7 @@ pub unsafe trait BorrowDatum {
4141 ///
4242 /// Do not attempt to handle pass-by-value versus pass-by-ref in this fn's body!
4343 /// A caller may be in a context where all types are handled by-reference, for instance.
44- unsafe fn point_from ( ptr : * mut u8 ) -> * mut Self ;
44+ unsafe fn point_from ( ptr : ptr :: NonNull < u8 > ) -> ptr :: NonNull < Self > ;
4545
4646 /// Cast a pointer to aligned varlena headers to this type
4747 ///
@@ -52,14 +52,14 @@ pub unsafe trait BorrowDatum {
5252 /// # Safety
5353 /// - This must be correctly invoked for the pointee type, as it may deref.
5454 /// - This must be 4-byte aligned!
55- unsafe fn point_from_align4 ( ptr : * mut u32 ) -> * mut Self {
55+ unsafe fn point_from_align4 ( ptr : ptr :: NonNull < u32 > ) -> ptr :: NonNull < Self > {
5656 debug_assert ! ( ptr. is_aligned( ) ) ;
5757 unsafe { <Self as BorrowDatum >:: point_from ( ptr. cast ( ) ) }
5858 }
5959
6060 /// Optimization for borrowing the referent
61- unsafe fn borrow_unchecked < ' dat > ( ptr : * const u8 ) -> & ' dat Self {
62- unsafe { & * Self :: point_from ( ptr. cast_mut ( ) ) }
61+ unsafe fn borrow_unchecked < ' dat > ( ptr : ptr :: NonNull < u8 > ) -> & ' dat Self {
62+ unsafe { Self :: point_from ( ptr) . as_ref ( ) }
6363 }
6464}
6565
@@ -73,7 +73,7 @@ macro_rules! borrow_by_value {
7373 PassBy :: Ref
7474 } ;
7575
76- unsafe fn point_from( ptr: * mut u8 ) -> * mut Self {
76+ unsafe fn point_from( ptr: ptr :: NonNull < u8 > ) -> ptr :: NonNull < Self > {
7777 ptr. cast( )
7878 }
7979 }
@@ -89,14 +89,16 @@ borrow_by_value! {
8989unsafe impl BorrowDatum for ffi:: CStr {
9090 const PASS : PassBy = PassBy :: Ref ;
9191
92- unsafe fn point_from ( ptr : * mut u8 ) -> * mut Self {
93- let char_ptr: * mut ffi:: c_char = ptr. cast ( ) ;
94- let len = unsafe { ffi:: CStr :: from_ptr ( char_ptr) . to_bytes_with_nul ( ) . len ( ) } ;
95- ptr:: slice_from_raw_parts_mut ( char_ptr, len) as * mut Self
92+ unsafe fn point_from ( ptr : ptr:: NonNull < u8 > ) -> ptr:: NonNull < Self > {
93+ let char_ptr: * mut ffi:: c_char = ptr. as_ptr ( ) . cast ( ) ;
94+ unsafe {
95+ let len = ffi:: CStr :: from_ptr ( char_ptr) . to_bytes_with_nul ( ) . len ( ) ;
96+ ptr:: NonNull :: new_unchecked ( ptr:: slice_from_raw_parts_mut ( char_ptr, len) as * mut Self )
97+ }
9698 }
9799
98- unsafe fn borrow_unchecked < ' dat > ( ptr : * const u8 ) -> & ' dat Self {
99- let char_ptr: * const ffi:: c_char = ptr. cast ( ) ;
100+ unsafe fn borrow_unchecked < ' dat > ( ptr : ptr :: NonNull < u8 > ) -> & ' dat Self {
101+ let char_ptr: * const ffi:: c_char = ptr. as_ptr ( ) . cast ( ) ;
100102 unsafe { ffi:: CStr :: from_ptr ( char_ptr) }
101103 }
102104}
0 commit comments