@@ -9,17 +9,16 @@ mod tests;
99use crate :: ffi:: OsString ;
1010use crate :: fmt;
1111use crate :: io;
12- use crate :: marker:: PhantomData ;
1312use crate :: num:: NonZeroU16 ;
1413use crate :: os:: windows:: prelude:: * ;
1514use crate :: path:: PathBuf ;
16- use crate :: ptr:: NonNull ;
1715use crate :: sys:: c;
1816use crate :: sys:: process:: ensure_no_nuls;
1917use crate :: sys:: windows:: os:: current_exe;
18+ use crate :: sys_common:: wstr:: WStrUnits ;
2019use crate :: vec;
2120
22- use core :: iter;
21+ use crate :: iter;
2322
2423/// This is the const equivalent to `NonZeroU16::new(n).unwrap()`
2524///
@@ -199,55 +198,6 @@ impl ExactSizeIterator for Args {
199198 }
200199}
201200
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-
251201#[ derive( Debug ) ]
252202pub ( crate ) enum Arg {
253203 /// Add quotes (if needed)
0 commit comments