18
18
//! It also generates relevant memory-management functions and free-standing functions with
19
19
//! parameters mapped.
20
20
21
- use std:: collections:: { HashMap , hash_map} ;
21
+ use std:: collections:: { HashMap , hash_map, HashSet } ;
22
22
use std:: env;
23
23
use std:: fs:: File ;
24
24
use std:: io:: { Read , Write } ;
@@ -875,7 +875,7 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct,
875
875
/// Trait struct containing a pointer to the passed struct's inner field and the wrapper functions.
876
876
///
877
877
/// A few non-crate Traits are hard-coded including Default.
878
- fn writeln_impl < W : std:: io:: Write > ( w : & mut W , i : & syn:: ItemImpl , types : & mut TypeResolver ) {
878
+ fn writeln_impl < W : std:: io:: Write > ( w : & mut W , w_uses : & mut HashSet < String , NonRandomHash > , i : & syn:: ItemImpl , types : & mut TypeResolver ) {
879
879
match export_status ( & i. attrs ) {
880
880
ExportStatus :: Export => { } ,
881
881
ExportStatus :: NoExport |ExportStatus :: TestOnly => return ,
@@ -979,11 +979,11 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
979
979
// type-conversion logic without actually knowing the concrete native type.
980
980
if !resolved_path. starts_with ( types. module_path ) {
981
981
if !first_seg_is_stdlib ( resolved_path. split ( "::" ) . next ( ) . unwrap ( ) ) {
982
- writeln ! ( w , "use crate::{}::native{} as native{};" , resolved_path. rsplitn( 2 , "::" ) . skip( 1 ) . next( ) . unwrap( ) , ident, ident) . unwrap ( ) ;
983
- writeln ! ( w , "use crate::{};" , resolved_path) . unwrap ( ) ;
984
- writeln ! ( w , "use crate::{}_free_void;" , resolved_path) . unwrap ( ) ;
982
+ w_uses . insert ( format ! ( "use crate::{}::native{} as native{};" , resolved_path. rsplitn( 2 , "::" ) . skip( 1 ) . next( ) . unwrap( ) , ident, ident) ) ;
983
+ w_uses . insert ( format ! ( "use crate::{};" , resolved_path) ) ;
984
+ w_uses . insert ( format ! ( "use crate::{}_free_void;" , resolved_path) ) ;
985
985
} else {
986
- writeln ! ( w , "use {} as native{};" , resolved_path, ident) . unwrap ( ) ;
986
+ w_uses . insert ( format ! ( "use {} as native{};" , resolved_path, ident) ) ;
987
987
}
988
988
}
989
989
writeln ! ( w, "impl From<native{}> for crate::{} {{" , ident, full_trait_path) . unwrap ( ) ;
@@ -1407,7 +1407,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
1407
1407
}
1408
1408
}
1409
1409
} else if let Some ( resolved_path) = types. maybe_resolve_ident ( & ident) {
1410
- create_alias_for_impl ( resolved_path, i, types, move |aliased_impl, types| writeln_impl ( w, & aliased_impl, types) ) ;
1410
+ create_alias_for_impl ( resolved_path, i, types, move |aliased_impl, types| writeln_impl ( w, w_uses , & aliased_impl, types) ) ;
1411
1411
} else {
1412
1412
eprintln ! ( "Not implementing anything for {} due to no-resolve (probably the type isn't pub)" , ident) ;
1413
1413
}
@@ -1915,7 +1915,7 @@ fn writeln_fn<'a, 'b, W: std::io::Write>(w: &mut W, f: &'a syn::ItemFn, types: &
1915
1915
// *** File/Crate Walking Logic ***
1916
1916
// ********************************
1917
1917
1918
- fn convert_priv_mod < ' a , ' b : ' a , W : std:: io:: Write > ( w : & mut W , libast : & ' b FullLibraryAST , crate_types : & CrateTypes < ' b > , out_dir : & str , mod_path : & str , module : & ' b syn:: ItemMod ) {
1918
+ fn convert_priv_mod < ' a , ' b : ' a , W : std:: io:: Write > ( w : & mut W , w_uses : & mut HashSet < String , NonRandomHash > , libast : & ' b FullLibraryAST , crate_types : & CrateTypes < ' b > , out_dir : & str , mod_path : & str , module : & ' b syn:: ItemMod ) {
1919
1919
// We want to ignore all items declared in this module (as they are not pub), but we still need
1920
1920
// to give the ImportResolver any use statements, so we copy them here.
1921
1921
let mut use_items = Vec :: new ( ) ;
@@ -1930,9 +1930,9 @@ fn convert_priv_mod<'a, 'b: 'a, W: std::io::Write>(w: &mut W, libast: &'b FullLi
1930
1930
writeln ! ( w, "mod {} {{\n {}" , module. ident, DEFAULT_IMPORTS ) . unwrap ( ) ;
1931
1931
for item in module. content . as_ref ( ) . unwrap ( ) . 1 . iter ( ) {
1932
1932
match item {
1933
- syn:: Item :: Mod ( m) => convert_priv_mod ( w, libast, crate_types, out_dir, & format ! ( "{}::{}" , mod_path, module. ident) , m) ,
1933
+ syn:: Item :: Mod ( m) => convert_priv_mod ( w, w_uses , libast, crate_types, out_dir, & format ! ( "{}::{}" , mod_path, module. ident) , m) ,
1934
1934
syn:: Item :: Impl ( i) => {
1935
- writeln_impl ( w, i, & mut types) ;
1935
+ writeln_impl ( w, w_uses , i, & mut types) ;
1936
1936
} ,
1937
1937
_ => { } ,
1938
1938
}
@@ -1959,6 +1959,7 @@ fn convert_file<'a, 'b>(libast: &'a FullLibraryAST, crate_types: &CrateTypes<'a>
1959
1959
let _ = std:: fs:: create_dir ( ( & new_file_path. as_ref ( ) as & std:: path:: Path ) . parent ( ) . unwrap ( ) ) ;
1960
1960
let mut out = std:: fs:: OpenOptions :: new ( ) . write ( true ) . create ( true ) . truncate ( true )
1961
1961
. open ( new_file_path) . expect ( "Unable to open new src file" ) ;
1962
+ let mut out_uses = HashSet :: default ( ) ;
1962
1963
1963
1964
writeln ! ( out, "// This file is Copyright its original authors, visible in version control" ) . unwrap ( ) ;
1964
1965
writeln ! ( out, "// history and in the source files from which this was generated." ) . unwrap ( ) ;
@@ -2018,7 +2019,7 @@ fn convert_file<'a, 'b>(libast: &'a FullLibraryAST, crate_types: &CrateTypes<'a>
2018
2019
}
2019
2020
} ,
2020
2021
syn:: Item :: Impl ( i) => {
2021
- writeln_impl ( & mut out, & i, & mut type_resolver) ;
2022
+ writeln_impl ( & mut out, & mut out_uses , & i, & mut type_resolver) ;
2022
2023
} ,
2023
2024
syn:: Item :: Struct ( s) => {
2024
2025
if let syn:: Visibility :: Public ( _) = s. vis {
@@ -2031,7 +2032,7 @@ fn convert_file<'a, 'b>(libast: &'a FullLibraryAST, crate_types: &CrateTypes<'a>
2031
2032
}
2032
2033
} ,
2033
2034
syn:: Item :: Mod ( m) => {
2034
- convert_priv_mod ( & mut out, libast, crate_types, out_dir, & format ! ( "{}::{}" , module, m. ident) , m) ;
2035
+ convert_priv_mod ( & mut out, & mut out_uses , libast, crate_types, out_dir, & format ! ( "{}::{}" , module, m. ident) , m) ;
2035
2036
} ,
2036
2037
syn:: Item :: Const ( c) => {
2037
2038
// Re-export any primitive-type constants.
@@ -2101,6 +2102,10 @@ fn convert_file<'a, 'b>(libast: &'a FullLibraryAST, crate_types: &CrateTypes<'a>
2101
2102
}
2102
2103
}
2103
2104
2105
+ for use_stmt in out_uses {
2106
+ writeln ! ( out, "{}" , use_stmt) . unwrap ( ) ;
2107
+ }
2108
+
2104
2109
out. flush ( ) . unwrap ( ) ;
2105
2110
}
2106
2111
}
0 commit comments