@@ -681,9 +681,13 @@ Optionally accepts the following attributes:
681
681
682
682
* `inoutfuncs(some_in_fn, some_out_fn)`: Define custom in/out functions for the type.
683
683
* `pgvarlena_inoutfuncs(some_in_fn, some_out_fn)`: Define custom in/out functions for the `PgVarlena` of this type.
684
+ * `sendrecvfuncs`: Define binary send/receive functions for the type.
684
685
* `sql`: Same arguments as [`#[pgx(sql = ..)]`](macro@pgx).
685
686
*/
686
- #[ proc_macro_derive( PostgresType , attributes( inoutfuncs, pgvarlena_inoutfuncs, requires, pgx) ) ]
687
+ #[ proc_macro_derive(
688
+ PostgresType ,
689
+ attributes( inoutfuncs, pgvarlena_inoutfuncs, sendrecvfuncs, requires, pgx)
690
+ ) ]
687
691
pub fn postgres_type ( input : TokenStream ) -> TokenStream {
688
692
let ast = parse_macro_input ! ( input as syn:: DeriveInput ) ;
689
693
@@ -696,6 +700,8 @@ fn impl_postgres_type(ast: DeriveInput) -> proc_macro2::TokenStream {
696
700
let has_lifetimes = generics. lifetimes ( ) . next ( ) ;
697
701
let funcname_in = Ident :: new ( & format ! ( "{}_in" , name) . to_lowercase ( ) , name. span ( ) ) ;
698
702
let funcname_out = Ident :: new ( & format ! ( "{}_out" , name) . to_lowercase ( ) , name. span ( ) ) ;
703
+ let funcname_send = Ident :: new ( & format ! ( "{}_send" , name) . to_lowercase ( ) , name. span ( ) ) ;
704
+ let funcname_recv = Ident :: new ( & format ! ( "{}_recv" , name) . to_lowercase ( ) , name. span ( ) ) ;
699
705
let mut args = parse_postgres_type_args ( & ast. attrs ) ;
700
706
let mut stream = proc_macro2:: TokenStream :: new ( ) ;
701
707
@@ -710,7 +716,9 @@ fn impl_postgres_type(ast: DeriveInput) -> proc_macro2::TokenStream {
710
716
_ => panic ! ( "#[derive(PostgresType)] can only be applied to structs or enums" ) ,
711
717
}
712
718
713
- if args. is_empty ( ) {
719
+ if !args. contains ( & PostgresTypeAttribute :: InOutFuncs )
720
+ && !args. contains ( & PostgresTypeAttribute :: PgVarlenaInOutFuncs )
721
+ {
714
722
// assume the user wants us to implement the InOutFuncs
715
723
args. insert ( PostgresTypeAttribute :: Default ) ;
716
724
}
@@ -803,7 +811,34 @@ fn impl_postgres_type(ast: DeriveInput) -> proc_macro2::TokenStream {
803
811
} ) ;
804
812
}
805
813
806
- let sql_graph_entity_item = PostgresType :: from_derive_input ( ast) . unwrap ( ) ;
814
+ if args. contains ( & PostgresTypeAttribute :: SendReceiveFuncs ) {
815
+ stream. extend ( quote ! {
816
+ #[ doc( hidden) ]
817
+ #[ pg_extern( immutable, parallel_safe, strict) ]
818
+ pub fn #funcname_recv #generics( input: :: pgx:: Internal ) -> #name #generics {
819
+ let mut buffer0 = unsafe {
820
+ input
821
+ . get_mut:: <:: pgx:: pg_sys:: StringInfoData >( )
822
+ . expect( "Can't retrieve StringInfo pointer" )
823
+ } ;
824
+ let mut buffer = StringInfo :: from_pg( buffer0 as * mut _) . expect( "failed to construct StringInfo" ) ;
825
+ let slice = buffer. read( ..) . expect( "failure reading StringInfo" ) ;
826
+ :: pgx:: SendRecvFuncs :: recv( slice)
827
+ }
828
+
829
+ #[ doc( hidden) ]
830
+ #[ pg_extern( immutable, parallel_safe, strict) ]
831
+ pub fn #funcname_send #generics( input: #name #generics) -> Vec <u8 > {
832
+ :: pgx:: SendRecvFuncs :: send( & input)
833
+ }
834
+ } ) ;
835
+ }
836
+
837
+ let sql_graph_entity_item = PostgresType :: from_derive_input (
838
+ ast,
839
+ args. contains ( & PostgresTypeAttribute :: SendReceiveFuncs ) ,
840
+ )
841
+ . unwrap ( ) ;
807
842
sql_graph_entity_item. to_tokens ( & mut stream) ;
808
843
809
844
stream
@@ -895,6 +930,7 @@ fn impl_guc_enum(ast: DeriveInput) -> proc_macro2::TokenStream {
895
930
enum PostgresTypeAttribute {
896
931
InOutFuncs ,
897
932
PgVarlenaInOutFuncs ,
933
+ SendReceiveFuncs ,
898
934
Default ,
899
935
}
900
936
@@ -912,6 +948,9 @@ fn parse_postgres_type_args(attributes: &[Attribute]) -> HashSet<PostgresTypeAtt
912
948
"pgvarlena_inoutfuncs" => {
913
949
categorized_attributes. insert ( PostgresTypeAttribute :: PgVarlenaInOutFuncs ) ;
914
950
}
951
+ "sendrecvfuncs" => {
952
+ categorized_attributes. insert ( PostgresTypeAttribute :: SendReceiveFuncs ) ;
953
+ }
915
954
916
955
_ => {
917
956
// we can just ignore attributes we don't understand
0 commit comments