@@ -15,12 +15,12 @@ pub trait Euclid: Sized + Div<Self, Output = Self> + Rem<Self, Output = Self> {
15
15
///
16
16
/// let a: i32 = 7;
17
17
/// let b: i32 = 4;
18
- /// assert_eq!(Euclid::div_euclid(a, b), 1); // 7 > 4 * 1
19
- /// assert_eq!(Euclid::div_euclid(-a, b), -2); // -7 >= 4 * -2
20
- /// assert_eq!(Euclid::div_euclid(a, -b), -1); // 7 >= -4 * -1
21
- /// assert_eq!(Euclid::div_euclid(-a, -b), 2); // -7 >= -4 * 2
18
+ /// assert_eq!(Euclid::div_euclid(& a, & b), 1); // 7 > 4 * 1
19
+ /// assert_eq!(Euclid::div_euclid(& -a, & b), -2); // -7 >= 4 * -2
20
+ /// assert_eq!(Euclid::div_euclid(& a, & -b), -1); // 7 >= -4 * -1
21
+ /// assert_eq!(Euclid::div_euclid(& -a, & -b), 2); // -7 >= -4 * 2
22
22
/// ```
23
- fn div_euclid ( self , v : Self ) -> Self ;
23
+ fn div_euclid ( & self , v : & Self ) -> Self ;
24
24
25
25
/// Calculates the least nonnegative remainder of `self (mod v)`.
26
26
///
@@ -40,30 +40,31 @@ pub trait Euclid: Sized + Div<Self, Output = Self> + Rem<Self, Output = Self> {
40
40
///
41
41
/// let a: i32 = 7;
42
42
/// let b: i32 = 4;
43
- /// assert_eq!(Euclid::rem_euclid(a, b), 3);
44
- /// assert_eq!(Euclid::rem_euclid(-a, b), 1);
45
- /// assert_eq!(Euclid::rem_euclid(a, -b), 3);
46
- /// assert_eq!(Euclid::rem_euclid(-a, -b), 1);
43
+ /// assert_eq!(Euclid::rem_euclid(& a, & b), 3);
44
+ /// assert_eq!(Euclid::rem_euclid(& -a, & b), 1);
45
+ /// assert_eq!(Euclid::rem_euclid(& a, & -b), 3);
46
+ /// assert_eq!(Euclid::rem_euclid(& -a, & -b), 1);
47
47
/// ```
48
- fn rem_euclid ( self , v : Self ) -> Self ;
48
+ fn rem_euclid ( & self , v : & Self ) -> Self ;
49
49
}
50
+
50
51
macro_rules! euclid_int_impl {
51
52
( $( $t: ty) * ) => { $(
52
53
impl Euclid for $t {
53
54
#[ inline]
54
- fn div_euclid( self , v: $t) -> Self {
55
+ fn div_euclid( & self , v: & $t) -> Self {
55
56
let q = self / v;
56
57
if self % v < 0 {
57
- return if v > 0 { q - 1 } else { q + 1 }
58
+ return if * v > 0 { q - 1 } else { q + 1 }
58
59
}
59
60
q
60
61
}
61
62
62
63
#[ inline]
63
- fn rem_euclid( self , v: $t) -> Self {
64
+ fn rem_euclid( & self , v: & $t) -> Self {
64
65
let r = self % v;
65
66
if r < 0 {
66
- if v < 0 {
67
+ if * v < 0 {
67
68
r - v
68
69
} else {
69
70
r + v
@@ -75,21 +76,23 @@ macro_rules! euclid_int_impl {
75
76
}
76
77
) * }
77
78
}
79
+
78
80
macro_rules! euclid_uint_impl {
79
81
( $( $t: ty) * ) => { $(
80
82
impl Euclid for $t {
81
83
#[ inline]
82
- fn div_euclid( self , v: $t) -> Self {
84
+ fn div_euclid( & self , v: & $t) -> Self {
83
85
self / v
84
86
}
85
87
86
88
#[ inline]
87
- fn rem_euclid( self , v: $t) -> Self {
89
+ fn rem_euclid( & self , v: & $t) -> Self {
88
90
self % v
89
91
}
90
92
}
91
93
) * }
92
94
}
95
+
93
96
euclid_int_impl ! ( isize i8 i16 i32 i64 ) ;
94
97
euclid_uint_impl ! ( usize u8 u16 u32 u64 ) ;
95
98
#[ cfg( has_i128) ]
@@ -99,19 +102,19 @@ euclid_uint_impl!(u128);
99
102
100
103
impl Euclid for f32 {
101
104
#[ inline]
102
- fn div_euclid ( self , v : f32 ) -> f32 {
103
- let q = <f32 as :: FloatCore >:: trunc ( self / v) ;
105
+ fn div_euclid ( & self , v : & f32 ) -> f32 {
106
+ let q = <f32 as :: float :: FloatCore >:: trunc ( self / v) ;
104
107
if self % v < 0.0 {
105
- return if v > 0.0 { q - 1.0 } else { q + 1.0 } ;
108
+ return if * v > 0.0 { q - 1.0 } else { q + 1.0 } ;
106
109
}
107
110
q
108
111
}
109
112
110
113
#[ inline]
111
- fn rem_euclid ( self , v : f32 ) -> f32 {
114
+ fn rem_euclid ( & self , v : & f32 ) -> f32 {
112
115
let r = self % v;
113
116
if r < 0.0 {
114
- r + <f32 as :: FloatCore >:: abs ( v)
117
+ r + <f32 as :: float :: FloatCore >:: abs ( * v)
115
118
} else {
116
119
r
117
120
}
@@ -120,19 +123,19 @@ impl Euclid for f32 {
120
123
121
124
impl Euclid for f64 {
122
125
#[ inline]
123
- fn div_euclid ( self , v : f64 ) -> f64 {
124
- let q = <f64 as :: FloatCore >:: trunc ( self / v) ;
126
+ fn div_euclid ( & self , v : & f64 ) -> f64 {
127
+ let q = <f64 as :: float :: FloatCore >:: trunc ( self / v) ;
125
128
if self % v < 0.0 {
126
- return if v > 0.0 { q - 1.0 } else { q + 1.0 } ;
129
+ return if * v > 0.0 { q - 1.0 } else { q + 1.0 } ;
127
130
}
128
131
q
129
132
}
130
133
131
134
#[ inline]
132
- fn rem_euclid ( self , v : f64 ) -> f64 {
135
+ fn rem_euclid ( & self , v : & f64 ) -> f64 {
133
136
let r = self % v;
134
137
if r < 0.0 {
135
- r + <f64 as :: FloatCore >:: abs ( v)
138
+ r + <f64 as :: float :: FloatCore >:: abs ( * v)
136
139
} else {
137
140
r
138
141
}
@@ -142,27 +145,28 @@ impl Euclid for f64 {
142
145
pub trait CheckedEuclid : Euclid {
143
146
/// Performs euclid division that returns `None` instead of panicking on division by zero
144
147
/// and instead of wrapping around on underflow and overflow.
145
- fn checked_div_euclid ( self , v : Self ) -> Option < Self > ;
148
+ fn checked_div_euclid ( & self , v : & Self ) -> Option < Self > ;
146
149
147
150
/// Finds the euclid remainder of dividing two numbers, checking for underflow, overflow and
148
151
/// division by zero. If any of that happens, `None` is returned.
149
- fn checked_rem_euclid ( self , v : Self ) -> Option < Self > ;
152
+ fn checked_rem_euclid ( & self , v : & Self ) -> Option < Self > ;
150
153
}
154
+
151
155
macro_rules! checked_euclid_int_impl {
152
156
( $( $t: ty) * ) => { $(
153
157
impl CheckedEuclid for $t {
154
158
#[ inline]
155
- fn checked_div_euclid( self , v: $t) -> Option <$t> {
156
- if v == 0 || ( self == Self :: min_value( ) && v == -1 ) {
159
+ fn checked_div_euclid( & self , v: & $t) -> Option <$t> {
160
+ if * v == 0 || ( * self == Self :: min_value( ) && * v == -1 ) {
157
161
None
158
162
} else {
159
163
Some ( Euclid :: div_euclid( self , v) )
160
164
}
161
165
}
162
166
163
167
#[ inline]
164
- fn checked_rem_euclid( self , v: $t) -> Option <$t> {
165
- if v == 0 || ( self == Self :: min_value( ) && v == -1 ) {
168
+ fn checked_rem_euclid( & self , v: & $t) -> Option <$t> {
169
+ if * v == 0 || ( * self == Self :: min_value( ) && * v == -1 ) {
166
170
None
167
171
} else {
168
172
Some ( Euclid :: rem_euclid( self , v) )
@@ -171,21 +175,22 @@ macro_rules! checked_euclid_int_impl {
171
175
}
172
176
) * }
173
177
}
178
+
174
179
macro_rules! checked_euclid_uint_impl {
175
180
( $( $t: ty) * ) => { $(
176
181
impl CheckedEuclid for $t {
177
182
#[ inline]
178
- fn checked_div_euclid( self , v: $t) -> Option <$t> {
179
- if v == 0 {
183
+ fn checked_div_euclid( & self , v: & $t) -> Option <$t> {
184
+ if * v == 0 {
180
185
None
181
186
} else {
182
187
Some ( Euclid :: div_euclid( self , v) )
183
188
}
184
189
}
185
190
186
191
#[ inline]
187
- fn checked_rem_euclid( self , v: $t) -> Option <$t> {
188
- if v == 0 {
192
+ fn checked_rem_euclid( & self , v: & $t) -> Option <$t> {
193
+ if * v == 0 {
189
194
None
190
195
} else {
191
196
Some ( Euclid :: rem_euclid( self , v) )
@@ -194,6 +199,7 @@ macro_rules! checked_euclid_uint_impl {
194
199
}
195
200
) * }
196
201
}
202
+
197
203
checked_euclid_int_impl ! ( isize i8 i16 i32 i64 ) ;
198
204
checked_euclid_uint_impl ! ( usize u8 u16 u32 u64 ) ;
199
205
#[ cfg( has_i128) ]
@@ -213,8 +219,8 @@ mod tests {
213
219
{
214
220
let x: $t = 10 ;
215
221
let y: $t = 3 ;
216
- assert_eq!( Euclid :: div_euclid( x, y) , 3 ) ;
217
- assert_eq!( Euclid :: rem_euclid( x, y) , 1 ) ;
222
+ assert_eq!( Euclid :: div_euclid( & x, & y) , 3 ) ;
223
+ assert_eq!( Euclid :: rem_euclid( & x, & y) , 1 ) ;
218
224
}
219
225
) +
220
226
} ;
@@ -231,13 +237,13 @@ mod tests {
231
237
{
232
238
let x: $t = 10 ;
233
239
let y: $t = -3 ;
234
- assert_eq!( Euclid :: div_euclid( x, y) , -3 ) ;
235
- assert_eq!( Euclid :: div_euclid( -x, y) , 4 ) ;
236
- assert_eq!( Euclid :: rem_euclid( x, y) , 1 ) ;
237
- assert_eq!( Euclid :: rem_euclid( -x, y) , 2 ) ;
238
- let x: $t = $t:: min_value( ) + 1 ;
240
+ assert_eq!( Euclid :: div_euclid( & x, & y) , -3 ) ;
241
+ assert_eq!( Euclid :: div_euclid( & -x, & y) , 4 ) ;
242
+ assert_eq!( Euclid :: rem_euclid( & x, & y) , 1 ) ;
243
+ assert_eq!( Euclid :: rem_euclid( & -x, & y) , 2 ) ;
244
+ let x: $t = $t:: min_value( ) + 1 ;
239
245
let y: $t = -1 ;
240
- assert_eq!( Euclid :: div_euclid( x, y) , $t:: max_value( ) ) ;
246
+ assert_eq!( Euclid :: div_euclid( & x, & y) , $t:: max_value( ) ) ;
241
247
}
242
248
) +
243
249
} ;
@@ -254,14 +260,14 @@ mod tests {
254
260
{
255
261
let x: $t = 12.1 ;
256
262
let y: $t = 3.2 ;
257
- assert!( Euclid :: div_euclid( x, y ) * y+ Euclid :: rem_euclid( x, y ) - x
258
- <=46.4 * <$t as :: FloatCore >:: epsilon( ) ) ;
259
- assert!( Euclid :: div_euclid( x, -y) * -y+ Euclid :: rem_euclid( x, -y) - x
260
- <= 46.4 * <$t as :: FloatCore >:: epsilon( ) ) ;
261
- assert!( Euclid :: div_euclid( -x, y ) * y+ Euclid :: rem_euclid( -x, y ) - ( -x )
262
- <= 46.4 * <$t as :: FloatCore >:: epsilon( ) ) ;
263
- assert!( Euclid :: div_euclid( -x, -y) * -y+ Euclid :: rem_euclid( -x, -y) - ( -x )
264
- <= 46.4 * <$t as :: FloatCore >:: epsilon( ) ) ;
263
+ assert!( Euclid :: div_euclid( & x, & y ) * y + Euclid :: rem_euclid( & x, & y ) - x
264
+ <=46.4 * <$t as :: float :: FloatCore >:: epsilon( ) ) ;
265
+ assert!( Euclid :: div_euclid( & x, & -y) * -y + Euclid :: rem_euclid( & x, & -y) - x
266
+ <= 46.4 * <$t as :: float :: FloatCore >:: epsilon( ) ) ;
267
+ assert!( Euclid :: div_euclid( & -x, & y ) * y + Euclid :: rem_euclid( & -x, & y ) + x
268
+ <= 46.4 * <$t as :: float :: FloatCore >:: epsilon( ) ) ;
269
+ assert!( Euclid :: div_euclid( & -x, & -y) * -y + Euclid :: rem_euclid( & -x, & -y) + x
270
+ <= 46.4 * <$t as :: float :: FloatCore >:: epsilon( ) ) ;
265
271
}
266
272
) +
267
273
} ;
@@ -276,10 +282,10 @@ mod tests {
276
282
( $( $t: ident) +) => {
277
283
$(
278
284
{
279
- assert_eq!( CheckedEuclid :: checked_div_euclid( $t:: min_value( ) , -1 ) , None ) ;
280
- assert_eq!( CheckedEuclid :: checked_rem_euclid( $t:: min_value( ) , -1 ) , None ) ;
281
- assert_eq!( CheckedEuclid :: checked_div_euclid( 1 , 0 ) , None ) ;
282
- assert_eq!( CheckedEuclid :: checked_rem_euclid( 1 , 0 ) , None ) ;
285
+ assert_eq!( CheckedEuclid :: checked_div_euclid( & $t:: min_value( ) , & -1 ) , None ) ;
286
+ assert_eq!( CheckedEuclid :: checked_rem_euclid( & $t:: min_value( ) , & -1 ) , None ) ;
287
+ assert_eq!( CheckedEuclid :: checked_div_euclid( & 1 , & 0 ) , None ) ;
288
+ assert_eq!( CheckedEuclid :: checked_rem_euclid( & 1 , & 0 ) , None ) ;
283
289
}
284
290
) +
285
291
} ;
0 commit comments