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
//! Support for the [`primitive-types`](https://crates.io/crates/primitive-types) crate.
#![cfg(feature = "primitive-types")]
#![cfg_attr(has_doc_cfg, doc(cfg(feature = "primitive-types")))]

use crate::aliases as ours;
use primitive_types::{U128, U256, U512};

// FEATURE: Support H160, H256..

macro_rules! impl_froms {
    ($ours:ty, $theirs:ident) => {
        impl From<$theirs> for $ours {
            #[inline]
            fn from(value: $theirs) -> Self {
                Self::from_limbs(value.0)
            }
        }

        impl From<$ours> for $theirs {
            fn from(value: $ours) -> Self {
                $theirs(value.into_limbs())
            }
        }
    };
}

impl_froms!(ours::U128, U128);
impl_froms!(ours::U256, U256);
impl_froms!(ours::U512, U512);

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::{arbitrary::Arbitrary, proptest};

    fn test_roundtrip<Ours, Theirs>()
    where
        Ours: Clone + PartialEq + Arbitrary + From<Theirs>,
        Theirs: From<Ours>,
    {
        proptest!(|(value: Ours)| {
            let theirs: Theirs = value.clone().into();
            let ours: Ours = theirs.into();
            assert_eq!(ours, value);
        });
    }

    #[test]
    fn test_roundtrips() {
        test_roundtrip::<ours::U128, U128>();
        test_roundtrip::<ours::U256, U256>();
        test_roundtrip::<ours::U512, U512>();
    }
}