Skip to content

Commit 861974c

Browse files
The crown jewel of this entire exercise
1 parent 450abf0 commit 861974c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

pgrx/src/callconv.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
#![deny(unsafe_op_in_unsafe_fn)]
1111
//! Helper implementations for returning sets and tables from `#[pg_extern]`-style functions
1212
13-
use crate::datum::Datum;
1413
use crate::datum::{
1514
AnyArray, AnyElement, AnyNumeric, Date, FromDatum, Inet, Internal, Interval, IntoDatum, Json,
1615
JsonB, Numeric, PgVarlena, Time, TimeWithTimeZone, Timestamp, TimestampWithTimeZone,
1716
UnboxDatum, Uuid,
1817
};
18+
use crate::datum::{BorrowDatum, Datum};
1919
use crate::datum::{Range, RangeSubType};
2020
use crate::heap_tuple::PgHeapTuple;
21+
use crate::layout::PassBy;
2122
use crate::nullable::Nullable;
2223
use crate::pg_sys;
2324
use crate::pgbox::*;
@@ -267,6 +268,20 @@ argue_from_datum! { 'fcx; Inet, Internal, Json, JsonB, Uuid, PgRelation }
267268
argue_from_datum! { 'fcx; pg_sys::BOX, pg_sys::ItemPointerData, pg_sys::Oid, pg_sys::Point }
268269
argue_from_datum! { 'fcx; &'fcx str, &'fcx CStr, &'fcx [u8] }
269270

271+
unsafe impl<'fcx, T> ArgAbi<'fcx> for &T
272+
where
273+
T: BorrowDatum,
274+
{
275+
unsafe fn unbox_arg_unchecked(arg: Arg<'_, 'fcx>) -> Self {
276+
let ptr: *mut u8 = match T::PASS {
277+
Some(PassBy::Ref) => arg.2.value.cast_mut_ptr(),
278+
Some(PassBy::Value) => ptr::addr_of!(arg.0.raw_args()[arg.1].value).cast_mut().cast(),
279+
_ => todo!(),
280+
};
281+
unsafe { &*T::point_from(ptr) }
282+
}
283+
}
284+
270285
/// How to return a value from Rust to Postgres
271286
///
272287
/// This bound is necessary to distinguish things which can be returned from a `#[pg_extern] fn`.

0 commit comments

Comments
 (0)