From a12b8bddd0c286e64ea521fb0a3c491d95de5e7b Mon Sep 17 00:00:00 2001 From: Smitty Date: Thu, 6 Oct 2022 18:20:40 -0400 Subject: [PATCH] Use seperate trait function when OID needed --- pgx/src/datum/anyarray.rs | 9 +++++++++ pgx/src/datum/anyelement.rs | 9 +++++++++ pgx/src/datum/from.rs | 11 +++++++++++ pgx/src/fcinfo.rs | 18 ++++++++---------- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/pgx/src/datum/anyarray.rs b/pgx/src/datum/anyarray.rs index 758ecf275f..f5c64b61d2 100644 --- a/pgx/src/datum/anyarray.rs +++ b/pgx/src/datum/anyarray.rs @@ -41,6 +41,15 @@ impl FromDatum for AnyArray { datum: pg_sys::Datum, is_null: bool, typoid: pg_sys::Oid, + ) -> Option { + FromDatum::from_datum_with_typid(datum, is_null, typoid) + } + + #[inline] + unsafe fn from_datum_with_typid( + datum: pg_sys::Datum, + is_null: bool, + typoid: pg_sys::Oid, ) -> Option { if is_null { None diff --git a/pgx/src/datum/anyelement.rs b/pgx/src/datum/anyelement.rs index 05cbcfc2dc..a91315aed5 100644 --- a/pgx/src/datum/anyelement.rs +++ b/pgx/src/datum/anyelement.rs @@ -41,6 +41,15 @@ impl FromDatum for AnyElement { datum: pg_sys::Datum, is_null: bool, typoid: pg_sys::Oid, + ) -> Option { + FromDatum::from_datum_with_typid(datum, is_null, typoid) + } + + #[inline] + unsafe fn from_datum_with_typid( + datum: pg_sys::Datum, + is_null: bool, + typoid: pg_sys::Oid, ) -> Option { if is_null { None diff --git a/pgx/src/datum/from.rs b/pgx/src/datum/from.rs index 9cc09df448..5b67ec20c3 100644 --- a/pgx/src/datum/from.rs +++ b/pgx/src/datum/from.rs @@ -57,6 +57,17 @@ pub trait FromDatum { where Self: Sized; + /// Like `from_datum` but the typoid must have a value. + /// + /// ## Safety + /// + /// Same caveats as `FromDatum::from_datum(...)`. + unsafe fn from_datum_with_typid(datum: pg_sys::Datum, is_null: bool, typoid: pg_sys::Oid) -> Option + where + Self: Sized { + FromDatum::from_datum(datum, is_null, typoid) + } + /// Default implementation switched to the specified memory context and then simply calls /// `FromDatum::from_datum(...)` from within that context. /// diff --git a/pgx/src/fcinfo.rs b/pgx/src/fcinfo.rs index dd1662a0d0..4eafb86da0 100644 --- a/pgx/src/fcinfo.rs +++ b/pgx/src/fcinfo.rs @@ -95,12 +95,11 @@ mod pg_10_11 { let datum = unsafe { fcinfo.as_ref() }.unwrap().arg[num]; let isnull = pg_arg_is_null(fcinfo, num); unsafe { - let typid = if T::GET_TYPOID { - super::pg_getarg_type(fcinfo, num) + if T::GET_TYPOID { + T::from_datum_with_typid(datum.value, isnull, super::pg_getarg_type(fcinfo, num)) } else { - pg_sys::InvalidOid - }; - T::from_datum(datum, isnull, typid) + T::from_datum(datum.value, isnull, pg_sys::InvalidOid) + } } } @@ -138,12 +137,11 @@ mod pg_12_13_14 { pub fn pg_getarg(fcinfo: pg_sys::FunctionCallInfo, num: usize) -> Option { let datum = get_nullable_datum(fcinfo, num); unsafe { - let typid = if T::GET_TYPOID { - super::pg_getarg_type(fcinfo, num) + if T::GET_TYPOID { + T::from_datum_with_typid(datum.value, datum.isnull, super::pg_getarg_type(fcinfo, num)) } else { - pg_sys::InvalidOid - }; - T::from_datum(datum.value, datum.isnull, typid) + T::from_datum(datum.value, datum.isnull, pg_sys::InvalidOid) + } } }