graphql_starter/
macros.rs

1/// Creates a newtype struct implementing [From], [Deref](std::ops::Deref) and [DerefMut](std::ops::DerefMut)
2#[macro_export]
3macro_rules! newtype {
4    ($(#[$attr:meta])* $new_v:vis $new_ty:ident $(< $($gen:ident$(: $gen_ty:path)?),* >)? ($v:vis $ty:path)) => {
5        $(#[$attr])*
6        $new_v struct $new_ty$(<$($gen$(: $gen_ty)?),*>)?($v $ty);
7        $crate::newtype_impl!($new_ty$(<$($gen$(: $gen_ty)?),*>)?($ty));
8    };
9}
10
11/// Implements [From], [Deref](std::ops::Deref) and [DerefMut](std::ops::DerefMut) for a newtype
12#[macro_export]
13macro_rules! newtype_impl {
14    ($new_ty:ident $(< $($gen:ident$(: $gen_ty:path)?),* >)? ($ty:path)) => {
15        impl$(<$($gen$(: $gen_ty)?),*>)? From<$ty> for $new_ty$(<$($gen),*>)? {
16            fn from(o: $ty) -> Self {
17                $new_ty(o)
18            }
19        }
20        impl$(<$($gen$(: $gen_ty)?),*>)? From<$new_ty$(<$($gen),*>)?> for $ty {
21            fn from(o: $new_ty$(<$($gen),*>)?) -> Self {
22                o.0
23            }
24        }
25        impl$(<$($gen$(: $gen_ty)?),*>)? ::std::ops::Deref for $new_ty$(<$($gen),*>)? {
26            type Target = $ty;
27
28            fn deref(&self) -> &Self::Target {
29                &self.0
30            }
31        }
32        impl$(<$($gen$(: $gen_ty)?),*>)? ::std::ops::DerefMut for $new_ty$(<$($gen),*>)? {
33            fn deref_mut(&mut self) -> &mut Self::Target {
34                &mut self.0
35            }
36        }
37    };
38}
39
40/// Declares a `mod` and uses it.
41///
42/// ## Examples
43/// ``` ignore
44/// using!{
45///   pub mod1,
46///   pub(crate) mod2,
47///   mod3
48/// }
49/// ```
50/// Expands to:
51/// ``` ignore
52/// mod mod1;
53/// pub use self::mod1::*;
54/// mod mod2;
55/// pub(crate) use self::mod2::*;
56/// mod mod3;
57/// use self::name::*;
58/// ```
59#[macro_export]
60macro_rules! using {
61    ($($v:vis $p:ident),* $(,)?) => {
62        $(
63            mod $p;
64            $v use self::$p::*;
65        )*
66    }
67}