1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * PCG Random Number Generation for Rust
 *
 * Copyright 2015 John Brooks <robojeb@robojeb.xyz>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

/// The types of numaric options that PCG needs to operate.
/// Some day this will be replaced with Num-traits when they support
/// wrapping opts for everything, and when extprim supports those traits as
/// well.
pub trait PcgOps {
    fn wrap_mul(&self, rhs: Self) -> Self;
    fn wrap_add(&self, rhs: Self) -> Self;
}

/// Convert a value to a usize don't care about overflow etc
pub trait AsUsize {
    fn as_usize(&self) -> usize;
}

/// A trait that determines how many bits are in a type.
pub trait BitSize {
    const BITS: usize;
}

/// Allows a type to become a type of a smaller value.
pub trait AsSmaller<T> {
    fn shrink(self) -> T;
}

//Implementations of the traits for basic types
macro_rules! basic_ops {
    ( $( $t:ty, $bits:expr);*) => {
        $(impl BitSize for $t {
            const BITS: usize = $bits;
        }

        impl AsUsize for $t {
            #[inline]
            fn as_usize(&self) -> usize {
                *self as usize
            }
        }

        impl PcgOps for $t {
            #[inline]
            fn wrap_mul(&self, rhs : $t) -> $t {
                self.wrapping_mul(rhs)
            }

            #[inline]
            fn wrap_add(&self, rhs : $t) -> $t {
                self.wrapping_add(rhs)
            }
        }

        )*
    }
}

basic_ops!(
    u8, 8;
    u16, 16;
    u32, 32;
    u64, 64;
    u128, 128
);

macro_rules! smaller {
    ( $( $t:ty, $other:ty);*) => {
        $(
            impl AsSmaller<$other> for $t {
                #[inline]
                fn shrink(self) -> $other {
                    self as $other
                }
            }
        )*
    }
}

smaller!(
    u128, u64;
    u128, u32;
    u128, u16;
    u128, u8;
    u64, u32;
    u64, u16;
    u64, u8;
    u32, u16;
    u32, u8;
    u16, u8
);

// impl PcgOps for u128 {
//     #[inline]
//     fn wrap_mul(&self, rhs : u128) -> u128 {
//         self.wrapping_mul(rhs)
//     }

//     #[inline]
//     fn wrap_add(&self, rhs : u128) -> u128 {
//         self.wrapping_add(rhs)
//     }
// }