-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmacros.rs
83 lines (71 loc) · 1.72 KB
/
macros.rs
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
/*!
# Utc2k - Macros
*/
/// # Helper: `AsRef` and `Borrow`.
macro_rules! as_ref_borrow_cast {
($parent:ty: $($cast:ident $ty:ty),+ $(,)?) => ($(
impl AsRef<$ty> for $parent {
#[inline]
fn as_ref(&self) -> &$ty { self.$cast() }
}
impl ::std::borrow::Borrow<$ty> for $parent {
#[inline]
fn borrow(&self) -> &$ty { self.$cast() }
}
)+);
}
/// # Helper: `Display`.
macro_rules! display_str {
($cast:ident $ty:ty) => (
impl ::std::fmt::Display for $ty {
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
f.write_str(self.$cast())
}
}
);
}
/// # Helper: 2-Way `PartialEq`.
macro_rules! partial_eq_cast {
// Dereference.
(deref $parent:ty: $($cast:ident $ty:ty),+ $(,)?) => ($(
impl PartialEq<$ty> for $parent {
#[inline]
fn eq(&self, other: &$ty) -> bool { self.$cast() == *other }
}
impl PartialEq<$parent> for $ty {
#[inline]
fn eq(&self, other: &$parent) -> bool { other.$cast() == *self }
}
)+);
// Plain.
($parent:ty: $($cast:ident $ty:ty),+ $(,)?) => ($(
impl PartialEq<$ty> for $parent {
#[inline]
fn eq(&self, other: &$ty) -> bool { self.$cast() == other }
}
impl PartialEq<$parent> for $ty {
#[inline]
fn eq(&self, other: &$parent) -> bool { other.$cast() == self }
}
)+);
}
/// # Helper: 2-Way `PartialEq`.
macro_rules! partial_eq_from {
($parent:ty: $($ty:ty),+ $(,)?) => ($(
impl PartialEq<$ty> for $parent {
#[inline]
fn eq(&self, other: &$ty) -> bool { <$ty>::from(*self).eq(other) }
}
impl PartialEq<$parent> for $ty {
#[inline]
fn eq(&self, other: &$parent) -> bool { Self::from(*other).eq(self) }
}
)+);
}
pub(super) use {
as_ref_borrow_cast,
display_str,
partial_eq_cast,
partial_eq_from,
};