macro_toolset/
lib.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! Dev deps: some useful macros.

#[cfg(feature = "feat-base64")]
pub mod base64;
#[cfg(feature = "feat-hash")]
pub mod hash;
pub mod misc;
#[cfg(feature = "feat-random")]
pub mod random;
#[cfg(feature = "feat-string")]
pub mod string;

#[macro_export]
/// Faster way to get current timestamp other than
/// `chrono::Local::now().timestamp()`, 12x faster on my machine.
///
/// # Example
///
/// ```rust
/// # use macro_toolset::now;
/// let now_ts_sec = now!().as_secs(); // Seconds since UNIX_EPOCH
/// let now_ts_millis = now!().as_millis(); // Milliseconds since UNIX_EPOCH
/// ```
///
/// See [`Duration`](https://doc.rust-lang.org/std/time/struct.Duration.html) for more details.
macro_rules! now {
    () => {{
        match std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) {
            Ok(t) => t,
            Err(_) => panic!("SystemTime before UNIX EPOCH!"),
        }
    }};
}

#[macro_export]
/// Init `tracing_subscriber` with default settings.
///
/// This is useful when running tests.
macro_rules! init_tracing_simple {
    () => {{
        use tracing::level_filters::LevelFilter;
        use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer};

        let fmt_layer = tracing_subscriber::fmt::layer().with_filter(
            EnvFilter::builder()
                .with_default_directive(LevelFilter::DEBUG.into())
                .from_env_lossy()
                .add_directive("otel::tracing=trace".parse().unwrap())
                .add_directive("h2=error".parse().unwrap())
                .add_directive("tower=error".parse().unwrap())
                .add_directive("hyper=error".parse().unwrap()),
        );

        tracing_subscriber::registry().with(fmt_layer).init();
    }};
}

#[macro_export]
/// Helper macro for creating a wrapper type.
///
/// The wrapper type will implement [`Deref`](std::ops::Deref),
/// [`DerefMut`](std::ops::DerefMut), [`From`] and [`AsRef`].
///
/// # Example
///
/// ```rust
/// # use macro_toolset::wrapper;
/// wrapper!(pub MyString(String));
/// // Derive is OK!
/// wrapper!(pub MyStringDerived(String), derive(Debug, Clone, PartialEq, Eq, Hash));
/// // Lifetime is supported too!
/// wrapper!(pub MyStringLifetime<'a>(&'a str));
/// wrapper!(pub MyStringLifetimePubDerived<'a>(pub &'a str), derive(Debug, Clone, PartialEq, Eq, Hash));
/// ```
///
/// Reference: https://stackoverflow.com/a/61189128
macro_rules! wrapper {
    ($vis:vis $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? ($inner_vis:vis $inner:ty) $(, <$($plt_name:ident: $plt:lifetime),+>)? $(, derive($($derive:path),+))?) => {
        $(#[derive($($derive),+)])?
        #[repr(transparent)]
        #[doc = concat!("Wrapper over `", stringify!($inner), "`")]
        $vis struct $name$(<$($lt),+>)? {
            $inner_vis inner: $inner,
            $($(
                $plt_name: std::marker::PhantomData<&$plt ()>,
            ),+)?
        }

        impl$(<$($lt),+>)? From<$inner> for $name$(<$($lt),+>)? {
            #[inline]
            fn from(inner: $inner) -> Self {
                Self {
                    inner,
                    $($($plt_name: std::marker::PhantomData),+)?
                }
            }
        }

        impl$(<$($lt),+>)? std::ops::Deref for $name$(<$($lt),+>)? {
            type Target = $inner;

            fn deref(&self) -> &Self::Target {
                &self.inner
            }
        }

        impl$(<$($lt),+>)? std::ops::DerefMut for $name$(<$($lt),+>)? {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.inner
            }
        }

        impl$(<$($lt),+>)? AsRef<$inner> for $name$(<$($lt),+>)? {
            fn as_ref(&self) -> &$inner {
                &self.inner
            }
        }

        impl$(<$($lt),+>)? $name$(<$($lt),+>)? {
            #[inline]
            #[doc = concat!("Creates a new instance of [`", stringify!($name), "`]")]
            $vis const fn new(inner: $inner) -> Self {
                Self {
                    inner,
                    $($($plt_name: std::marker::PhantomData),+)?
                }
            }
        }
    };
}

#[cfg(test)]
mod tests {
    #![allow(unused)]
    #![allow(unreachable_pub)]

    // Simple wrapper!
    wrapper!(pub MyString(String));
    wrapper!(pub MyStringPub(pub String));
    wrapper!(pub MyStringPubCrate(pub(crate) String));
    // Derive is OK!
    wrapper!(pub MyStringDerived(String), derive(Debug, Clone, PartialEq, Eq, Hash));
    wrapper!(pub MyStringPubInnerDerived(pub String), derive(Debug, Clone, PartialEq, Eq, Hash));
    wrapper!(pub MyStringPubCrateInnerDerived(pub(crate) String), derive(Debug, Clone, PartialEq, Eq, Hash));
    // Lifetime is supported too!
    wrapper!(pub MyStringLifetime<'a>(&'a str));
    wrapper!(pub MyStringLifetimePubInner<'a>(pub &'a str));
    wrapper!(pub MyStringLifetimePubCrateInner<'a>(pub(crate) &'a str));
    wrapper!(pub MyStringLifetimePubDerived<'a>(pub &'a str), derive(Debug, Clone, PartialEq, Eq, Hash));
    wrapper!(pub MyStringLifetimePubMutDerived<'a>(pub &'a mut str), derive(Debug, PartialEq, Eq, Hash));
    // Generic is supported too!
    wrapper!(pub MyStringLifetimePubT<T>(pub T), derive(Debug, Clone));
    // Complicated!
    wrapper!(pub MyStringLifetimePubRefT<'a, T>(pub &'a T), derive(Debug, Clone));
}