10
10
11
11
//! Integer trait and functions.
12
12
13
- use { Num , Signed } ;
13
+ extern crate num_traits as traits;
14
+
15
+ use traits:: { Num , Signed } ;
14
16
15
17
pub trait Integer
16
18
: Sized
@@ -179,7 +181,7 @@ macro_rules! impl_integer_for_isize {
179
181
impl Integer for $T {
180
182
/// Floored integer division
181
183
#[ inline]
182
- fn div_floor( & self , other: & $T ) -> $T {
184
+ fn div_floor( & self , other: & Self ) -> Self {
183
185
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
184
186
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
185
187
match self . div_rem( other) {
@@ -191,7 +193,7 @@ macro_rules! impl_integer_for_isize {
191
193
192
194
/// Floored integer modulo
193
195
#[ inline]
194
- fn mod_floor( & self , other: & $T ) -> $T {
196
+ fn mod_floor( & self , other: & Self ) -> Self {
195
197
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
196
198
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
197
199
match * self % * other {
@@ -203,7 +205,7 @@ macro_rules! impl_integer_for_isize {
203
205
204
206
/// Calculates `div_floor` and `mod_floor` simultaneously
205
207
#[ inline]
206
- fn div_mod_floor( & self , other: & $T ) -> ( $T , $T ) {
208
+ fn div_mod_floor( & self , other: & Self ) -> ( Self , Self ) {
207
209
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
208
210
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
209
211
match self . div_rem( other) {
@@ -216,7 +218,7 @@ macro_rules! impl_integer_for_isize {
216
218
/// Calculates the Greatest Common Divisor (GCD) of the number and
217
219
/// `other`. The result is always positive.
218
220
#[ inline]
219
- fn gcd( & self , other: & $T ) -> $T {
221
+ fn gcd( & self , other: & Self ) -> Self {
220
222
// Use Stein's algorithm
221
223
let mut m = * self ;
222
224
let mut n = * other;
@@ -233,7 +235,7 @@ macro_rules! impl_integer_for_isize {
233
235
// Assuming two's complement, the number created by the shift
234
236
// is positive for all numbers except gcd = abs(min value)
235
237
// The call to .abs() causes a panic in debug mode
236
- if m == <$T> :: min_value( ) || n == <$T> :: min_value( ) {
238
+ if m == Self :: min_value( ) || n == Self :: min_value( ) {
237
239
return ( 1 << shift) . abs( )
238
240
}
239
241
@@ -257,18 +259,22 @@ macro_rules! impl_integer_for_isize {
257
259
/// Calculates the Lowest Common Multiple (LCM) of the number and
258
260
/// `other`.
259
261
#[ inline]
260
- fn lcm( & self , other: & $T ) -> $T {
262
+ fn lcm( & self , other: & Self ) -> Self {
261
263
// should not have to recalculate abs
262
264
( ( * self * * other) / self . gcd( other) ) . abs( )
263
265
}
264
266
265
267
/// Deprecated, use `is_multiple_of` instead.
266
268
#[ inline]
267
- fn divides( & self , other: & $T) -> bool { return self . is_multiple_of( other) ; }
269
+ fn divides( & self , other: & Self ) -> bool {
270
+ self . is_multiple_of( other)
271
+ }
268
272
269
273
/// Returns `true` if the number is a multiple of `other`.
270
274
#[ inline]
271
- fn is_multiple_of( & self , other: & $T) -> bool { * self % * other == 0 }
275
+ fn is_multiple_of( & self , other: & Self ) -> bool {
276
+ * self % * other == 0
277
+ }
272
278
273
279
/// Returns `true` if the number is divisible by `2`
274
280
#[ inline]
@@ -280,7 +286,7 @@ macro_rules! impl_integer_for_isize {
280
286
281
287
/// Simultaneous truncated integer division and modulus.
282
288
#[ inline]
283
- fn div_rem( & self , other: & $T ) -> ( $T , $T ) {
289
+ fn div_rem( & self , other: & Self ) -> ( Self , Self ) {
284
290
( * self / * other, * self % * other)
285
291
}
286
292
}
@@ -295,7 +301,7 @@ macro_rules! impl_integer_for_isize {
295
301
/// - `d`: denominator (divisor)
296
302
/// - `qr`: quotient and remainder
297
303
#[ cfg( test) ]
298
- fn test_division_rule( ( n, d) : ( $T, $T) , ( q, r) : ( $T, $T) ) {
304
+ fn test_division_rule( ( n, d) : ( $T, $T) , ( q, r) : ( $T, $T) ) {
299
305
assert_eq!( d * q + r, n) ;
300
306
}
301
307
@@ -475,15 +481,19 @@ macro_rules! impl_integer_for_usize {
475
481
impl Integer for $T {
476
482
/// Unsigned integer division. Returns the same result as `div` (`/`).
477
483
#[ inline]
478
- fn div_floor( & self , other: & $T) -> $T { * self / * other }
484
+ fn div_floor( & self , other: & Self ) -> Self {
485
+ * self / * other
486
+ }
479
487
480
488
/// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
481
489
#[ inline]
482
- fn mod_floor( & self , other: & $T) -> $T { * self % * other }
490
+ fn mod_floor( & self , other: & Self ) -> Self {
491
+ * self % * other
492
+ }
483
493
484
494
/// Calculates the Greatest Common Divisor (GCD) of the number and `other`
485
495
#[ inline]
486
- fn gcd( & self , other: & $T ) -> $T {
496
+ fn gcd( & self , other: & Self ) -> Self {
487
497
// Use Stein's algorithm
488
498
let mut m = * self ;
489
499
let mut n = * other;
@@ -507,29 +517,37 @@ macro_rules! impl_integer_for_usize {
507
517
508
518
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
509
519
#[ inline]
510
- fn lcm( & self , other: & $T ) -> $T {
520
+ fn lcm( & self , other: & Self ) -> Self {
511
521
( * self * * other) / self . gcd( other)
512
522
}
513
523
514
524
/// Deprecated, use `is_multiple_of` instead.
515
525
#[ inline]
516
- fn divides( & self , other: & $T) -> bool { return self . is_multiple_of( other) ; }
526
+ fn divides( & self , other: & Self ) -> bool {
527
+ self . is_multiple_of( other)
528
+ }
517
529
518
530
/// Returns `true` if the number is a multiple of `other`.
519
531
#[ inline]
520
- fn is_multiple_of( & self , other: & $T) -> bool { * self % * other == 0 }
532
+ fn is_multiple_of( & self , other: & Self ) -> bool {
533
+ * self % * other == 0
534
+ }
521
535
522
536
/// Returns `true` if the number is divisible by `2`.
523
537
#[ inline]
524
- fn is_even( & self ) -> bool { ( * self ) & 1 == 0 }
538
+ fn is_even( & self ) -> bool {
539
+ * self % 2 == 0
540
+ }
525
541
526
542
/// Returns `true` if the number is not divisible by `2`.
527
543
#[ inline]
528
- fn is_odd( & self ) -> bool { !( * self ) . is_even( ) }
544
+ fn is_odd( & self ) -> bool {
545
+ !self . is_even( )
546
+ }
529
547
530
548
/// Simultaneous truncated integer division and modulus.
531
549
#[ inline]
532
- fn div_rem( & self , other: & $T ) -> ( $T , $T ) {
550
+ fn div_rem( & self , other: & Self ) -> ( Self , Self ) {
533
551
( * self / * other, * self % * other)
534
552
}
535
553
}
0 commit comments