@@ -94,15 +94,15 @@ fn file_is_cpp(name_file: &str) -> bool {
94
94
name_file. ends_with ( ".h++" )
95
95
}
96
96
97
- fn args_are_cpp ( clang_args : & [ String ] ) -> bool {
97
+ fn args_are_cpp ( clang_args : & [ Box < str > ] ) -> bool {
98
98
for w in clang_args. windows ( 2 ) {
99
- if w[ 0 ] == "-xc++" || w[ 1 ] == "-xc++" {
99
+ if w[ 0 ] . as_ref ( ) == "-xc++" || w[ 1 ] . as_ref ( ) == "-xc++" {
100
100
return true ;
101
101
}
102
- if w[ 0 ] == "-x" && w[ 1 ] == "c++" {
102
+ if w[ 0 ] . as_ref ( ) == "-x" && w[ 1 ] . as_ref ( ) == "c++" {
103
103
return true ;
104
104
}
105
- if w[ 0 ] == "-include" && file_is_cpp ( & w[ 1 ] ) {
105
+ if w[ 0 ] . as_ref ( ) == "-include" && file_is_cpp ( w[ 1 ] . as_ref ( ) ) {
106
106
return true ;
107
107
}
108
108
}
@@ -319,22 +319,26 @@ impl Builder {
319
319
/// Generate the Rust bindings using the options built up thus far.
320
320
pub fn generate ( mut self ) -> Result < Bindings , BindgenError > {
321
321
// Add any extra arguments from the environment to the clang command line.
322
- self . options
323
- . clang_args
324
- . extend ( get_extra_clang_args ( & self . options . parse_callbacks ) ) ;
322
+ self . options . clang_args . extend (
323
+ get_extra_clang_args ( & self . options . parse_callbacks )
324
+ . into_iter ( )
325
+ . map ( String :: into_boxed_str) ,
326
+ ) ;
325
327
326
328
// Transform input headers to arguments on the clang command line.
327
329
self . options . clang_args . extend (
328
330
self . options . input_headers
329
331
[ ..self . options . input_headers . len ( ) . saturating_sub ( 1 ) ]
330
332
. iter ( )
331
- . flat_map ( |header| [ "-include" . into ( ) , header. to_string ( ) ] ) ,
333
+ . flat_map ( |header| [ "-include" . into ( ) , header. clone ( ) ] ) ,
332
334
) ;
333
335
334
336
let input_unsaved_files =
335
337
std:: mem:: take ( & mut self . options . input_header_contents )
336
338
. into_iter ( )
337
- . map ( |( name, contents) | clang:: UnsavedFile :: new ( name, contents) )
339
+ . map ( |( name, contents) | {
340
+ clang:: UnsavedFile :: new ( name. as_ref ( ) , contents. as_ref ( ) )
341
+ } )
338
342
. collect :: < Vec < _ > > ( ) ;
339
343
340
344
Bindings :: generate ( self . options , input_unsaved_files)
@@ -401,7 +405,7 @@ impl Builder {
401
405
. stdout ( Stdio :: piped ( ) ) ;
402
406
403
407
for a in & self . options . clang_args {
404
- cmd. arg ( a) ;
408
+ cmd. arg ( a. as_ref ( ) ) ;
405
409
}
406
410
407
411
for a in get_extra_clang_args ( & self . options . parse_callbacks ) {
@@ -668,16 +672,16 @@ pub(crate) const HOST_TARGET: &str =
668
672
669
673
// Some architecture triplets are different between rust and libclang, see #1211
670
674
// and duplicates.
671
- fn rust_to_clang_target ( rust_target : & str ) -> String {
675
+ fn rust_to_clang_target ( rust_target : & str ) -> Box < str > {
672
676
if rust_target. starts_with ( "aarch64-apple-" ) {
673
677
let mut clang_target = "arm64-apple-" . to_owned ( ) ;
674
678
clang_target
675
679
. push_str ( rust_target. strip_prefix ( "aarch64-apple-" ) . unwrap ( ) ) ;
676
- return clang_target;
680
+ return clang_target. into ( ) ;
677
681
} else if rust_target. starts_with ( "riscv64gc-" ) {
678
682
let mut clang_target = "riscv64-" . to_owned ( ) ;
679
683
clang_target. push_str ( rust_target. strip_prefix ( "riscv64gc-" ) . unwrap ( ) ) ;
680
- return clang_target;
684
+ return clang_target. into ( ) ;
681
685
} else if rust_target. ends_with ( "-espidf" ) {
682
686
let mut clang_target =
683
687
rust_target. strip_suffix ( "-espidf" ) . unwrap ( ) . to_owned ( ) ;
@@ -686,32 +690,32 @@ fn rust_to_clang_target(rust_target: &str) -> String {
686
690
clang_target = "riscv32-" . to_owned ( ) +
687
691
clang_target. strip_prefix ( "riscv32imc-" ) . unwrap ( ) ;
688
692
}
689
- return clang_target;
693
+ return clang_target. into ( ) ;
690
694
} else if rust_target. starts_with ( "riscv32imc-" ) {
691
695
let mut clang_target = "riscv32-" . to_owned ( ) ;
692
696
clang_target. push_str ( rust_target. strip_prefix ( "riscv32imc-" ) . unwrap ( ) ) ;
693
- return clang_target;
697
+ return clang_target. into ( ) ;
694
698
} else if rust_target. starts_with ( "riscv32imac-" ) {
695
699
let mut clang_target = "riscv32-" . to_owned ( ) ;
696
700
clang_target
697
701
. push_str ( rust_target. strip_prefix ( "riscv32imac-" ) . unwrap ( ) ) ;
698
- return clang_target;
702
+ return clang_target. into ( ) ;
699
703
}
700
- rust_target. to_owned ( )
704
+ rust_target. into ( )
701
705
}
702
706
703
707
/// Returns the effective target, and whether it was explicitly specified on the
704
708
/// clang flags.
705
- fn find_effective_target ( clang_args : & [ String ] ) -> ( String , bool ) {
709
+ fn find_effective_target ( clang_args : & [ Box < str > ] ) -> ( Box < str > , bool ) {
706
710
let mut args = clang_args. iter ( ) ;
707
711
while let Some ( opt) = args. next ( ) {
708
712
if opt. starts_with ( "--target=" ) {
709
713
let mut split = opt. split ( '=' ) ;
710
714
split. next ( ) ;
711
- return ( split. next ( ) . unwrap ( ) . to_owned ( ) , true ) ;
715
+ return ( split. next ( ) . unwrap ( ) . into ( ) , true ) ;
712
716
}
713
717
714
- if opt == "-target" {
718
+ if opt. as_ref ( ) == "-target" {
715
719
if let Some ( target) = args. next ( ) {
716
720
return ( target. clone ( ) , true ) ;
717
721
}
@@ -756,9 +760,10 @@ impl Bindings {
756
760
// opening libclang.so, it has to be the same architecture and thus the
757
761
// check is fine.
758
762
if !explicit_target && !is_host_build {
759
- options
760
- . clang_args
761
- . insert ( 0 , format ! ( "--target={}" , effective_target) ) ;
763
+ options. clang_args . insert (
764
+ 0 ,
765
+ format ! ( "--target={}" , effective_target) . into_boxed_str ( ) ,
766
+ ) ;
762
767
} ;
763
768
764
769
fn detect_include_paths ( options : & mut BindgenOptions ) {
@@ -779,7 +784,7 @@ impl Bindings {
779
784
return false ;
780
785
}
781
786
782
- let arg = & * * arg;
787
+ let arg = arg. as_ref ( ) ;
783
788
784
789
// https://clang.llvm.org/docs/ClangCommandLineReference.html
785
790
// -isystem and -isystem-after are harmless.
@@ -796,7 +801,7 @@ impl Bindings {
796
801
797
802
true
798
803
} )
799
- . cloned ( )
804
+ . map ( |arg| arg . clone ( ) . into ( ) )
800
805
. collect :: < Vec < _ > > ( )
801
806
} ;
802
807
@@ -828,8 +833,8 @@ impl Bindings {
828
833
if let Some ( search_paths) = search_paths {
829
834
for path in search_paths. into_iter ( ) {
830
835
if let Ok ( path) = path. into_os_string ( ) . into_string ( ) {
831
- options. clang_args . push ( "-isystem" . to_owned ( ) ) ;
832
- options. clang_args . push ( path) ;
836
+ options. clang_args . push ( "-isystem" . into ( ) ) ;
837
+ options. clang_args . push ( path. into_boxed_str ( ) ) ;
833
838
}
834
839
}
835
840
}
@@ -849,7 +854,7 @@ impl Bindings {
849
854
}
850
855
851
856
if let Some ( h) = options. input_headers . last ( ) {
852
- let path = Path :: new ( h) ;
857
+ let path = Path :: new ( h. as_ref ( ) ) ;
853
858
if let Ok ( md) = std:: fs:: metadata ( path) {
854
859
if md. is_dir ( ) {
855
860
return Err ( BindgenError :: FolderAsHeader ( path. into ( ) ) ) ;
@@ -859,18 +864,17 @@ impl Bindings {
859
864
path. into ( ) ,
860
865
) ) ;
861
866
}
862
- let h = h. clone ( ) ;
863
- options. clang_args . push ( h) ;
867
+ options. clang_args . push ( h. clone ( ) ) ;
864
868
} else {
865
869
return Err ( BindgenError :: NotExist ( path. into ( ) ) ) ;
866
870
}
867
871
}
868
872
869
873
for ( idx, f) in input_unsaved_files. iter ( ) . enumerate ( ) {
870
874
if idx != 0 || !options. input_headers . is_empty ( ) {
871
- options. clang_args . push ( "-include" . to_owned ( ) ) ;
875
+ options. clang_args . push ( "-include" . into ( ) ) ;
872
876
}
873
- options. clang_args . push ( f. name . to_str ( ) . unwrap ( ) . to_owned ( ) )
877
+ options. clang_args . push ( f. name . to_str ( ) . unwrap ( ) . into ( ) )
874
878
}
875
879
876
880
debug ! ( "Fixed-up options: {:?}" , options) ;
@@ -1285,33 +1289,36 @@ fn commandline_flag_unit_test_function() {
1285
1289
1286
1290
#[ test]
1287
1291
fn test_rust_to_clang_target ( ) {
1288
- assert_eq ! ( rust_to_clang_target( "aarch64-apple-ios" ) , "arm64-apple-ios" ) ;
1292
+ assert_eq ! (
1293
+ rust_to_clang_target( "aarch64-apple-ios" ) . as_ref( ) ,
1294
+ "arm64-apple-ios"
1295
+ ) ;
1289
1296
}
1290
1297
1291
1298
#[ test]
1292
1299
fn test_rust_to_clang_target_riscv ( ) {
1293
1300
assert_eq ! (
1294
- rust_to_clang_target( "riscv64gc-unknown-linux-gnu" ) ,
1301
+ rust_to_clang_target( "riscv64gc-unknown-linux-gnu" ) . as_ref ( ) ,
1295
1302
"riscv64-unknown-linux-gnu"
1296
1303
) ;
1297
1304
assert_eq ! (
1298
- rust_to_clang_target( "riscv32imc-unknown-none-elf" ) ,
1305
+ rust_to_clang_target( "riscv32imc-unknown-none-elf" ) . as_ref ( ) ,
1299
1306
"riscv32-unknown-none-elf"
1300
1307
) ;
1301
1308
assert_eq ! (
1302
- rust_to_clang_target( "riscv32imac-unknown-none-elf" ) ,
1309
+ rust_to_clang_target( "riscv32imac-unknown-none-elf" ) . as_ref ( ) ,
1303
1310
"riscv32-unknown-none-elf"
1304
1311
) ;
1305
1312
}
1306
1313
1307
1314
#[ test]
1308
1315
fn test_rust_to_clang_target_espidf ( ) {
1309
1316
assert_eq ! (
1310
- rust_to_clang_target( "riscv32imc-esp-espidf" ) ,
1317
+ rust_to_clang_target( "riscv32imc-esp-espidf" ) . as_ref ( ) ,
1311
1318
"riscv32-esp-elf"
1312
1319
) ;
1313
1320
assert_eq ! (
1314
- rust_to_clang_target( "xtensa-esp32-espidf" ) ,
1321
+ rust_to_clang_target( "xtensa-esp32-espidf" ) . as_ref ( ) ,
1315
1322
"xtensa-esp32-elf"
1316
1323
) ;
1317
1324
}
0 commit comments