@@ -25,15 +25,15 @@ impl fmt::Display for PythonVersion {
25
25
}
26
26
}
27
27
28
- const CFG_KEY : & ' static str = "py_sys_config" ;
28
+ const CFG_KEY : & str = "py_sys_config" ;
29
29
30
30
// windows' python writes out lines with the windows crlf sequence;
31
31
// posix platforms and mac os should write out lines with just lf.
32
32
#[ cfg( target_os = "windows" ) ]
33
- static NEWLINE_SEQUENCE : & ' static str = "\r \n " ;
33
+ static NEWLINE_SEQUENCE : & str = "\r \n " ;
34
34
35
35
#[ cfg( not( target_os = "windows" ) ) ]
36
- static NEWLINE_SEQUENCE : & ' static str = "\n " ;
36
+ static NEWLINE_SEQUENCE : & str = "\n " ;
37
37
38
38
// A list of python interpreter compile-time preprocessor defines that
39
39
// we will pick up and pass to rustc via --cfg=py_sys_config={varname};
@@ -48,7 +48,7 @@ static NEWLINE_SEQUENCE: &'static str = "\n";
48
48
// (hrm, this is sort of re-implementing what distutils does, except
49
49
// by passing command line args instead of referring to a python.h)
50
50
#[ cfg( not( target_os = "windows" ) ) ]
51
- static SYSCONFIG_FLAGS : [ & ' static str ; 7 ] = [
51
+ static SYSCONFIG_FLAGS : [ & str ; 7 ] = [
52
52
"Py_USING_UNICODE" ,
53
53
"Py_UNICODE_WIDE" ,
54
54
"WITH_THREAD" ,
@@ -58,7 +58,7 @@ static SYSCONFIG_FLAGS: [&'static str; 7] = [
58
58
"COUNT_ALLOCS" ,
59
59
] ;
60
60
61
- static SYSCONFIG_VALUES : [ & ' static str ; 1 ] = [
61
+ static SYSCONFIG_VALUES : [ & str ; 1 ] = [
62
62
// cfg doesn't support flags with values, just bools - so flags
63
63
// below are translated into bools as {varname}_{val}
64
64
//
@@ -70,7 +70,7 @@ static SYSCONFIG_VALUES: [&'static str; 1] = [
70
70
/// the interpreter and printing variables of interest from
71
71
/// sysconfig.get_config_vars.
72
72
#[ cfg( not( target_os = "windows" ) ) ]
73
- fn get_config_vars ( python_path : & String ) -> Result < HashMap < String , String > , String > {
73
+ fn get_config_vars ( python_path : & str ) -> Result < HashMap < String , String > , String > {
74
74
let mut script = "import sysconfig; \
75
75
config = sysconfig.get_config_vars();"
76
76
. to_owned ( ) ;
@@ -81,7 +81,7 @@ fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, Stri
81
81
k,
82
82
if is_value( k) { "None" } else { "0" }
83
83
) ) ;
84
- script. push_str ( ";" ) ;
84
+ script. push ( ';' ) ;
85
85
}
86
86
87
87
let mut cmd = Command :: new ( python_path) ;
@@ -93,7 +93,7 @@ fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, Stri
93
93
94
94
if !out. status . success ( ) {
95
95
let stderr = String :: from_utf8 ( out. stderr ) . unwrap ( ) ;
96
- let mut msg = format ! ( "python script failed with stderr:\n \n " ) ;
96
+ let mut msg = "python script failed with stderr:\n \n " . to_string ( ) ;
97
97
msg. push_str ( & stderr) ;
98
98
return Err ( msg) ;
99
99
}
@@ -105,15 +105,14 @@ fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, Stri
105
105
"python stdout len didn't return expected number of lines:
106
106
{}" ,
107
107
split_stdout. len( )
108
- )
109
- . to_string ( ) ) ;
108
+ ) ) ;
110
109
}
111
110
let all_vars = SYSCONFIG_FLAGS . iter ( ) . chain ( SYSCONFIG_VALUES . iter ( ) ) ;
112
111
// let var_map: HashMap<String, String> = HashMap::new();
113
112
Ok ( all_vars. zip ( split_stdout. iter ( ) ) . fold (
114
113
HashMap :: new ( ) ,
115
114
|mut memo : HashMap < String , String > , ( & k, & v) | {
116
- if !( v. to_owned ( ) == "None" && is_value ( k) ) {
115
+ if !( v == "None" && is_value ( k) ) {
117
116
memo. insert ( k. to_owned ( ) , v. to_owned ( ) ) ;
118
117
}
119
118
memo
@@ -122,7 +121,7 @@ fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, Stri
122
121
}
123
122
124
123
#[ cfg( target_os = "windows" ) ]
125
- fn get_config_vars ( _: & String ) -> Result < HashMap < String , String > , String > {
124
+ fn get_config_vars ( _: & str ) -> Result < HashMap < String , String > , String > {
126
125
// sysconfig is missing all the flags on windows, so we can't actually
127
126
// query the interpreter directly for its build flags.
128
127
//
@@ -151,7 +150,7 @@ fn get_config_vars(_: &String) -> Result<HashMap<String, String>, String> {
151
150
}
152
151
153
152
fn is_value ( key : & str ) -> bool {
154
- SYSCONFIG_VALUES . iter ( ) . find ( |x| * * x == key) . is_some ( )
153
+ SYSCONFIG_VALUES . iter ( ) . any ( |x| * x == key)
155
154
}
156
155
157
156
fn cfg_line_for_var ( key : & str , val : & str ) -> Option < String > {
@@ -185,17 +184,17 @@ fn run_python_script(interpreter: &str, script: &str) -> Result<String, String>
185
184
186
185
if !out. status . success ( ) {
187
186
let stderr = String :: from_utf8 ( out. stderr ) . unwrap ( ) ;
188
- let mut msg = format ! ( "python script failed with stderr:\n \n " ) ;
187
+ let mut msg = "python script failed with stderr:\n \n " . to_string ( ) ;
189
188
msg. push_str ( & stderr) ;
190
189
return Err ( msg) ;
191
190
}
192
191
193
- let out = String :: from_utf8 ( out. stdout ) . unwrap ( ) ;
194
- return Ok ( out) ;
192
+ Ok ( String :: from_utf8 ( out. stdout ) . unwrap ( ) )
195
193
}
196
194
197
195
#[ cfg( not( target_os = "macos" ) ) ]
198
196
#[ cfg( not( target_os = "windows" ) ) ]
197
+ #[ allow( clippy:: unnecessary_wraps) ]
199
198
fn get_rustc_link_lib (
200
199
_: & PythonVersion ,
201
200
ld_version : & str ,
@@ -278,7 +277,7 @@ fn find_interpreter_and_get_config(
278
277
let ( executable, interpreter_version, lines) =
279
278
get_config_from_interpreter ( interpreter_path) ?;
280
279
if matching_version ( expected_version, & interpreter_version) {
281
- return Ok ( ( interpreter_version, executable. to_owned ( ) , lines) ) ;
280
+ return Ok ( ( interpreter_version, executable, lines) ) ;
282
281
} else {
283
282
return Err ( format ! (
284
283
"Wrong python version in PYTHON_SYS_EXECUTABLE={}\n \
@@ -297,11 +296,11 @@ fn find_interpreter_and_get_config(
297
296
}
298
297
299
298
for name in possible_names. iter ( ) {
300
- if let Some ( ( executable, interpreter_version, lines) ) =
301
- get_config_from_interpreter ( name) . ok ( )
299
+ if let Ok ( ( executable, interpreter_version, lines) ) =
300
+ get_config_from_interpreter ( name)
302
301
{
303
302
if matching_version ( expected_version, & interpreter_version) {
304
- return Ok ( ( interpreter_version, executable. to_owned ( ) , lines) ) ;
303
+ return Ok ( ( interpreter_version, executable, lines) ) ;
305
304
}
306
305
}
307
306
}
@@ -371,20 +370,18 @@ fn configure_from_path(expected_version: &PythonVersion) -> Result<String, Strin
371
370
println ! ( "cargo:rustc-link-search=native={}\\ libs" , exec_prefix) ;
372
371
}
373
372
}
374
- } else if link_mode_unresolved_static {
375
- if cfg ! ( target_os = "windows" ) {
376
- // static-nobundle requires a Nightly rustc up to at least
377
- // Rust 1.39 (https://github.com/rust-lang/rust/issues/37403).
378
- //
379
- // We need to use static linking on Windows to prevent symbol
380
- // name mangling. Otherwise Rust will prefix extern {} symbols
381
- // with __imp_. But if we used normal "static," we need a
382
- // pythonXY.lib at build time to package into the rlib.
383
- //
384
- // static-nobundle removes the build-time library requirement,
385
- // allowing a downstream consumer to provide the pythonXY library.
386
- println ! ( "cargo:rustc-link-lib=static-nobundle=pythonXY" ) ;
387
- }
373
+ } else if link_mode_unresolved_static && cfg ! ( target_os = "windows" ) {
374
+ // static-nobundle requires a Nightly rustc up to at least
375
+ // Rust 1.39 (https://github.com/rust-lang/rust/issues/37403).
376
+ //
377
+ // We need to use static linking on Windows to prevent symbol
378
+ // name mangling. Otherwise Rust will prefix extern {} symbols
379
+ // with __imp_. But if we used normal "static," we need a
380
+ // pythonXY.lib at build time to package into the rlib.
381
+ //
382
+ // static-nobundle removes the build-time library requirement,
383
+ // allowing a downstream consumer to provide the pythonXY library.
384
+ println ! ( "cargo:rustc-link-lib=static-nobundle=pythonXY" ) ;
388
385
}
389
386
390
387
if let PythonVersion {
@@ -402,7 +399,7 @@ fn configure_from_path(expected_version: &PythonVersion) -> Result<String, Strin
402
399
}
403
400
}
404
401
405
- return Ok ( interpreter_path) ;
402
+ Ok ( interpreter_path)
406
403
}
407
404
408
405
/// Determine the python version we're supposed to be building
@@ -418,17 +415,11 @@ fn version_from_env() -> Result<PythonVersion, String> {
418
415
let mut vars = env:: vars ( ) . collect :: < Vec < _ > > ( ) ;
419
416
vars. sort_by ( |a, b| b. cmp ( a) ) ;
420
417
for ( key, _) in vars {
421
- match re. captures ( & key) {
422
- Some ( cap) => {
423
- return Ok ( PythonVersion {
424
- major : cap. get ( 1 ) . unwrap ( ) . as_str ( ) . parse ( ) . unwrap ( ) ,
425
- minor : match cap. get ( 3 ) {
426
- Some ( s) => Some ( s. as_str ( ) . parse ( ) . unwrap ( ) ) ,
427
- None => None ,
428
- } ,
429
- } )
430
- }
431
- None => ( ) ,
418
+ if let Some ( cap) = re. captures ( & key) {
419
+ return Ok ( PythonVersion {
420
+ major : cap. get ( 1 ) . unwrap ( ) . as_str ( ) . parse ( ) . unwrap ( ) ,
421
+ minor : cap. get ( 3 ) . map ( |s| s. as_str ( ) . parse ( ) . unwrap ( ) ) ,
422
+ } ) ;
432
423
}
433
424
}
434
425
Err (
@@ -459,9 +450,8 @@ fn main() {
459
450
config_map. insert ( "Py_REF_DEBUG" . to_owned ( ) , "1" . to_owned ( ) ) ; // Py_TRACE_REFS implies Py_REF_DEBUG.
460
451
}
461
452
for ( key, val) in & config_map {
462
- match cfg_line_for_var ( key, val) {
463
- Some ( line) => println ! ( "{}" , line) ,
464
- None => ( ) ,
453
+ if let Some ( line) = cfg_line_for_var ( key, val) {
454
+ println ! ( "{}" , line) ;
465
455
}
466
456
}
467
457
@@ -489,7 +479,7 @@ fn main() {
489
479
} ) ;
490
480
println ! (
491
481
"cargo:python_flags={}" ,
492
- if flags. len ( ) > 0 {
482
+ if ! flags. is_empty ( ) {
493
483
& flags[ ..flags. len( ) - 1 ]
494
484
} else {
495
485
""
0 commit comments