1
1
use crate :: ffi:: OsString ;
2
2
use crate :: fmt;
3
+ use crate :: num:: NonZero ;
3
4
use crate :: sys:: os_str;
4
5
use crate :: sys:: pal:: { WORD_SIZE , abi} ;
5
6
use crate :: sys_common:: FromInner ;
6
7
8
+ #[ derive( Clone ) ]
7
9
pub struct Args {
8
- i_forward : usize ,
9
- i_back : usize ,
10
- count : usize ,
10
+ front : usize ,
11
+ back : usize ,
11
12
}
12
13
13
14
pub fn args ( ) -> Args {
14
15
let count = unsafe { abi:: sys_argc ( ) } ;
15
- Args { i_forward : 0 , i_back : 0 , count }
16
+ Args { front : 0 , back : count }
16
17
}
17
18
18
19
impl Args {
@@ -38,44 +39,80 @@ impl Args {
38
39
}
39
40
}
40
41
42
+ impl !Send for Args { }
43
+ impl !Sync for Args { }
44
+
41
45
impl fmt:: Debug for Args {
42
46
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
43
- f. debug_list ( ) . finish ( )
47
+ f. debug_list ( ) . entries ( self . clone ( ) ) . finish ( )
44
48
}
45
49
}
46
50
47
51
impl Iterator for Args {
48
52
type Item = OsString ;
49
53
54
+ #[ inline]
50
55
fn next ( & mut self ) -> Option < OsString > {
51
- if self . i_forward >= self . count - self . i_back {
56
+ if self . front == self . back {
52
57
None
53
58
} else {
54
- let arg = Self :: argv ( self . i_forward ) ;
55
- self . i_forward += 1 ;
59
+ let arg = Self :: argv ( self . front ) ;
60
+ self . front += 1 ;
56
61
Some ( arg)
57
62
}
58
63
}
59
64
65
+ #[ inline]
60
66
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
61
- ( self . count , Some ( self . count ) )
67
+ let len = self . len ( ) ;
68
+ ( len, Some ( len) )
62
69
}
63
- }
64
70
65
- impl ExactSizeIterator for Args {
66
- fn len ( & self ) -> usize {
67
- self . count
71
+ #[ inline]
72
+ fn count ( self ) -> usize {
73
+ self . len ( )
74
+ }
75
+
76
+ #[ inline]
77
+ fn last ( mut self ) -> Option < OsString > {
78
+ self . next_back ( )
79
+ }
80
+
81
+ #[ inline]
82
+ fn advance_by ( & mut self , n : usize ) -> Result < ( ) , NonZero < usize > > {
83
+ let step_size = self . len ( ) . min ( n) ;
84
+ self . front += step_size;
85
+ NonZero :: new ( n - step_size) . map_or ( Ok ( ( ) ) , Err )
68
86
}
69
87
}
70
88
71
89
impl DoubleEndedIterator for Args {
90
+ #[ inline]
72
91
fn next_back ( & mut self ) -> Option < OsString > {
73
- if self . i_back >= self . count - self . i_forward {
92
+ if self . back == self . front {
74
93
None
75
94
} else {
76
- let arg = Self :: argv ( self . count - 1 - self . i_back ) ;
77
- self . i_back += 1 ;
78
- Some ( arg)
95
+ self . back -= 1 ;
96
+ Some ( Self :: argv ( self . back ) )
79
97
}
80
98
}
99
+
100
+ #[ inline]
101
+ fn advance_back_by ( & mut self , n : usize ) -> Result < ( ) , NonZero < usize > > {
102
+ let step_size = self . len ( ) . min ( n) ;
103
+ self . back -= step_size;
104
+ NonZero :: new ( n - step_size) . map_or ( Ok ( ( ) ) , Err )
105
+ }
106
+ }
107
+
108
+ impl ExactSizeIterator for Args {
109
+ #[ inline]
110
+ fn len ( & self ) -> usize {
111
+ self . back - self . front
112
+ }
113
+
114
+ #[ inline]
115
+ fn is_empty ( & self ) -> bool {
116
+ self . front == self . back
117
+ }
81
118
}
0 commit comments