@@ -41,7 +41,7 @@ pub unsafe trait BorrowDatum {
41
41
///
42
42
/// Do not attempt to handle pass-by-value versus pass-by-ref in this fn's body!
43
43
/// 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 > ;
45
45
46
46
/// Cast a pointer to aligned varlena headers to this type
47
47
///
@@ -52,14 +52,14 @@ pub unsafe trait BorrowDatum {
52
52
/// # Safety
53
53
/// - This must be correctly invoked for the pointee type, as it may deref.
54
54
/// - 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 > {
56
56
debug_assert ! ( ptr. is_aligned( ) ) ;
57
57
unsafe { <Self as BorrowDatum >:: point_from ( ptr. cast ( ) ) }
58
58
}
59
59
60
60
/// 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 ( ) }
63
63
}
64
64
}
65
65
@@ -73,7 +73,7 @@ macro_rules! borrow_by_value {
73
73
PassBy :: Ref
74
74
} ;
75
75
76
- unsafe fn point_from( ptr: * mut u8 ) -> * mut Self {
76
+ unsafe fn point_from( ptr: ptr :: NonNull < u8 > ) -> ptr :: NonNull < Self > {
77
77
ptr. cast( )
78
78
}
79
79
}
@@ -89,14 +89,16 @@ borrow_by_value! {
89
89
unsafe impl BorrowDatum for ffi:: CStr {
90
90
const PASS : PassBy = PassBy :: Ref ;
91
91
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
+ }
96
98
}
97
99
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 ( ) ;
100
102
unsafe { ffi:: CStr :: from_ptr ( char_ptr) }
101
103
}
102
104
}
0 commit comments