3
3
#[ cfg( test) ]
4
4
mod tests;
5
5
6
- use crate :: borrow:: Borrow ;
7
6
use crate :: cmp;
8
7
use crate :: collections:: BTreeMap ;
9
8
use crate :: convert:: { TryFrom , TryInto } ;
@@ -46,6 +45,12 @@ pub struct EnvKey {
46
45
utf16 : Vec < u16 > ,
47
46
}
48
47
48
+ impl EnvKey {
49
+ fn new < T : Into < OsString > > ( key : T ) -> Self {
50
+ EnvKey :: from ( key. into ( ) )
51
+ }
52
+ }
53
+
49
54
// Comparing Windows environment variable keys[1] are behaviourally the
50
55
// composition of two operations[2]:
51
56
//
@@ -100,6 +105,20 @@ impl PartialEq for EnvKey {
100
105
}
101
106
}
102
107
}
108
+ impl PartialOrd < str > for EnvKey {
109
+ fn partial_cmp ( & self , other : & str ) -> Option < cmp:: Ordering > {
110
+ Some ( self . cmp ( & EnvKey :: new ( other) ) )
111
+ }
112
+ }
113
+ impl PartialEq < str > for EnvKey {
114
+ fn eq ( & self , other : & str ) -> bool {
115
+ if self . os_string . len ( ) != other. len ( ) {
116
+ false
117
+ } else {
118
+ self . cmp ( & EnvKey :: new ( other) ) == cmp:: Ordering :: Equal
119
+ }
120
+ }
121
+ }
103
122
104
123
// Environment variable keys should preserve their original case even though
105
124
// they are compared using a caseless string mapping.
@@ -115,9 +134,9 @@ impl From<EnvKey> for OsString {
115
134
}
116
135
}
117
136
118
- impl Borrow < OsStr > for EnvKey {
119
- fn borrow ( & self ) -> & OsStr {
120
- & self . os_string
137
+ impl From < & OsStr > for EnvKey {
138
+ fn from ( k : & OsStr ) -> Self {
139
+ Self :: from ( k . to_os_string ( ) )
121
140
}
122
141
}
123
142
@@ -242,7 +261,7 @@ impl Command {
242
261
// to read the *child's* PATH if one is provided. See #15149 for more
243
262
// details.
244
263
let program = maybe_env. as_ref ( ) . and_then ( |env| {
245
- if let Some ( v) = env. get ( OsStr :: new ( "PATH" ) ) {
264
+ if let Some ( v) = env. get ( & EnvKey :: new ( "PATH" ) ) {
246
265
// Split the value and test each path to see if the
247
266
// program exists.
248
267
for path in split_paths ( & v) {
0 commit comments