Skip to content

Commit b3094c0

Browse files
Move BorrowDatum interfaces to ptr::NonNull
1 parent 751c33e commit b3094c0

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

pgrx/src/callconv.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,20 @@ where
277277
PassBy::Ref => arg.2.value.cast_mut_ptr(),
278278
PassBy::Value => ptr::addr_of!(arg.0.raw_args()[arg.1].value).cast_mut().cast(),
279279
};
280-
unsafe { T::borrow_unchecked(ptr) }
280+
unsafe {
281+
let ptr = ptr::NonNull::new_unchecked(ptr);
282+
T::borrow_unchecked(ptr)
283+
}
281284
}
282285

283286
unsafe fn unbox_nullable_arg(arg: Arg<'_, 'fcx>) -> Nullable<Self> {
284-
let ptr: *mut u8 = match T::PASS {
287+
let ptr: Option<ptr::NonNull<u8>> = NonNull::new(match T::PASS {
285288
PassBy::Ref => arg.2.value.cast_mut_ptr(),
286289
PassBy::Value => ptr::addr_of!(arg.0.raw_args()[arg.1].value).cast_mut().cast(),
287-
};
288-
if arg.is_null() || ptr.is_null() {
289-
Nullable::Null
290-
} else {
291-
unsafe { Nullable::Valid(T::borrow_unchecked(ptr)) }
290+
});
291+
match (arg.is_null(), ptr) {
292+
(true, _) | (false, None) => Nullable::Null,
293+
(false, Some(ptr)) => unsafe { Nullable::Valid(T::borrow_unchecked(ptr)) },
292294
}
293295
}
294296
}

pgrx/src/datum/borrow.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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! {
8989
unsafe 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

Comments
 (0)