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
        scale_info::TypeInfo,
22
        sp_core::{Decode, Encode, RuntimeDebug},
23
    };
24
}
25

            
26
pub use macro_rules_attribute::apply;
27

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

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

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

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

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

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

            
120
);
121

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

            
130
);