@@ -109,31 +109,6 @@ macro_rules! walk_supertraits { ($t: expr, $types: expr, ($( $pat: pat => $e: ex
109
109
}
110
110
} } }
111
111
112
- /// Gets a HashMap from name idents to the bounding trait for associated types.
113
- /// eg if a native trait has a "type T = TraitA", this will return a HashMap containing a mapping
114
- /// from "T" to "TraitA".
115
- fn learn_associated_types < ' a > ( t : & ' a syn:: ItemTrait ) -> HashMap < & ' a syn:: Ident , & ' a syn:: Ident > {
116
- let mut associated_types = HashMap :: new ( ) ;
117
- for item in t. items . iter ( ) {
118
- match item {
119
- & syn:: TraitItem :: Type ( ref t) => {
120
- if t. default . is_some ( ) || t. generics . lt_token . is_some ( ) { unimplemented ! ( ) ; }
121
- let mut bounds_iter = t. bounds . iter ( ) ;
122
- match bounds_iter. next ( ) . unwrap ( ) {
123
- syn:: TypeParamBound :: Trait ( tr) => {
124
- assert_simple_bound ( & tr) ;
125
- associated_types. insert ( & t. ident , assert_single_path_seg ( & tr. path ) ) ;
126
- } ,
127
- _ => unimplemented ! ( ) ,
128
- }
129
- if bounds_iter. next ( ) . is_some ( ) { unimplemented ! ( ) ; }
130
- } ,
131
- _ => { } ,
132
- }
133
- }
134
- associated_types
135
- }
136
-
137
112
/// Prints a C-mapped trait object containing a void pointer and a jump table for each function in
138
113
/// the original trait.
139
114
/// Implements the native Rust trait and relevant parent traits for the new C-mapped trait.
@@ -150,10 +125,10 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
150
125
151
126
let mut gen_types = GenericTypes :: new ( ) ;
152
127
assert ! ( gen_types. learn_generics( & t. generics, types) ) ;
128
+ gen_types. learn_associated_types ( & t, types) ;
153
129
154
130
writeln ! ( w, "#[repr(C)]\n pub struct {} {{" , trait_name) . unwrap ( ) ;
155
131
writeln ! ( w, "\t pub this_arg: *mut c_void," ) . unwrap ( ) ;
156
- let associated_types = learn_associated_types ( t) ;
157
132
let mut generated_fields = Vec :: new ( ) ; // Every field's name except this_arg, used in Clone generation
158
133
for item in t. items . iter ( ) {
159
134
match item {
@@ -210,7 +185,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
210
185
211
186
write ! ( w, "\t pub {}: extern \" C\" fn (" , m. sig. ident) . unwrap ( ) ;
212
187
generated_fields. push ( format ! ( "{}" , m. sig. ident) ) ;
213
- write_method_params ( w, & m. sig , & associated_types , "c_void" , types, Some ( & gen_types) , true , false ) ;
188
+ write_method_params ( w, & m. sig , "c_void" , types, Some ( & gen_types) , true , false ) ;
214
189
writeln ! ( w, "," ) . unwrap ( ) ;
215
190
216
191
gen_types. pop_ctx ( ) ;
@@ -363,7 +338,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
363
338
}
364
339
write_method_var_decl_body ( w, & m. sig , "\t " , types, Some ( & gen_types) , true ) ;
365
340
write ! ( w, "(self.{})(" , m. sig. ident) . unwrap ( ) ;
366
- write_method_call_params ( w, & m. sig , & associated_types , "\t " , types, Some ( & gen_types) , "" , true ) ;
341
+ write_method_call_params ( w, & m. sig , "\t " , types, Some ( & gen_types) , "" , true ) ;
367
342
368
343
writeln ! ( w, "\n \t }}" ) . unwrap ( ) ;
369
344
gen_types. pop_ctx ( ) ;
@@ -605,7 +580,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
605
580
// That's great, except that they are unresolved idents, so if we learn
606
581
// mappings from a trai defined in a different file, we may mis-resolve or
607
582
// fail to resolve the mapped types.
608
- let trait_associated_types = learn_associated_types ( trait_obj) ;
583
+ gen_types . learn_associated_types ( trait_obj, types ) ;
609
584
let mut impl_associated_types = HashMap :: new ( ) ;
610
585
for item in i. items . iter ( ) {
611
586
match item {
@@ -706,7 +681,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
706
681
write!( w, "extern \" C\" fn {}_{}_{}(" , ident, trait_obj. ident, $m. sig. ident) . unwrap( ) ;
707
682
gen_types. push_ctx( ) ;
708
683
assert!( gen_types. learn_generics( & $m. sig. generics, types) ) ;
709
- write_method_params( w, & $m. sig, & trait_associated_types , "c_void" , types, Some ( & gen_types) , true , true ) ;
684
+ write_method_params( w, & $m. sig, "c_void" , types, Some ( & gen_types) , true , true ) ;
710
685
write!( w, " {{\n \t " ) . unwrap( ) ;
711
686
write_method_var_decl_body( w, & $m. sig, "" , types, Some ( & gen_types) , false ) ;
712
687
let mut takes_self = false ;
@@ -732,7 +707,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
732
707
} ,
733
708
_ => { } ,
734
709
}
735
- write_method_call_params( w, & $m. sig, & trait_associated_types , "" , types, Some ( & gen_types) , & real_type, false ) ;
710
+ write_method_call_params( w, & $m. sig, "" , types, Some ( & gen_types) , & real_type, false ) ;
736
711
gen_types. pop_ctx( ) ;
737
712
write!( w, "\n }}\n " ) . unwrap( ) ;
738
713
if let syn:: ReturnType :: Type ( _, rtype) = & $m. sig. output {
@@ -819,7 +794,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
819
794
} ;
820
795
gen_types. push_ctx ( ) ;
821
796
assert ! ( gen_types. learn_generics( & m. sig. generics, types) ) ;
822
- write_method_params ( w, & m. sig , & HashMap :: new ( ) , & ret_type, types, Some ( & gen_types) , false , true ) ;
797
+ write_method_params ( w, & m. sig , & ret_type, types, Some ( & gen_types) , false , true ) ;
823
798
write ! ( w, " {{\n \t " ) . unwrap ( ) ;
824
799
write_method_var_decl_body ( w, & m. sig , "" , types, Some ( & gen_types) , false ) ;
825
800
let mut takes_self = false ;
@@ -837,7 +812,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
837
812
} else {
838
813
write ! ( w, "{}::{}::{}(" , types. orig_crate, resolved_path, m. sig. ident) . unwrap ( ) ;
839
814
}
840
- write_method_call_params ( w, & m. sig , & HashMap :: new ( ) , "" , types, Some ( & gen_types) , & ret_type, false ) ;
815
+ write_method_call_params ( w, & m. sig , "" , types, Some ( & gen_types) , & ret_type, false ) ;
841
816
gen_types. pop_ctx ( ) ;
842
817
writeln ! ( w, "\n }}\n " ) . unwrap ( ) ;
843
818
}
@@ -1018,11 +993,11 @@ fn writeln_fn<'a, 'b, W: std::io::Write>(w: &mut W, f: &'a syn::ItemFn, types: &
1018
993
if !gen_types. learn_generics ( & f. sig . generics , types) { return ; }
1019
994
1020
995
write ! ( w, "#[no_mangle]\n pub extern \" C\" fn {}(" , f. sig. ident) . unwrap ( ) ;
1021
- write_method_params ( w, & f. sig , & HashMap :: new ( ) , "" , types, Some ( & gen_types) , false , true ) ;
996
+ write_method_params ( w, & f. sig , "" , types, Some ( & gen_types) , false , true ) ;
1022
997
write ! ( w, " {{\n \t " ) . unwrap ( ) ;
1023
998
write_method_var_decl_body ( w, & f. sig , "" , types, Some ( & gen_types) , false ) ;
1024
999
write ! ( w, "{}::{}::{}(" , types. orig_crate, types. module_path, f. sig. ident) . unwrap ( ) ;
1025
- write_method_call_params ( w, & f. sig , & HashMap :: new ( ) , "" , types, Some ( & gen_types) , "" , false ) ;
1000
+ write_method_call_params ( w, & f. sig , "" , types, Some ( & gen_types) , "" , false ) ;
1026
1001
writeln ! ( w, "\n }}\n " ) . unwrap ( ) ;
1027
1002
}
1028
1003
0 commit comments