@@ -94,15 +94,15 @@ fn file_is_cpp(name_file: &str) -> bool {
9494 name_file. ends_with ( ".h++" )
9595}
9696
97- fn args_are_cpp ( clang_args : & [ String ] ) -> bool {
97+ fn args_are_cpp ( clang_args : & [ Box < str > ] ) -> bool {
9898 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++" {
100100 return true ;
101101 }
102- if w[ 0 ] == "-x" && w[ 1 ] == "c++" {
102+ if w[ 0 ] . as_ref ( ) == "-x" && w[ 1 ] . as_ref ( ) == "c++" {
103103 return true ;
104104 }
105- if w[ 0 ] == "-include" && file_is_cpp ( & w[ 1 ] ) {
105+ if w[ 0 ] . as_ref ( ) == "-include" && file_is_cpp ( w[ 1 ] . as_ref ( ) ) {
106106 return true ;
107107 }
108108 }
@@ -319,22 +319,26 @@ impl Builder {
319319 /// Generate the Rust bindings using the options built up thus far.
320320 pub fn generate ( mut self ) -> Result < Bindings , BindgenError > {
321321 // 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+ ) ;
325327
326328 // Transform input headers to arguments on the clang command line.
327329 self . options . clang_args . extend (
328330 self . options . input_headers
329331 [ ..self . options . input_headers . len ( ) . saturating_sub ( 1 ) ]
330332 . iter ( )
331- . flat_map ( |header| [ "-include" . into ( ) , header. to_string ( ) ] ) ,
333+ . flat_map ( |header| [ "-include" . into ( ) , header. clone ( ) ] ) ,
332334 ) ;
333335
334336 let input_unsaved_files =
335337 std:: mem:: take ( & mut self . options . input_header_contents )
336338 . 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+ } )
338342 . collect :: < Vec < _ > > ( ) ;
339343
340344 Bindings :: generate ( self . options , input_unsaved_files)
@@ -401,7 +405,7 @@ impl Builder {
401405 . stdout ( Stdio :: piped ( ) ) ;
402406
403407 for a in & self . options . clang_args {
404- cmd. arg ( a) ;
408+ cmd. arg ( a. as_ref ( ) ) ;
405409 }
406410
407411 for a in get_extra_clang_args ( & self . options . parse_callbacks ) {
@@ -668,16 +672,16 @@ pub(crate) const HOST_TARGET: &str =
668672
669673// Some architecture triplets are different between rust and libclang, see #1211
670674// and duplicates.
671- fn rust_to_clang_target ( rust_target : & str ) -> String {
675+ fn rust_to_clang_target ( rust_target : & str ) -> Box < str > {
672676 if rust_target. starts_with ( "aarch64-apple-" ) {
673677 let mut clang_target = "arm64-apple-" . to_owned ( ) ;
674678 clang_target
675679 . push_str ( rust_target. strip_prefix ( "aarch64-apple-" ) . unwrap ( ) ) ;
676- return clang_target;
680+ return clang_target. into ( ) ;
677681 } else if rust_target. starts_with ( "riscv64gc-" ) {
678682 let mut clang_target = "riscv64-" . to_owned ( ) ;
679683 clang_target. push_str ( rust_target. strip_prefix ( "riscv64gc-" ) . unwrap ( ) ) ;
680- return clang_target;
684+ return clang_target. into ( ) ;
681685 } else if rust_target. ends_with ( "-espidf" ) {
682686 let mut clang_target =
683687 rust_target. strip_suffix ( "-espidf" ) . unwrap ( ) . to_owned ( ) ;
@@ -686,32 +690,32 @@ fn rust_to_clang_target(rust_target: &str) -> String {
686690 clang_target = "riscv32-" . to_owned ( ) +
687691 clang_target. strip_prefix ( "riscv32imc-" ) . unwrap ( ) ;
688692 }
689- return clang_target;
693+ return clang_target. into ( ) ;
690694 } else if rust_target. starts_with ( "riscv32imc-" ) {
691695 let mut clang_target = "riscv32-" . to_owned ( ) ;
692696 clang_target. push_str ( rust_target. strip_prefix ( "riscv32imc-" ) . unwrap ( ) ) ;
693- return clang_target;
697+ return clang_target. into ( ) ;
694698 } else if rust_target. starts_with ( "riscv32imac-" ) {
695699 let mut clang_target = "riscv32-" . to_owned ( ) ;
696700 clang_target
697701 . push_str ( rust_target. strip_prefix ( "riscv32imac-" ) . unwrap ( ) ) ;
698- return clang_target;
702+ return clang_target. into ( ) ;
699703 }
700- rust_target. to_owned ( )
704+ rust_target. into ( )
701705}
702706
703707/// Returns the effective target, and whether it was explicitly specified on the
704708/// clang flags.
705- fn find_effective_target ( clang_args : & [ String ] ) -> ( String , bool ) {
709+ fn find_effective_target ( clang_args : & [ Box < str > ] ) -> ( Box < str > , bool ) {
706710 let mut args = clang_args. iter ( ) ;
707711 while let Some ( opt) = args. next ( ) {
708712 if opt. starts_with ( "--target=" ) {
709713 let mut split = opt. split ( '=' ) ;
710714 split. next ( ) ;
711- return ( split. next ( ) . unwrap ( ) . to_owned ( ) , true ) ;
715+ return ( split. next ( ) . unwrap ( ) . into ( ) , true ) ;
712716 }
713717
714- if opt == "-target" {
718+ if opt. as_ref ( ) == "-target" {
715719 if let Some ( target) = args. next ( ) {
716720 return ( target. clone ( ) , true ) ;
717721 }
@@ -756,9 +760,10 @@ impl Bindings {
756760 // opening libclang.so, it has to be the same architecture and thus the
757761 // check is fine.
758762 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+ ) ;
762767 } ;
763768
764769 fn detect_include_paths ( options : & mut BindgenOptions ) {
@@ -779,7 +784,7 @@ impl Bindings {
779784 return false ;
780785 }
781786
782- let arg = & * * arg;
787+ let arg = arg. as_ref ( ) ;
783788
784789 // https://clang.llvm.org/docs/ClangCommandLineReference.html
785790 // -isystem and -isystem-after are harmless.
@@ -796,7 +801,7 @@ impl Bindings {
796801
797802 true
798803 } )
799- . cloned ( )
804+ . map ( |arg| arg . clone ( ) . into ( ) )
800805 . collect :: < Vec < _ > > ( )
801806 } ;
802807
@@ -828,8 +833,8 @@ impl Bindings {
828833 if let Some ( search_paths) = search_paths {
829834 for path in search_paths. into_iter ( ) {
830835 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 ( ) ) ;
833838 }
834839 }
835840 }
@@ -849,7 +854,7 @@ impl Bindings {
849854 }
850855
851856 if let Some ( h) = options. input_headers . last ( ) {
852- let path = Path :: new ( h) ;
857+ let path = Path :: new ( h. as_ref ( ) ) ;
853858 if let Ok ( md) = std:: fs:: metadata ( path) {
854859 if md. is_dir ( ) {
855860 return Err ( BindgenError :: FolderAsHeader ( path. into ( ) ) ) ;
@@ -859,18 +864,17 @@ impl Bindings {
859864 path. into ( ) ,
860865 ) ) ;
861866 }
862- let h = h. clone ( ) ;
863- options. clang_args . push ( h) ;
867+ options. clang_args . push ( h. clone ( ) ) ;
864868 } else {
865869 return Err ( BindgenError :: NotExist ( path. into ( ) ) ) ;
866870 }
867871 }
868872
869873 for ( idx, f) in input_unsaved_files. iter ( ) . enumerate ( ) {
870874 if idx != 0 || !options. input_headers . is_empty ( ) {
871- options. clang_args . push ( "-include" . to_owned ( ) ) ;
875+ options. clang_args . push ( "-include" . into ( ) ) ;
872876 }
873- options. clang_args . push ( f. name . to_str ( ) . unwrap ( ) . to_owned ( ) )
877+ options. clang_args . push ( f. name . to_str ( ) . unwrap ( ) . into ( ) )
874878 }
875879
876880 debug ! ( "Fixed-up options: {:?}" , options) ;
@@ -1285,33 +1289,36 @@ fn commandline_flag_unit_test_function() {
12851289
12861290#[ test]
12871291fn 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+ ) ;
12891296}
12901297
12911298#[ test]
12921299fn test_rust_to_clang_target_riscv ( ) {
12931300 assert_eq ! (
1294- rust_to_clang_target( "riscv64gc-unknown-linux-gnu" ) ,
1301+ rust_to_clang_target( "riscv64gc-unknown-linux-gnu" ) . as_ref ( ) ,
12951302 "riscv64-unknown-linux-gnu"
12961303 ) ;
12971304 assert_eq ! (
1298- rust_to_clang_target( "riscv32imc-unknown-none-elf" ) ,
1305+ rust_to_clang_target( "riscv32imc-unknown-none-elf" ) . as_ref ( ) ,
12991306 "riscv32-unknown-none-elf"
13001307 ) ;
13011308 assert_eq ! (
1302- rust_to_clang_target( "riscv32imac-unknown-none-elf" ) ,
1309+ rust_to_clang_target( "riscv32imac-unknown-none-elf" ) . as_ref ( ) ,
13031310 "riscv32-unknown-none-elf"
13041311 ) ;
13051312}
13061313
13071314#[ test]
13081315fn test_rust_to_clang_target_espidf ( ) {
13091316 assert_eq ! (
1310- rust_to_clang_target( "riscv32imc-esp-espidf" ) ,
1317+ rust_to_clang_target( "riscv32imc-esp-espidf" ) . as_ref ( ) ,
13111318 "riscv32-esp-elf"
13121319 ) ;
13131320 assert_eq ! (
1314- rust_to_clang_target( "xtensa-esp32-espidf" ) ,
1321+ rust_to_clang_target( "xtensa-esp32-espidf" ) . as_ref ( ) ,
13151322 "xtensa-esp32-elf"
13161323 ) ;
13171324}
0 commit comments