@@ -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,7 @@ 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 ) && !args . contains ( & PostgresTypeAttribute :: PgVarlenaInOutFuncs ) {
714
720
// assume the user wants us to implement the InOutFuncs
715
721
args. insert ( PostgresTypeAttribute :: Default ) ;
716
722
}
@@ -803,7 +809,34 @@ fn impl_postgres_type(ast: DeriveInput) -> proc_macro2::TokenStream {
803
809
} ) ;
804
810
}
805
811
806
- let sql_graph_entity_item = PostgresType :: from_derive_input ( ast) . unwrap ( ) ;
812
+ if args. contains ( & PostgresTypeAttribute :: SendReceiveFuncs ) {
813
+ stream. extend ( quote ! {
814
+ #[ doc( hidden) ]
815
+ #[ pg_extern( immutable, parallel_safe, strict) ]
816
+ pub fn #funcname_recv #generics( input: :: pgx:: Internal ) -> #name #generics {
817
+ let mut buffer0 = unsafe {
818
+ input
819
+ . get_mut:: <:: pgx:: pg_sys:: StringInfoData >( )
820
+ . expect( "Can't retrieve StringInfo pointer" )
821
+ } ;
822
+ let mut buffer = StringInfo :: from_pg( buffer0 as * mut _) . expect( "failed to construct StringInfo" ) ;
823
+ let slice = buffer. read( ..) . expect( "failure reading StringInfo" ) ;
824
+ :: pgx:: SendRecvFuncs :: recv( slice)
825
+ }
826
+
827
+ #[ doc( hidden) ]
828
+ #[ pg_extern( immutable, parallel_safe, strict) ]
829
+ pub fn #funcname_send #generics( input: #name #generics) -> Vec <u8 > {
830
+ :: pgx:: SendRecvFuncs :: send( & input)
831
+ }
832
+ } ) ;
833
+ }
834
+
835
+ let sql_graph_entity_item = PostgresType :: from_derive_input (
836
+ ast,
837
+ args. contains ( & PostgresTypeAttribute :: SendReceiveFuncs ) ,
838
+ )
839
+ . unwrap ( ) ;
807
840
sql_graph_entity_item. to_tokens ( & mut stream) ;
808
841
809
842
stream
@@ -895,6 +928,7 @@ fn impl_guc_enum(ast: DeriveInput) -> proc_macro2::TokenStream {
895
928
enum PostgresTypeAttribute {
896
929
InOutFuncs ,
897
930
PgVarlenaInOutFuncs ,
931
+ SendReceiveFuncs ,
898
932
Default ,
899
933
}
900
934
@@ -912,6 +946,9 @@ fn parse_postgres_type_args(attributes: &[Attribute]) -> HashSet<PostgresTypeAtt
912
946
"pgvarlena_inoutfuncs" => {
913
947
categorized_attributes. insert ( PostgresTypeAttribute :: PgVarlenaInOutFuncs ) ;
914
948
}
949
+ "sendrecvfuncs" => {
950
+ categorized_attributes. insert ( PostgresTypeAttribute :: SendReceiveFuncs ) ;
951
+ }
915
952
916
953
_ => {
917
954
// we can just ignore attributes we don't understand
0 commit comments