@@ -47,11 +47,24 @@ pub trait Euclid: Sized + Div<Self, Output = Self> + Rem<Self, Output = Self> {
47
47
/// ```
48
48
fn rem_euclid ( & self , v : & Self ) -> Self ;
49
49
50
- /// Returns both the euclidian division quotien and remainer
50
+ /// Returns both the quotient and remainder from Euclidean division.
51
51
///
52
- /// By default, it internaly calls both `Euclid::div_euclid` and `Euclid::rem_euclid`,
53
- /// but it can be overwritten in order to implement some optimization.
54
- fn div_with_rem_euclid ( & self , v : & Self ) -> ( Self , Self ) {
52
+ /// By default, it internally calls both `Euclid::div_euclid` and `Euclid::rem_euclid`,
53
+ /// but it can be overidden in order to implement some optimization.
54
+ ///
55
+ /// # Examples
56
+ ///
57
+ /// ```
58
+ /// # use num_traits::Euclid;
59
+ /// let x = 5u8;
60
+ /// let y = 3u8;
61
+ ///
62
+ /// let div = Euclid::div_euclid(&x, &y);
63
+ /// let rem = Euclid::rem_euclid(&x, &y);
64
+ ///
65
+ /// assert_eq!((div, rem), Euclid::div_rem_euclid(&x, &y));
66
+ /// ```
67
+ fn div_rem_euclid ( & self , v : & Self ) -> ( Self , Self ) {
55
68
( self . div_euclid ( v) , self . rem_euclid ( v) )
56
69
}
57
70
}
@@ -183,11 +196,23 @@ pub trait CheckedEuclid: Euclid {
183
196
/// division by zero. If any of that happens, `None` is returned.
184
197
fn checked_rem_euclid ( & self , v : & Self ) -> Option < Self > ;
185
198
186
- /// Returns both the checked euclidian division quotien and remainer
199
+ /// Returns both the quotient and remainder from checked Euclidean division.
187
200
///
188
- /// By default, it internaly calls both `Euclid::div_euclid` and `Euclid::rem_euclid`,
189
- /// but it can be overwritten in order to implement some optimization.
190
- fn checked_div_with_rem_euclid ( & self , v : & Self ) -> Option < ( Self , Self ) > {
201
+ /// By default, it internally calls both `CheckedEuclid::checked_div_euclid` and `CheckedEuclid::checked_rem_euclid`,
202
+ /// but it can be overridden in order to implement some optimization.
203
+ /// # Examples
204
+ ///
205
+ /// ```
206
+ /// # use num_traits::CheckedEuclid;
207
+ /// let x = 5u8;
208
+ /// let y = 3u8;
209
+ ///
210
+ /// let div = CheckedEuclid::checked_div_euclid(&x, &y);
211
+ /// let rem = CheckedEuclid::checked_rem_euclid(&x, &y);
212
+ ///
213
+ /// assert_eq!(Some((div.unwrap(), rem.unwrap())), CheckedEuclid::checked_div_rem_euclid(&x, &y));
214
+ /// ```
215
+ fn checked_div_rem_euclid ( & self , v : & Self ) -> Option < ( Self , Self ) > {
191
216
Some ( ( self . checked_div_euclid ( v) ?, self . checked_rem_euclid ( v) ?) )
192
217
}
193
218
}
@@ -278,8 +303,11 @@ mod tests {
278
303
{
279
304
let x: $t = 10 ;
280
305
let y: $t = 3 ;
281
- assert_eq!( Euclid :: div_euclid( & x, & y) , 3 ) ;
282
- assert_eq!( Euclid :: rem_euclid( & x, & y) , 1 ) ;
306
+ let div = Euclid :: div_euclid( & x, & y) ;
307
+ let rem = Euclid :: rem_euclid( & x, & y) ;
308
+ assert_eq!( div, 3 ) ;
309
+ assert_eq!( rem, 1 ) ;
310
+ assert_eq!( ( div, rem) , Euclid :: div_rem_euclid( & x, & y) ) ;
283
311
}
284
312
) +
285
313
} ;
@@ -300,6 +328,7 @@ mod tests {
300
328
assert_eq!( Euclid :: div_euclid( & -x, & y) , 4 ) ;
301
329
assert_eq!( Euclid :: rem_euclid( & x, & y) , 1 ) ;
302
330
assert_eq!( Euclid :: rem_euclid( & -x, & y) , 2 ) ;
331
+ assert_eq!( ( Euclid :: div_euclid( & x, & y) , Euclid :: rem_euclid( & x, & y) ) , Euclid :: div_rem_euclid( & x, & y) ) ;
303
332
let x: $t = $t:: min_value( ) + 1 ;
304
333
let y: $t = -1 ;
305
334
assert_eq!( Euclid :: div_euclid( & x, & y) , $t:: max_value( ) ) ;
@@ -327,6 +356,7 @@ mod tests {
327
356
<= 46.4 * <$t as crate :: float:: FloatCore >:: epsilon( ) ) ;
328
357
assert!( Euclid :: div_euclid( & -x, & -y) * -y + Euclid :: rem_euclid( & -x, & -y) + x
329
358
<= 46.4 * <$t as crate :: float:: FloatCore >:: epsilon( ) ) ;
359
+ assert_eq!( ( Euclid :: div_euclid( & x, & y) , Euclid :: rem_euclid( & x, & y) ) , Euclid :: div_rem_euclid( & x, & y) ) ;
330
360
}
331
361
) +
332
362
} ;
@@ -343,8 +373,6 @@ mod tests {
343
373
{
344
374
assert_eq!( CheckedEuclid :: checked_div_euclid( & $t:: min_value( ) , & -1 ) , None ) ;
345
375
assert_eq!( CheckedEuclid :: checked_rem_euclid( & $t:: min_value( ) , & -1 ) , None ) ;
346
- assert_eq!( CheckedEuclid :: checked_div_euclid( & 1 , & 0 ) , None ) ;
347
- assert_eq!( CheckedEuclid :: checked_rem_euclid( & 1 , & 0 ) , None ) ;
348
376
}
349
377
) +
350
378
} ;
0 commit comments