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
//! Macros for internal use.

/// Asserts that the safe alternative should return `Ok(_)`.
macro_rules! debug_assert_safe_version_ok {
    ($result:expr) => {
        if cfg!(debug_assertions) {
            if let core::result::Result::Err(e) = $result {
                panic!(
                    "Assertion by safe version failed (expr = {}): {}",
                    stringify!($result),
                    e
                );
            }
        }
    };
}

/// Asserts that the expression must return `Ok(_)`.
macro_rules! debug_assert_ok {
    ($result:expr) => {
        if cfg!(debug_assertions) {
            if let core::result::Result::Err(e) = $result {
                panic!("Assertion failed (expr = {}): {}", stringify!($result), e);
            }
        }
    };
    ($result:expr, $($arg:tt)+) => {
        if cfg!(debug_assertions) {
            if let core::result::Result::Err(e) = $result {
                panic!(
                    "Assertion failed: {} (expr = {}): {}",
                    format_args!("{}", $($arg)+),
                    stringify!($result),
                    e
                );
            }
        }
    };
}

/// Asserts that the safe alternative should return `Some(_)`.
macro_rules! debug_assert_safe_version_some {
    ($opt:expr) => {
        if cfg!(debug_assertions) {
            if $opt.is_none() {
                panic!(
                    "Assertion by safe version failed (expr = {}): expected `Some` but got `None`",
                    stringify!($opt)
                );
            }
        }
    };
}

/// Implement `PartialEq` and `Eq` for the given types.
macro_rules! impl_cmp {
    ($ty_common:ty, $ty_lhs:ty, $ty_rhs:ty) => {
        impl PartialEq<$ty_rhs> for $ty_lhs {
            #[inline]
            fn eq(&self, o: &$ty_rhs) -> bool {
                <$ty_common as PartialEq<$ty_common>>::eq(AsRef::as_ref(self), AsRef::as_ref(o))
            }
        }
        impl PartialOrd<$ty_rhs> for $ty_lhs {
            #[inline]
            fn partial_cmp(&self, o: &$ty_rhs) -> Option<core::cmp::Ordering> {
                <$ty_common as PartialOrd<$ty_common>>::partial_cmp(
                    AsRef::as_ref(self),
                    AsRef::as_ref(o),
                )
            }
        }
    };
}

/// Implement `PartialEq` and `Eq` symmetrically for the given types.
macro_rules! impl_cmp_symmetric {
    ($ty_common:ty, $ty_lhs:ty, $ty_rhs:ty) => {
        impl_cmp!($ty_common, $ty_lhs, $ty_rhs);
        impl_cmp!($ty_common, $ty_rhs, $ty_lhs);
    };
}