@@ -85,25 +85,69 @@ pub fn error_string(mut errnum: i32) -> String {
85
85
86
86
pub struct Env {
87
87
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
+ }
89
120
}
90
121
91
122
impl Iterator for Env {
92
123
type Item = ( OsString , OsString ) ;
93
124
94
125
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 ;
95
139
loop {
96
140
unsafe {
97
- if * self . cur == 0 {
141
+ if * * cur == 0 {
98
142
return None ;
99
143
}
100
- let p = self . cur as * const u16 ;
144
+ let p = * cur as * const u16 ;
101
145
let mut len = 0 ;
102
146
while * p. add ( len) != 0 {
103
147
len += 1 ;
104
148
}
105
149
let s = slice:: from_raw_parts ( p, len) ;
106
- self . cur = self . cur . add ( len + 1 ) ;
150
+ * cur = cur. add ( len + 1 ) ;
107
151
108
152
// Windows allows environment variables to start with an equals
109
153
// symbol (in any other position, this is the separator between
@@ -137,7 +181,7 @@ pub fn env() -> Env {
137
181
if ch. is_null ( ) {
138
182
panic ! ( "failure getting env string from OS: {}" , io:: Error :: last_os_error( ) ) ;
139
183
}
140
- Env { base : ch, cur : ch }
184
+ Env { base : ch, iter : EnvIterator ( ch ) }
141
185
}
142
186
}
143
187
0 commit comments