Skip to content

Commit cae7f30

Browse files
Move BorrowDatum interfaces to ptr::NonNull
1 parent cf6a8da commit cae7f30

File tree

4 files changed

+35
-26
lines changed

4 files changed

+35
-26
lines changed

pgrx/src/array.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ where
7777
let nulls =
7878
raw.nulls_bitptr().map(|p| unsafe { bitslice::from_raw_parts(p, nelems).unwrap() });
7979

80-
let data = raw.data_ptr();
80+
let data = unsafe { NonNull::new_unchecked(raw.data_ptr().cast_mut()) };
8181
let arr = self;
8282
let index = 0;
8383
let offset = 0;
@@ -164,10 +164,13 @@ trait BuiltinRepr {
164164

165165
unsafe impl<T: ?Sized> BorrowDatum for FlatArray<'_, T> {
166166
const PASS: layout::PassBy = layout::PassBy::Ref;
167-
unsafe fn point_from(ptr: *mut u8) -> *mut Self {
167+
unsafe fn point_from(ptr: ptr::NonNull<u8>) -> ptr::NonNull<Self> {
168168
unsafe {
169-
let len = varlena::varsize_any(ptr.cast()) - mem::size_of::<pg_sys::ArrayType>();
170-
ptr::slice_from_raw_parts_mut(ptr, len) as *mut Self
169+
let len =
170+
varlena::varsize_any(ptr.as_ptr().cast()) - mem::size_of::<pg_sys::ArrayType>();
171+
ptr::NonNull::new_unchecked(
172+
ptr::slice_from_raw_parts_mut(ptr.as_ptr(), len) as *mut Self
173+
)
171174
}
172175
}
173176
}
@@ -217,7 +220,7 @@ where
217220
T: ?Sized + BorrowDatum,
218221
{
219222
arr: &'arr FlatArray<'arr, T>,
220-
data: *const u8,
223+
data: ptr::NonNull<u8>,
221224
nulls: Option<&'arr BitSlice<u8>>,
222225
nelems: usize,
223226
index: usize,

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
}

pgrx/src/text.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,12 @@ impl Text {
115115

116116
unsafe impl BorrowDatum for Text {
117117
const PASS: PassBy = PassBy::Ref;
118-
unsafe fn point_from(ptr: *mut u8) -> *mut Self {
118+
unsafe fn point_from(ptr: ptr::NonNull<u8>) -> ptr::NonNull<Self> {
119119
unsafe {
120-
let len = varlena::varsize_any(ptr.cast());
121-
ptr::slice_from_raw_parts_mut(ptr, len) as *mut Text
120+
let len = varlena::varsize_any(ptr.as_ptr().cast());
121+
ptr::NonNull::new_unchecked(
122+
ptr::slice_from_raw_parts_mut(ptr.as_ptr(), len) as *mut Text
123+
)
122124
}
123125
}
124126
}

0 commit comments

Comments
 (0)