@@ -276,6 +276,7 @@ impl<'a> TypeResolver<'a> {
276
276
return Some ( "" ) ;
277
277
}
278
278
match full_path {
279
+ "bitcoin::secp256k1::key::PublicKey" if is_ref => Some ( "&" ) ,
279
280
"bitcoin::secp256k1::key::PublicKey" => Some ( "" ) ,
280
281
"bitcoin::secp256k1::key::SecretKey" if is_ref => unimplemented ! ( ) ,
281
282
"bitcoin::secp256k1::key::SecretKey" => Some ( "" ) ,
@@ -340,7 +341,6 @@ impl<'a> TypeResolver<'a> {
340
341
return None ;
341
342
}
342
343
match full_path {
343
- "bitcoin::blockdata::script::Script" => Some ( ( "::bitcoin::consensus::encode::serialize(" , ")" ) ) ,
344
344
"bitcoin::blockdata::transaction::Transaction" => Some ( ( "::bitcoin::consensus::encode::serialize(" , ")" ) ) ,
345
345
"bitcoin::hash_types::Txid" => None ,
346
346
@@ -358,8 +358,10 @@ impl<'a> TypeResolver<'a> {
358
358
match full_path {
359
359
"bitcoin::secp256k1::key::PublicKey" if is_ref => Some ( "crate::c_types::PublicKey::from_rust(" ) ,
360
360
"bitcoin::secp256k1::key::PublicKey" => Some ( "crate::c_types::PublicKey::from_rust(&" ) ,
361
- "bitcoin::blockdata::script::Script" => Some ( "crate::c_types::Script::from_slice(&c_" ) ,
362
- "bitcoin::blockdata::transaction::Transaction" => Some ( "crate::c_types::Transaction::from_slice(&c_" ) ,
361
+ "bitcoin::secp256k1::key::SecretKey" if is_ref => unimplemented ! ( ) ,
362
+ "bitcoin::secp256k1::key::SecretKey" if !is_ref => Some ( "crate::c_types::SecretKey::from_rust(" ) ,
363
+ "bitcoin::blockdata::script::Script" if is_ref => Some ( "crate::c_types::Script::from_slice(&c_" ) ,
364
+ "bitcoin::blockdata::transaction::Transaction" if is_ref => Some ( "crate::c_types::Transaction::from_slice(&c_" ) ,
363
365
"bitcoin::hash_types::Txid" => Some ( "" ) ,
364
366
365
367
// Override the default since Records contain an fmt with a lifetime:
@@ -384,6 +386,7 @@ impl<'a> TypeResolver<'a> {
384
386
}
385
387
match full_path {
386
388
"bitcoin::secp256k1::key::PublicKey" => Some ( ")" ) ,
389
+ "bitcoin::secp256k1::key::SecretKey" => Some ( ")" ) ,
387
390
"bitcoin::blockdata::script::Script" => Some ( ")" ) ,
388
391
"bitcoin::blockdata::transaction::Transaction" => Some ( ")" ) ,
389
392
"bitcoin::hash_types::Txid" => Some ( ".into_inner()" ) ,
@@ -467,24 +470,24 @@ impl<'a> TypeResolver<'a> {
467
470
self . declared . get ( ident)
468
471
}
469
472
473
+ pub fn maybe_resolve_ident ( & self , id : & syn:: Ident ) -> Option < String > {
474
+ if let Some ( imp) = self . imports . get ( id) {
475
+ Some ( imp. clone ( ) )
476
+ } else if self . declared . get ( id) . is_some ( ) {
477
+ Some ( self . module_path . to_string ( ) + "::" + & format ! ( "{}" , id) )
478
+ } else { None }
479
+ }
480
+
470
481
pub fn maybe_resolve_path ( & self , p : & syn:: Path ) -> Option < String > {
471
482
if p. leading_colon . is_some ( ) {
472
483
//format!("{}", p.segments);
473
484
return None ;
474
485
} else if let Some ( id) = p. get_ident ( ) {
475
- if let Some ( imp) = self . imports . get ( id) {
476
- Some ( imp. clone ( ) )
477
- } else if self . declared . get ( id) . is_some ( ) {
478
- Some ( self . module_path . to_string ( ) + "::" + & format ! ( "{}" , id) )
479
- } else { None }
486
+ self . maybe_resolve_ident ( id)
480
487
} else {
481
488
if p. segments . len ( ) == 1 {
482
489
let seg = p. segments . iter ( ) . next ( ) . unwrap ( ) ;
483
- if let Some ( imp) = self . imports . get ( & seg. ident ) {
484
- return Some ( imp. clone ( ) ) ;
485
- } else if self . declared . get ( & seg. ident ) . is_some ( ) {
486
- return Some ( self . module_path . to_string ( ) + "::" + & format ! ( "{}" , seg. ident) ) ;
487
- } else { return None ; }
490
+ return self . maybe_resolve_ident ( & seg. ident ) ;
488
491
}
489
492
let mut seg_iter = p. segments . iter ( ) ;
490
493
let first_seg = seg_iter. next ( ) . unwrap ( ) ;
@@ -845,17 +848,18 @@ impl<'a> TypeResolver<'a> {
845
848
} else { unimplemented ! ( ) ; }
846
849
} else { unimplemented ! ( ) ; }
847
850
true
848
- } else {
849
- let ty_ident = single_ident_generic_path_to_ident ( & p. path ) . unwrap ( ) ;
850
- if let Some ( full_path) = self . imports . get ( ty_ident) {
851
- if let Some ( rust_type) = self . from_c_conversion_new_var_from_path_prefix ( & full_path) {
851
+ } else if self . is_primitive ( & resolved_path) {
852
+ false
853
+ } else if let Some ( ty_ident) = single_ident_generic_path_to_ident ( & p. path ) {
854
+ if let Some ( rust_type) = self . from_c_conversion_new_var_from_path_prefix ( & resolved_path) {
855
+ if let Some ( _) = self . imports . get ( ty_ident) {
852
856
write ! ( w, "let rust_{} = {}{});" , ident, rust_type, ident) . unwrap ( ) ;
853
857
true
854
858
} else { false }
855
859
} else if self . declared . get ( ty_ident) . is_some ( ) {
856
860
false
857
- } else { unimplemented ! ( ) ; }
858
- }
861
+ } else { false }
862
+ } else { false }
859
863
} ,
860
864
syn:: Type :: Array ( _) => {
861
865
// We assume all arrays contain only primitive types.
@@ -901,17 +905,22 @@ impl<'a> TypeResolver<'a> {
901
905
if self . is_known_container ( & resolved_path) {
902
906
write ! ( w, "rust_" ) . unwrap ( ) ;
903
907
} else {
904
- let ident = single_ident_generic_path_to_ident ( & p. path ) . unwrap ( ) ;
905
- if let Some ( full_path) = self . imports . get ( ident) {
906
- if let Some ( c_type) = self . from_c_conversion_prefix_from_path ( & full_path, is_ref) {
907
- write ! ( w, "{}" , c_type) . unwrap ( ) ;
908
+ if let Some ( c_type) = self . from_c_conversion_prefix_from_path ( & resolved_path, is_ref) {
909
+ write ! ( w, "{}" , c_type) . unwrap ( ) ;
910
+ } else if let Some ( ident) = single_ident_generic_path_to_ident ( & p. path ) {
911
+ if let Some ( decl_type) = self . declared . get ( ident) {
912
+ match decl_type {
913
+ DeclType :: StructImported ( _) if !is_ref => write ! ( w, "*unsafe {{ Box::from_raw(" ) . unwrap ( ) ,
914
+ DeclType :: StructImported ( _) => { } ,
915
+ DeclType :: MirroredEnum => { } ,
916
+ _ => unimplemented ! ( ) ,
917
+ }
908
918
} else { unimplemented ! ( ) ; }
909
- } else if let Some ( decl_type) = self . declared . get ( ident) {
910
- match decl_type {
911
- DeclType :: StructImported ( _) if !is_ref => write ! ( w, "*unsafe {{ Box::from_raw(" ) . unwrap ( ) ,
912
- DeclType :: StructImported ( _) => { } ,
913
- DeclType :: MirroredEnum => { } ,
914
- _ => unimplemented ! ( ) ,
919
+ } else if self . crate_types . structs . get ( & resolved_path) . is_some ( ) {
920
+ if !is_ref {
921
+ write ! ( w, "&unsafe {{ &*" ) . unwrap ( ) ;
922
+ } else {
923
+ write ! ( w, "unsafe {{ &*" ) . unwrap ( ) ;
915
924
}
916
925
} else { unimplemented ! ( ) ; }
917
926
}
@@ -964,18 +973,19 @@ impl<'a> TypeResolver<'a> {
964
973
if self . is_known_container ( & resolved_path) {
965
974
write ! ( w, "" ) . unwrap ( ) ;
966
975
} else {
967
- let ident = single_ident_generic_path_to_ident ( & p. path ) . unwrap ( ) ;
968
- if let Some ( full_path) = self . imports . get ( ident) {
969
- if let Some ( c_type) = self . from_c_conversion_suffix_from_path ( & full_path) {
970
- write ! ( w, "{}" , c_type) . unwrap ( ) ;
976
+ if let Some ( c_type) = self . from_c_conversion_suffix_from_path ( & resolved_path) {
977
+ write ! ( w, "{}" , c_type) . unwrap ( ) ;
978
+ } else if let Some ( ident) = single_ident_generic_path_to_ident ( & p. path ) {
979
+ if let Some ( decl_type) = self . declared . get ( ident) {
980
+ match decl_type {
981
+ DeclType :: StructImported ( _) if !is_ref => write ! ( w, ".inner as *mut _) }}" ) . unwrap ( ) ,
982
+ DeclType :: StructImported ( _) => { } ,
983
+ DeclType :: MirroredEnum => write ! ( w, ".to_ln()" ) . unwrap ( ) ,
984
+ _ => unimplemented ! ( ) ,
985
+ }
971
986
} else { unimplemented ! ( ) ; }
972
- } else if let Some ( decl_type) = self . declared . get ( ident) {
973
- match decl_type {
974
- DeclType :: StructImported ( _) if !is_ref => write ! ( w, ".inner as *mut _) }}" ) . unwrap ( ) ,
975
- DeclType :: StructImported ( _) => { } ,
976
- DeclType :: MirroredEnum => write ! ( w, ".to_ln()" ) . unwrap ( ) ,
977
- _ => unimplemented ! ( ) ,
978
- }
987
+ } else if self . crate_types . structs . get ( & resolved_path) . is_some ( ) {
988
+ write ! ( w, ".inner }}" ) . unwrap ( ) ;
979
989
} else { unimplemented ! ( ) ; }
980
990
}
981
991
} ,
@@ -1031,6 +1041,34 @@ impl<'a> TypeResolver<'a> {
1031
1041
true
1032
1042
} else { false }
1033
1043
}
1044
+
1045
+ fn print_c_path_intern < W : std:: io:: Write > ( & self , w : & mut W , path : & syn:: Path , is_ref : bool , is_mut : bool , ptr_for_ref : bool ) -> bool {
1046
+ let full_path = match self . maybe_resolve_path ( & path) {
1047
+ Some ( path) => path, None => return false } ;
1048
+ if let Some ( ident) = single_ident_generic_path_to_ident ( & path) {
1049
+ self . print_c_ident_intern ( w, ident, is_ref, is_mut, ptr_for_ref)
1050
+ } else if let Some ( t) = self . crate_types . traits . get ( & full_path) {
1051
+ if is_ref && ptr_for_ref {
1052
+ write ! ( w, "*const crate::{}" , full_path) . unwrap ( ) ;
1053
+ } else if is_ref {
1054
+ write ! ( w, "&crate::{}" , full_path) . unwrap ( ) ;
1055
+ } else {
1056
+ write ! ( w, "crate::{}" , full_path) . unwrap ( ) ;
1057
+ }
1058
+ true
1059
+ } else if let Some ( s) = self . crate_types . structs . get ( & full_path) {
1060
+ if is_ref && ptr_for_ref {
1061
+ write ! ( w, "*const crate::{}" , full_path) . unwrap ( ) ;
1062
+ } else if is_ref {
1063
+ write ! ( w, "&crate::{}" , full_path) . unwrap ( ) ;
1064
+ } else {
1065
+ write ! ( w, "crate::{}" , full_path) . unwrap ( ) ;
1066
+ }
1067
+ true
1068
+ } else {
1069
+ false
1070
+ }
1071
+ }
1034
1072
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 {
1035
1073
match t {
1036
1074
syn:: Type :: Path ( p) => {
@@ -1054,9 +1092,7 @@ impl<'a> TypeResolver<'a> {
1054
1092
}
1055
1093
}
1056
1094
if p. path . leading_colon . is_some ( ) { return false ; }
1057
- if let Some ( ident) = single_ident_generic_path_to_ident ( & p. path ) {
1058
- self . print_c_ident_intern ( w, & ident, is_ref, is_mut, ptr_for_ref)
1059
- } else { false }
1095
+ self . print_c_path_intern ( w, & p. path , is_ref, is_mut, ptr_for_ref)
1060
1096
} ,
1061
1097
syn:: Type :: Reference ( r) => {
1062
1098
if r. lifetime . is_some ( ) { return false ; }
0 commit comments