@@ -9,17 +9,16 @@ mod tests;
9
9
use crate :: ffi:: OsString ;
10
10
use crate :: fmt;
11
11
use crate :: io;
12
- use crate :: marker:: PhantomData ;
13
12
use crate :: num:: NonZeroU16 ;
14
13
use crate :: os:: windows:: prelude:: * ;
15
14
use crate :: path:: PathBuf ;
16
- use crate :: ptr:: NonNull ;
17
15
use crate :: sys:: c;
18
16
use crate :: sys:: process:: ensure_no_nuls;
19
17
use crate :: sys:: windows:: os:: current_exe;
18
+ use crate :: sys_common:: wstr:: WStrUnits ;
20
19
use crate :: vec;
21
20
22
- use core :: iter;
21
+ use crate :: iter;
23
22
24
23
/// This is the const equivalent to `NonZeroU16::new(n).unwrap()`
25
24
///
@@ -199,55 +198,6 @@ impl ExactSizeIterator for Args {
199
198
}
200
199
}
201
200
202
- /// A safe iterator over a LPWSTR
203
- /// (aka a pointer to a series of UTF-16 code units terminated by a NULL).
204
- struct WStrUnits < ' a > {
205
- // The pointer must never be null...
206
- lpwstr : NonNull < u16 > ,
207
- // ...and the memory it points to must be valid for this lifetime.
208
- lifetime : PhantomData < & ' a [ u16 ] > ,
209
- }
210
- impl WStrUnits < ' _ > {
211
- /// Create the iterator. Returns `None` if `lpwstr` is null.
212
- ///
213
- /// SAFETY: `lpwstr` must point to a null-terminated wide string that lives
214
- /// at least as long as the lifetime of this struct.
215
- unsafe fn new ( lpwstr : * const u16 ) -> Option < Self > {
216
- Some ( Self { lpwstr : NonNull :: new ( lpwstr as _ ) ?, lifetime : PhantomData } )
217
- }
218
- fn peek ( & self ) -> Option < NonZeroU16 > {
219
- // SAFETY: It's always safe to read the current item because we don't
220
- // ever move out of the array's bounds.
221
- unsafe { NonZeroU16 :: new ( * self . lpwstr . as_ptr ( ) ) }
222
- }
223
- /// Advance the iterator while `predicate` returns true.
224
- /// Returns the number of items it advanced by.
225
- fn advance_while < P : FnMut ( NonZeroU16 ) -> bool > ( & mut self , mut predicate : P ) -> usize {
226
- let mut counter = 0 ;
227
- while let Some ( w) = self . peek ( ) {
228
- if !predicate ( w) {
229
- break ;
230
- }
231
- counter += 1 ;
232
- self . next ( ) ;
233
- }
234
- counter
235
- }
236
- }
237
- impl Iterator for WStrUnits < ' _ > {
238
- // This can never return zero as that marks the end of the string.
239
- type Item = NonZeroU16 ;
240
- fn next ( & mut self ) -> Option < NonZeroU16 > {
241
- // SAFETY: If NULL is reached we immediately return.
242
- // Therefore it's safe to advance the pointer after that.
243
- unsafe {
244
- let next = self . peek ( ) ?;
245
- self . lpwstr = NonNull :: new_unchecked ( self . lpwstr . as_ptr ( ) . add ( 1 ) ) ;
246
- Some ( next)
247
- }
248
- }
249
- }
250
-
251
201
#[ derive( Debug ) ]
252
202
pub ( crate ) enum Arg {
253
203
/// Add quotes (if needed)
0 commit comments