1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
#[doc(hidden)]
18
pub mod __reexports {
19
    pub use {
20
        frame_support::{CloneNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound},
21
        parity_scale_codec::DecodeWithMemTracking,
22
        scale_info::TypeInfo,
23
        sp_core::{Decode, Encode, RuntimeDebug},
24
    };
25
}
26

            
27
pub use macro_rules_attribute::apply;
28

            
29
#[macro_export]
30
macro_rules! derive_storage_traits {
31
    ( $( $tt:tt )* ) => {
32
        #[derive(
33
            $crate::alias::__reexports::RuntimeDebug,
34
            ::core::cmp::PartialEq,
35
            ::core::cmp::Eq,
36
            ::core::clone::Clone,
37
            $crate::alias::__reexports::Encode,
38
            $crate::alias::__reexports::Decode,
39
19158
            $crate::alias::__reexports::TypeInfo,
40
        )]
41
        $($tt)*
42
    }
43
}
44

            
45
// This currently doesn't work due to a quirk in RuntimeDebugNoBound, PartialEqNoBound
46
// and CloneNoBound, as there seem to be something breaking macro hygiene. This is not an
47
// issue when using the derive directly, but doesn't compile when adding it through our macro.
48
// #[macro_export]
49
// macro_rules! derive_storage_traits_no_bounds {
50
//     ( $( $tt:tt )* ) => (
51
//         #[derive(
52
//             $crate::alias::__reexports::RuntimeDebugNoBound,
53
//             $crate::alias::__reexports::PartialEqNoBound,
54
//             $crate::alias::__reexports::EqNoBound,
55
//             $crate::alias::__reexports::CloneNoBound,
56
//             $crate::alias::__reexports::Encode,
57
//             $crate::alias::__reexports::Decode,
58
//             $crate::alias::__reexports::TypeInfo,
59
//         )]
60
//         $($tt)*
61
//     );
62
// }
63

            
64
/// Derives traits related to SCALE encoding and serde.
65
#[macro_export]
66
macro_rules! derive_scale_codec {
67
    ( $( $tt:tt )* ) => {
68
        #[derive(
69
            $crate::alias::__reexports::Encode,
70
            $crate::alias::__reexports::Decode,
71
            $crate::alias::__reexports::DecodeWithMemTracking,
72
6798
            $crate::alias::__reexports::TypeInfo,
73
        )]
74
        $($tt)*
75
    }
76
}
77

            
78
/// Macro to define a trait alias for one or othe traits.
79
/// Thanks to Associated Type Bounds syntax stabilized in Rust 1.79, it can be used to
80
/// reduce the need to repeat a lot of `<Foo as Bar>::Baz : Traits`.
81
///
82
/// Extra parenthesis around bounds allows to easily parse them as-is and not restrict their
83
/// expressivity.
84
#[macro_export]
85
macro_rules! alias {
86
    (
87
        $(#[$attr:meta])*
88
        $vis:vis
89
        trait
90
        $alias:ident
91
        $(< $(
92
            $tparam:ident
93
            $( : ( $( $tparam_bound:tt )+ ) )?
94
        ),+ $(,)? >)?
95
        : $( $bounds:tt )+
96
    ) => {
97
        $(#[$attr])*
98
        $vis trait $alias $( < $(
99
            $tparam
100
            $( : $($tparam_bound)+)?
101
        ),+ > )?
102
        : $( $bounds )+
103
        { }
104

            
105
        impl<__Self, $( $(
106
            $tparam
107
            $( : $($tparam_bound)+)?
108
        ),+ )?>
109
        $alias $( < $( $tparam ),+ > )?
110
        for __Self
111
        where __Self : $( $bounds )+
112
        { }
113
    }
114
}
115

            
116
alias!(
117
    pub trait ScaleCodec :
118
        __reexports::Encode +
119
        __reexports::Decode +
120
        __reexports::TypeInfo
121

            
122
);
123

            
124
alias!(
125
    pub trait StorageTraits :
126
        ::core::fmt::Debug +
127
        ::core::clone::Clone +
128
        ::core::cmp::Eq +
129
        ::core::cmp::PartialEq +
130
        ScaleCodec
131

            
132
);