datetime_string/
macros.rs

1//! Macros for internal use.
2
3/// Asserts that the safe alternative should return `Ok(_)`.
4macro_rules! debug_assert_safe_version_ok {
5    ($result:expr) => {
6        if cfg!(debug_assertions) {
7            if let core::result::Result::Err(e) = $result {
8                panic!(
9                    "Assertion by safe version failed (expr = {}): {}",
10                    stringify!($result),
11                    e
12                );
13            }
14        }
15    };
16}
17
18/// Asserts that the expression must return `Ok(_)`.
19macro_rules! debug_assert_ok {
20    ($result:expr) => {
21        if cfg!(debug_assertions) {
22            if let core::result::Result::Err(e) = $result {
23                panic!("Assertion failed (expr = {}): {}", stringify!($result), e);
24            }
25        }
26    };
27    ($result:expr, $($arg:tt)+) => {
28        if cfg!(debug_assertions) {
29            if let core::result::Result::Err(e) = $result {
30                panic!(
31                    "Assertion failed: {} (expr = {}): {}",
32                    format_args!("{}", $($arg)+),
33                    stringify!($result),
34                    e
35                );
36            }
37        }
38    };
39}
40
41/// Asserts that the safe alternative should return `Some(_)`.
42macro_rules! debug_assert_safe_version_some {
43    ($opt:expr) => {
44        if cfg!(debug_assertions) {
45            if $opt.is_none() {
46                panic!(
47                    "Assertion by safe version failed (expr = {}): expected `Some` but got `None`",
48                    stringify!($opt)
49                );
50            }
51        }
52    };
53}
54
55/// Implement `PartialEq` and `Eq` for the given types.
56macro_rules! impl_cmp {
57    ($ty_common:ty, $ty_lhs:ty, $ty_rhs:ty) => {
58        impl PartialEq<$ty_rhs> for $ty_lhs {
59            #[inline]
60            fn eq(&self, o: &$ty_rhs) -> bool {
61                <$ty_common as PartialEq<$ty_common>>::eq(AsRef::as_ref(self), AsRef::as_ref(o))
62            }
63        }
64        impl PartialOrd<$ty_rhs> for $ty_lhs {
65            #[inline]
66            fn partial_cmp(&self, o: &$ty_rhs) -> Option<core::cmp::Ordering> {
67                <$ty_common as PartialOrd<$ty_common>>::partial_cmp(
68                    AsRef::as_ref(self),
69                    AsRef::as_ref(o),
70                )
71            }
72        }
73    };
74}
75
76/// Implement `PartialEq` and `Eq` symmetrically for the given types.
77macro_rules! impl_cmp_symmetric {
78    ($ty_common:ty, $ty_lhs:ty, $ty_rhs:ty) => {
79        impl_cmp!($ty_common, $ty_lhs, $ty_rhs);
80        impl_cmp!($ty_common, $ty_rhs, $ty_lhs);
81    };
82}