1
- #![ cfg_attr( feature = "nightly" , feature( step_trait) ) ]
1
+ #![ cfg_attr( feature = "nightly" , feature( step_trait, step_trait_ext ) ) ]
2
2
3
3
#[ cfg( feature = "derive" ) ]
4
4
#[ macro_use]
@@ -9,8 +9,6 @@ use serde::{Deserialize, Serialize};
9
9
use std:: marker:: PhantomData ;
10
10
use std:: path:: PathBuf ;
11
11
12
- #[ cfg( feature = "nightly" ) ]
13
- use std:: convert:: TryFrom ;
14
12
#[ cfg( feature = "nightly" ) ]
15
13
use std:: iter:: Step ;
16
14
@@ -86,62 +84,30 @@ impl Column<ZeroIndexed> {
86
84
}
87
85
88
86
#[ cfg( feature = "nightly" ) ]
89
- impl Step for Column < ZeroIndexed > {
90
- fn steps_between ( start : & Self , end : & Self ) -> Option < usize > {
91
- <u32 as Step >:: steps_between ( & start. 0 , & end. 0 )
92
- }
93
-
94
- fn replace_one ( & mut self ) -> Self {
95
- self . 0 = 1 ;
96
- self . clone ( )
97
- }
98
-
99
- fn replace_zero ( & mut self ) -> Self {
100
- self . 0 = 0 ;
101
- self . clone ( )
102
- }
103
-
104
- fn add_one ( & self ) -> Self {
105
- Self :: new ( self . 0 + 1 )
106
- }
107
-
108
- fn sub_one ( & self ) -> Self {
109
- Self :: new ( self . 0 - 1 )
110
- }
111
-
112
- fn add_usize ( & self , n : usize ) -> Option < Self > {
113
- ( self . 0 as usize ) . checked_add ( n) . and_then ( |n| u32:: try_from ( n) . ok ( ) ) . map ( Self :: new)
114
- }
87
+ macro_rules! impl_step {
88
+ ( $target: ty) => {
89
+ unsafe impl Step for $target {
90
+ fn steps_between( start: & Self , end: & Self ) -> Option <usize > {
91
+ Step :: steps_between( & start. 0 , & end. 0 )
92
+ }
93
+ fn forward_checked( arg: Self , count: usize ) -> Option <Self > {
94
+ Step :: forward_checked( arg. 0 , count) . map( |x| Self ( x, PhantomData ) )
95
+ }
96
+ fn backward_checked( arg: Self , count: usize ) -> Option <Self > {
97
+ Step :: backward_checked( arg. 0 , count) . map( |x| Self ( x, PhantomData ) )
98
+ }
99
+ }
100
+ } ;
115
101
}
116
102
117
103
#[ cfg( feature = "nightly" ) ]
118
- impl Step for Column < OneIndexed > {
119
- fn steps_between ( start : & Self , end : & Self ) -> Option < usize > {
120
- <u32 as Step >:: steps_between ( & start. 0 , & end. 0 )
121
- }
122
-
123
- fn replace_one ( & mut self ) -> Self {
124
- self . 0 = 2 ;
125
- self . clone ( )
126
- }
127
-
128
- fn replace_zero ( & mut self ) -> Self {
129
- self . 0 = 1 ;
130
- self . clone ( )
131
- }
132
-
133
- fn add_one ( & self ) -> Self {
134
- Self :: new ( self . 0 + 1 )
135
- }
136
-
137
- fn sub_one ( & self ) -> Self {
138
- Self :: new ( self . 0 - 1 )
139
- }
140
-
141
- fn add_usize ( & self , n : usize ) -> Option < Self > {
142
- ( self . 0 as usize ) . checked_add ( n) . and_then ( |n| u32:: try_from ( n) . ok ( ) ) . map ( Self :: new)
143
- }
144
- }
104
+ impl_step ! ( Column <ZeroIndexed >) ;
105
+ #[ cfg( feature = "nightly" ) ]
106
+ impl_step ! ( Column <OneIndexed >) ;
107
+ #[ cfg( feature = "nightly" ) ]
108
+ impl_step ! ( Row <ZeroIndexed >) ;
109
+ #[ cfg( feature = "nightly" ) ]
110
+ impl_step ! ( Row <OneIndexed >) ;
145
111
146
112
#[ derive( Debug , Hash , PartialEq , Eq , PartialOrd , Ord ) ]
147
113
pub struct Row < I : Indexed > ( pub u32 , PhantomData < I > ) ;
@@ -206,64 +172,6 @@ impl Row<ZeroIndexed> {
206
172
}
207
173
}
208
174
209
- #[ cfg( feature = "nightly" ) ]
210
- impl Step for Row < ZeroIndexed > {
211
- fn steps_between ( start : & Self , end : & Self ) -> Option < usize > {
212
- <u32 as Step >:: steps_between ( & start. 0 , & end. 0 )
213
- }
214
-
215
- fn replace_one ( & mut self ) -> Self {
216
- self . 0 = 1 ;
217
- self . clone ( )
218
- }
219
-
220
- fn replace_zero ( & mut self ) -> Self {
221
- self . 0 = 0 ;
222
- self . clone ( )
223
- }
224
-
225
- fn add_one ( & self ) -> Self {
226
- Self :: new ( self . 0 + 1 )
227
- }
228
-
229
- fn sub_one ( & self ) -> Self {
230
- Self :: new ( self . 0 - 1 )
231
- }
232
-
233
- fn add_usize ( & self , n : usize ) -> Option < Self > {
234
- ( self . 0 as usize ) . checked_add ( n) . and_then ( |n| u32:: try_from ( n) . ok ( ) ) . map ( Self :: new)
235
- }
236
- }
237
-
238
- #[ cfg( feature = "nightly" ) ]
239
- impl Step for Row < OneIndexed > {
240
- fn steps_between ( start : & Self , end : & Self ) -> Option < usize > {
241
- <u32 as Step >:: steps_between ( & start. 0 , & end. 0 )
242
- }
243
-
244
- fn replace_one ( & mut self ) -> Self {
245
- self . 0 = 2 ;
246
- self . clone ( )
247
- }
248
-
249
- fn replace_zero ( & mut self ) -> Self {
250
- self . 0 = 1 ;
251
- self . clone ( )
252
- }
253
-
254
- fn add_one ( & self ) -> Self {
255
- Self :: new ( self . 0 + 1 )
256
- }
257
-
258
- fn sub_one ( & self ) -> Self {
259
- Self :: new ( self . 0 - 1 )
260
- }
261
-
262
- fn add_usize ( & self , n : usize ) -> Option < Self > {
263
- ( self . 0 as usize ) . checked_add ( n) . and_then ( |n| u32:: try_from ( n) . ok ( ) ) . map ( Self :: new)
264
- }
265
- }
266
-
267
175
#[ cfg_attr( feature = "derive" , derive( Serialize , Deserialize ) ) ]
268
176
#[ cfg_attr( feature = "serialize-rustc" , derive( RustcDecodable , RustcEncodable ) ) ]
269
177
#[ derive( Debug , Hash , PartialEq , Eq , PartialOrd , Ord ) ]
0 commit comments