unstd/
math.rs

1//! math related helpers
2
3macro_rules! doc_comment {
4    ($x:expr, $($tt:tt)*) => {
5        #[doc = $x]
6        $($tt)*
7    };
8}
9
10macro_rules! divrems_for {
11    ($ty:ident ) => {
12        doc_comment! {
13            concat!("Math helpers for integer type ",stringify!($ty)),
14            pub mod $ty {
15                doc_comment! {
16                    concat!("Calculates the quotient and remainder of regular integer division and returns the result as a tuple.
17
18Internally it does simply `(lhs / rhs, lhs % rhs)`.
19
20## Panics
21
22This function will panic if `rhs` is 0.
23
24## Example
25```rust
26let (q, r) = unstd::math::",stringify!($ty),"::divrem(&3, &2);
27//=> (1, 1)
28```
29                    "),
30                    pub fn divrem(lhs: &$ty, rhs: &$ty) -> ($ty, $ty) {
31                        (lhs / rhs, lhs % rhs)
32                    }
33                }
34
35                doc_comment! {
36                    concat!("Calculates the quotient and remainder of Euclidean division and returns the result as a tuple.
37
38Internally it calls both [`div_euclid`][div_euclid] and [`rem_euclid`][rem_euclid], so same rules apply as described in the rust-lang docs for the type.
39
40## Panics
41
42This function will panic if `rhs` is 0.
43
44## Example
45```rust
46let (q, r) = unstd::math::",stringify!($ty),"::divrem(&3, &2);
47//=> (1, 1)
48```
49
50[div_euclid]: ",stringify!($ty),"::div_euclid
51[rem_euclid]: ",stringify!($ty),"::rem_euclid
52                    "),
53                    pub fn divrem_euclid(lhs: &$ty, rhs: &$ty) -> ($ty, $ty) {
54                        (lhs.div_euclid(*rhs), lhs.rem_euclid(*rhs))
55                    }
56                }
57            }
58        }
59    };
60}
61
62macro_rules! create_fns_for_ints {
63  ($( $ty:ident ),*) => {
64    $(
65      divrems_for!($ty);
66    )*
67  };
68}
69
70create_fns_for_ints!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);