33#[ cfg( test) ]
44mod tests;
55
6- use crate :: borrow:: Borrow ;
76use crate :: cmp;
87use crate :: collections:: BTreeMap ;
98use crate :: convert:: { TryFrom , TryInto } ;
@@ -46,6 +45,12 @@ pub struct EnvKey {
4645 utf16 : Vec < u16 > ,
4746}
4847
48+ impl EnvKey {
49+ fn new < T : Into < OsString > > ( key : T ) -> Self {
50+ EnvKey :: from ( key. into ( ) )
51+ }
52+ }
53+
4954// Comparing Windows environment variable keys[1] are behaviourally the
5055// composition of two operations[2]:
5156//
@@ -100,6 +105,20 @@ impl PartialEq for EnvKey {
100105 }
101106 }
102107}
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+ }
103122
104123// Environment variable keys should preserve their original case even though
105124// they are compared using a caseless string mapping.
@@ -115,9 +134,9 @@ impl From<EnvKey> for OsString {
115134 }
116135}
117136
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 ( ) )
121140 }
122141}
123142
@@ -242,7 +261,7 @@ impl Command {
242261 // to read the *child's* PATH if one is provided. See #15149 for more
243262 // details.
244263 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" ) ) {
246265 // Split the value and test each path to see if the
247266 // program exists.
248267 for path in split_paths ( & v) {
0 commit comments