Skip to content

Commit

Permalink
Refactor BoxRet to be easier to impl before showing it (#1776)
Browse files Browse the repository at this point in the history
People need to implement some of the code in this module, but I want to
make sure it is easier to actually do so. To that end, refactor BoxRet
to have a more user-friendly interface, ish, and then expose it.

While doing so, adjust some safety requirements here and there.
  • Loading branch information
workingjubilee authored Jul 15, 2024
1 parent 8cf8d04 commit c862427
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 71 deletions.
4 changes: 2 additions & 2 deletions pgrx-examples/custom_types/src/hexint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ where
}

unsafe impl BoxRet for HexInt {
unsafe fn box_in_fcinfo(self, _fcinfo: pg_sys::FunctionCallInfo) -> pg_sys::Datum {
Datum::from(self.value)
unsafe fn box_into<'fcx>(self, fcinfo: &mut pgrx::callconv::FcInfo<'fcx>) -> pgrx::Datum<'fcx> {
unsafe { fcinfo.return_raw_datum(Datum::from(self.value)) }
}
}

Expand Down
13 changes: 8 additions & 5 deletions pgrx-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,11 @@ fn impl_postgres_enum(ast: DeriveInput) -> syn::Result<proc_macro2::TokenStream>
}

unsafe impl ::pgrx::callconv::BoxRet for #enum_ident {
unsafe fn box_in_fcinfo(self, fcinfo: ::pgrx::pg_sys::FunctionCallInfo) -> ::pgrx::pg_sys::Datum {
::pgrx::datum::IntoDatum::into_datum(self).unwrap()
unsafe fn box_into<'fcx>(self, fcinfo: &mut ::pgrx::callconv::FcInfo<'fcx>) -> ::pgrx::datum::Datum<'fcx> {
match ::pgrx::datum::IntoDatum::into_datum(self) {
None => fcinfo.return_null(),
Some(datum) => unsafe { fcinfo.return_raw_datum(datum) },
}
}
}
});
Expand Down Expand Up @@ -859,10 +862,10 @@ fn impl_postgres_type(ast: DeriveInput) -> syn::Result<proc_macro2::TokenStream>
}

unsafe impl #generics ::pgrx::callconv::BoxRet for #name #generics {
unsafe fn box_in_fcinfo(self, fcinfo: ::pgrx::pg_sys::FunctionCallInfo) -> ::pgrx::pg_sys::Datum {
unsafe fn box_into<'fcx>(self, fcinfo: &mut ::pgrx::callconv::FcInfo<'fcx>) -> ::pgrx::datum::Datum<'fcx> {
match ::pgrx::datum::IntoDatum::into_datum(self) {
None => ::pgrx::fcinfo::pg_return_null(fcinfo),
Some(datum) => datum,
None => fcinfo.return_null(),
Some(datum) => unsafe { fcinfo.return_raw_datum(datum) },
}
}
}
Expand Down
Loading

0 comments on commit c862427

Please sign in to comment.