@@ -354,6 +354,7 @@ impl<'a> TypeResolver<'a> {
354
354
return Some ( "" ) ;
355
355
}
356
356
match full_path {
357
+ "bitcoin::secp256k1::key::PublicKey" if is_ref => Some ( "crate::c_types::PublicKey::from_rust(" ) ,
357
358
"bitcoin::secp256k1::key::PublicKey" => Some ( "crate::c_types::PublicKey::from_rust(&" ) ,
358
359
"bitcoin::blockdata::script::Script" => Some ( "crate::c_types::Script::from_slice(&c_" ) ,
359
360
"bitcoin::blockdata::transaction::Transaction" => Some ( "crate::c_types::Transaction::from_slice(&c_" ) ,
@@ -637,11 +638,11 @@ impl<'a> TypeResolver<'a> {
637
638
}
638
639
}
639
640
640
- fn print_to_c_conversion_inline_prefix_intern < W : std:: io:: Write > ( & self , w : & mut W , t : & syn:: Type , generics : Option < & GenericTypes > , is_ref : bool ) {
641
+ fn print_to_c_conversion_inline_prefix_intern < W : std:: io:: Write > ( & self , w : & mut W , t : & syn:: Type , generics : Option < & GenericTypes > , is_ref : bool , ptr_for_ref : bool ) {
641
642
match t {
642
643
syn:: Type :: Reference ( r) => {
643
644
if r. lifetime . is_some ( ) { unimplemented ! ( ) ; }
644
- self . print_to_c_conversion_inline_prefix_intern ( w, & * r. elem , generics, true ) ;
645
+ self . print_to_c_conversion_inline_prefix_intern ( w, & * r. elem , generics, true , ptr_for_ref ) ;
645
646
} ,
646
647
syn:: Type :: Path ( p) => {
647
648
if p. qself . is_some ( ) || p. path . leading_colon . is_some ( ) {
@@ -667,8 +668,12 @@ impl<'a> TypeResolver<'a> {
667
668
} else if let Some ( decl_type) = self . declared . get ( ident) {
668
669
match decl_type {
669
670
DeclType :: MirroredEnum => write ! ( w, "{}::from_ln(" , ident) . unwrap ( ) ,
670
- DeclType :: StructImported ( name) if is_ref => write ! ( w, "Box::into_raw(Box::new({} {{ inner: " , name) . unwrap ( ) ,
671
- DeclType :: StructImported ( name) if !is_ref => write ! ( w, "{} {{ inner: Box::into_raw(Box::new(" , name) . unwrap ( ) ,
671
+ DeclType :: StructImported ( name) if is_ref && ptr_for_ref =>
672
+ write ! ( w, "Box::into_raw(Box::new({} {{ inner: " , name) . unwrap ( ) ,
673
+ DeclType :: StructImported ( name) if is_ref =>
674
+ write ! ( w, "&{} {{ inner: " , name) . unwrap ( ) ,
675
+ DeclType :: StructImported ( name) if !is_ref =>
676
+ write ! ( w, "{} {{ inner: Box::into_raw(Box::new(" , name) . unwrap ( ) ,
672
677
_ => { } ,
673
678
}
674
679
} else { unimplemented ! ( ) ; }
@@ -697,14 +702,14 @@ impl<'a> TypeResolver<'a> {
697
702
_ => unimplemented ! ( ) ,
698
703
}
699
704
}
700
- pub fn print_to_c_conversion_inline_prefix < W : std:: io:: Write > ( & self , w : & mut W , t : & syn:: Type , generics : Option < & GenericTypes > ) {
701
- self . print_to_c_conversion_inline_prefix_intern ( w, t, generics, false ) ;
705
+ pub fn print_to_c_conversion_inline_prefix < W : std:: io:: Write > ( & self , w : & mut W , t : & syn:: Type , generics : Option < & GenericTypes > , ptr_for_ref : bool ) {
706
+ self . print_to_c_conversion_inline_prefix_intern ( w, t, generics, false , ptr_for_ref ) ;
702
707
}
703
- pub fn print_to_c_conversion_inline_suffix_intern < W : std:: io:: Write > ( & self , w : & mut W , t : & syn:: Type , generics : Option < & GenericTypes > , is_ref : bool ) {
708
+ pub fn print_to_c_conversion_inline_suffix_intern < W : std:: io:: Write > ( & self , w : & mut W , t : & syn:: Type , generics : Option < & GenericTypes > , is_ref : bool , ptr_for_ref : bool ) {
704
709
match t {
705
710
syn:: Type :: Reference ( r) => {
706
711
if r. lifetime . is_some ( ) { unimplemented ! ( ) ; }
707
- self . print_to_c_conversion_inline_suffix_intern ( w, & * r. elem , generics, true ) ;
712
+ self . print_to_c_conversion_inline_suffix_intern ( w, & * r. elem , generics, true , ptr_for_ref ) ;
708
713
} ,
709
714
syn:: Type :: Path ( p) => {
710
715
if p. qself . is_some ( ) || p. path . leading_colon . is_some ( ) {
@@ -730,7 +735,8 @@ impl<'a> TypeResolver<'a> {
730
735
} else if let Some ( decltype) = self . declared . get ( ident) {
731
736
match decltype {
732
737
DeclType :: MirroredEnum => write ! ( w, ")" ) . unwrap ( ) ,
733
- DeclType :: StructImported ( _) if is_ref => write ! ( w, " }}))" ) . unwrap ( ) ,
738
+ DeclType :: StructImported ( _) if is_ref && ptr_for_ref => write ! ( w, " }} ))" ) . unwrap ( ) ,
739
+ DeclType :: StructImported ( _) if is_ref => write ! ( w, " }}" ) . unwrap ( ) ,
734
740
DeclType :: StructImported ( _) if !is_ref => write ! ( w, ")) }}" ) . unwrap ( ) ,
735
741
_ => { } ,
736
742
}
@@ -752,8 +758,8 @@ impl<'a> TypeResolver<'a> {
752
758
_ => unimplemented ! ( ) ,
753
759
}
754
760
}
755
- pub fn print_to_c_conversion_inline_suffix < W : std:: io:: Write > ( & self , w : & mut W , t : & syn:: Type , generics : Option < & GenericTypes > ) {
756
- self . print_to_c_conversion_inline_suffix_intern ( w, t, generics, false ) ;
761
+ pub fn print_to_c_conversion_inline_suffix < W : std:: io:: Write > ( & self , w : & mut W , t : & syn:: Type , generics : Option < & GenericTypes > , ptr_for_ref : bool ) {
762
+ self . print_to_c_conversion_inline_suffix_intern ( w, t, generics, false , ptr_for_ref ) ;
757
763
}
758
764
pub fn print_to_c_conversion_new_var < W : std:: io:: Write > ( & self , w : & mut W , ident : & syn:: Ident , t : & syn:: Type , generics : Option < & GenericTypes > ) -> bool {
759
765
match t {
@@ -882,7 +888,6 @@ impl<'a> TypeResolver<'a> {
882
888
DeclType :: StructImported ( _) => { } ,
883
889
DeclType :: EnumIgnored => unimplemented ! ( ) ,
884
890
DeclType :: MirroredEnum => { } ,
885
- DeclType :: Trait ( _) if is_ref => write ! ( w, "unsafe {{ &*" ) . unwrap ( ) ,
886
891
DeclType :: Trait ( _) => { } ,
887
892
}
888
893
return ;
@@ -946,7 +951,6 @@ impl<'a> TypeResolver<'a> {
946
951
DeclType :: StructImported ( _) => { } ,
947
952
DeclType :: EnumIgnored => unimplemented ! ( ) ,
948
953
DeclType :: MirroredEnum => { } ,
949
- DeclType :: Trait ( _) if is_ref => write ! ( w, " }}" ) . unwrap ( ) ,
950
954
DeclType :: Trait ( _) => { } ,
951
955
}
952
956
return ;
@@ -989,7 +993,7 @@ impl<'a> TypeResolver<'a> {
989
993
self . print_from_c_conversion_suffix_intern ( w, t, generics, false ) ;
990
994
}
991
995
992
- fn print_c_ident_intern < W : std:: io:: Write > ( & self , w : & mut W , ident : & syn:: Ident , is_ref : bool , is_mut : bool ) -> bool {
996
+ fn print_c_ident_intern < W : std:: io:: Write > ( & self , w : & mut W , ident : & syn:: Ident , is_ref : bool , is_mut : bool , ptr_for_ref : bool ) -> bool {
993
997
let full_path = match self . maybe_resolve_path ( & syn:: Path :: from ( ident. clone ( ) ) ) {
994
998
Some ( path) => path, None => return false } ;
995
999
if let Some ( c_type) = self . c_type_from_path ( & full_path, is_ref) {
@@ -1001,24 +1005,31 @@ impl<'a> TypeResolver<'a> {
1001
1005
}
1002
1006
if let DeclType :: StructImported ( name) = decl_type {
1003
1007
if is_mut { unimplemented ! ( ) ; }
1004
- if is_ref {
1008
+ if is_ref && ptr_for_ref {
1005
1009
write ! ( w, "*const {}" , name) . unwrap ( ) ;
1010
+ } else if is_ref {
1011
+ write ! ( w, "&{}" , name) . unwrap ( ) ;
1006
1012
} else {
1007
1013
write ! ( w, "{}" , name) . unwrap ( ) ;
1008
1014
}
1009
1015
} else {
1016
+ if is_ref && ptr_for_ref {
1017
+ write ! ( w, "*{}" , if !is_mut { "const" } else { "" } ) . unwrap ( ) ;
1018
+ } else if is_ref {
1019
+ write ! ( w, "&" ) . unwrap ( ) ;
1020
+ }
1010
1021
if is_ref && is_mut {
1011
- write ! ( w, "* mut {}" , ident) . unwrap ( ) ;
1022
+ write ! ( w, "mut {}" , ident) . unwrap ( ) ;
1012
1023
} else if is_ref {
1013
- write ! ( w, "*const {}" , ident) . unwrap ( ) ;
1024
+ write ! ( w, " {}" , ident) . unwrap ( ) ;
1014
1025
} else {
1015
1026
write ! ( w, "{}" , ident) . unwrap ( ) ;
1016
1027
}
1017
1028
}
1018
1029
true
1019
1030
} else { false }
1020
1031
}
1021
- fn print_c_type_intern < W : std:: io:: Write > ( & self , generics : Option < & GenericTypes > , w : & mut W , t : & syn:: Type , is_ref : bool , is_mut : bool ) -> bool {
1032
+ fn print_c_type_intern < W : std:: io:: Write > ( & self , generics : Option < & GenericTypes > , w : & mut W , t : & syn:: Type , is_ref : bool , is_mut : bool , ptr_for_ref : bool ) -> bool {
1022
1033
match t {
1023
1034
syn:: Type :: Path ( p) => {
1024
1035
if p. qself . is_some ( ) || p. path . leading_colon . is_some ( ) {
@@ -1027,39 +1038,39 @@ impl<'a> TypeResolver<'a> {
1027
1038
if let Some ( gen_types) = generics {
1028
1039
if let Some ( resolved) = gen_types. maybe_resolve_path ( & p. path ) {
1029
1040
if self . is_known_container ( & resolved. 0 ) { return false ; }
1030
- return self . print_c_ident_intern ( w, & resolved. 1 , is_ref, is_mut) ;
1041
+ return self . print_c_ident_intern ( w, & resolved. 1 , is_ref, is_mut, ptr_for_ref ) ;
1031
1042
}
1032
1043
}
1033
1044
if let Some ( full_path) = self . maybe_resolve_path ( & p. path ) {
1034
1045
if self . is_known_container ( & full_path) {
1035
1046
if let syn:: PathArguments :: AngleBracketed ( args) = & p. path . segments . iter ( ) . next ( ) . unwrap ( ) . arguments {
1036
1047
if args. args . len ( ) != 1 { return false ; }
1037
1048
if let syn:: GenericArgument :: Type ( t) = args. args . iter ( ) . next ( ) . unwrap ( ) {
1038
- return self . print_c_type_intern ( generics, w, t, false , false ) ;
1049
+ return self . print_c_type_intern ( generics, w, t, false , false , ptr_for_ref ) ;
1039
1050
} else { return false ; }
1040
1051
} else { return false ; }
1041
1052
}
1042
1053
}
1043
1054
if p. path . leading_colon . is_some ( ) { return false ; }
1044
1055
if let Some ( ident) = single_ident_generic_path_to_ident ( & p. path ) {
1045
- self . print_c_ident_intern ( w, & ident, is_ref, is_mut)
1056
+ self . print_c_ident_intern ( w, & ident, is_ref, is_mut, ptr_for_ref )
1046
1057
} else { false }
1047
1058
} ,
1048
1059
syn:: Type :: Reference ( r) => {
1049
1060
if r. lifetime . is_some ( ) { return false ; }
1050
- self . print_c_type_intern ( generics, w, & * r. elem , true , r. mutability . is_some ( ) )
1061
+ self . print_c_type_intern ( generics, w, & * r. elem , true , r. mutability . is_some ( ) , ptr_for_ref )
1051
1062
} ,
1052
1063
syn:: Type :: Array ( a) => {
1053
1064
if is_ref && is_mut {
1054
1065
write ! ( w, "*mut [" ) . unwrap ( ) ;
1055
- if !self . print_c_type_intern ( generics, w, & a. elem , false , false ) { return false ; }
1066
+ if !self . print_c_type_intern ( generics, w, & a. elem , false , false , ptr_for_ref ) { return false ; }
1056
1067
} else if is_ref {
1057
1068
write ! ( w, "*const [" ) . unwrap ( ) ;
1058
- if !self . print_c_type_intern ( generics, w, & a. elem , false , false ) { return false ; }
1069
+ if !self . print_c_type_intern ( generics, w, & a. elem , false , false , ptr_for_ref ) { return false ; }
1059
1070
} else {
1060
1071
write ! ( w, "crate::c_types::ThirtyTwoBytes" ) . unwrap ( ) ;
1061
1072
let mut typecheck = Vec :: new ( ) ;
1062
- if !self . print_c_type_intern ( generics, & mut typecheck, & a. elem , false , false ) { return false ; }
1073
+ if !self . print_c_type_intern ( generics, & mut typecheck, & a. elem , false , false , ptr_for_ref ) { return false ; }
1063
1074
if typecheck[ ..] != [ 'u' as u8 , '8' as u8 ] { return false ; }
1064
1075
}
1065
1076
if let syn:: Expr :: Lit ( l) = & a. len {
@@ -1087,16 +1098,16 @@ impl<'a> TypeResolver<'a> {
1087
1098
_ => false ,
1088
1099
}
1089
1100
}
1090
- pub fn print_c_type < W : std:: io:: Write > ( & self , w : & mut W , t : & syn:: Type , generics : Option < & GenericTypes > ) {
1091
- assert ! ( self . print_c_type_intern( generics, w, t, false , false ) ) ;
1101
+ pub fn print_c_type < W : std:: io:: Write > ( & self , w : & mut W , t : & syn:: Type , generics : Option < & GenericTypes > , ptr_for_ref : bool ) {
1102
+ assert ! ( self . print_c_type_intern( generics, w, t, false , false , ptr_for_ref ) ) ;
1092
1103
}
1093
1104
pub fn understood_c_path ( & self , p : & syn:: Path ) -> bool {
1094
1105
if p. leading_colon . is_some ( ) { return false ; }
1095
1106
if let Some ( ident) = single_ident_generic_path_to_ident ( p) {
1096
- self . print_c_ident_intern ( & mut std:: io:: sink ( ) , ident, false , false )
1107
+ self . print_c_ident_intern ( & mut std:: io:: sink ( ) , ident, false , false , false )
1097
1108
} else { false }
1098
1109
}
1099
1110
pub fn understood_c_type ( & self , t : & syn:: Type , generics : Option < & GenericTypes > ) -> bool {
1100
- self . print_c_type_intern ( generics, & mut std:: io:: sink ( ) , t, false , false )
1111
+ self . print_c_type_intern ( generics, & mut std:: io:: sink ( ) , t, false , false , false )
1101
1112
}
1102
1113
}
0 commit comments