@@ -85,25 +85,69 @@ pub fn error_string(mut errnum: i32) -> String {
8585
8686pub struct Env {
8787 base : c:: LPWCH ,
88- cur : c:: LPWCH ,
88+ iter : EnvIterator ,
89+ }
90+
91+ // FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
92+ pub struct EnvStrDebug < ' a > {
93+ iter : & ' a EnvIterator ,
94+ }
95+
96+ impl fmt:: Debug for EnvStrDebug < ' _ > {
97+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
98+ let Self { iter } = self ;
99+ let iter: EnvIterator = ( * iter) . clone ( ) ;
100+ let mut list = f. debug_list ( ) ;
101+ for ( a, b) in iter {
102+ list. entry ( & ( a. to_str ( ) . unwrap ( ) , b. to_str ( ) . unwrap ( ) ) ) ;
103+ }
104+ list. finish ( )
105+ }
106+ }
107+
108+ impl Env {
109+ pub fn str_debug ( & self ) -> impl fmt:: Debug + ' _ {
110+ let Self { base : _, iter } = self ;
111+ EnvStrDebug { iter }
112+ }
113+ }
114+
115+ impl fmt:: Debug for Env {
116+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
117+ let Self { base : _, iter } = self ;
118+ f. debug_list ( ) . entries ( iter. clone ( ) ) . finish ( )
119+ }
89120}
90121
91122impl Iterator for Env {
92123 type Item = ( OsString , OsString ) ;
93124
94125 fn next ( & mut self ) -> Option < ( OsString , OsString ) > {
126+ let Self { base : _, iter } = self ;
127+ iter. next ( )
128+ }
129+ }
130+
131+ #[ derive( Clone ) ]
132+ struct EnvIterator ( c:: LPWCH ) ;
133+
134+ impl Iterator for EnvIterator {
135+ type Item = ( OsString , OsString ) ;
136+
137+ fn next ( & mut self ) -> Option < ( OsString , OsString ) > {
138+ let Self ( cur) = self ;
95139 loop {
96140 unsafe {
97- if * self . cur == 0 {
141+ if * * cur == 0 {
98142 return None ;
99143 }
100- let p = self . cur as * const u16 ;
144+ let p = * cur as * const u16 ;
101145 let mut len = 0 ;
102146 while * p. add ( len) != 0 {
103147 len += 1 ;
104148 }
105149 let s = slice:: from_raw_parts ( p, len) ;
106- self . cur = self . cur . add ( len + 1 ) ;
150+ * cur = cur. add ( len + 1 ) ;
107151
108152 // Windows allows environment variables to start with an equals
109153 // symbol (in any other position, this is the separator between
@@ -137,7 +181,7 @@ pub fn env() -> Env {
137181 if ch. is_null ( ) {
138182 panic ! ( "failure getting env string from OS: {}" , io:: Error :: last_os_error( ) ) ;
139183 }
140- Env { base : ch, cur : ch }
184+ Env { base : ch, iter : EnvIterator ( ch ) }
141185 }
142186}
143187
0 commit comments