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
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0

//!
//! Macros that help implement common traits for versioned wrapper types
//!

/// Macro that implements [From] trait for for a struct and its versioned wrapper
#[macro_export]
macro_rules! impl_from_versioned {
    ($t:ty) => {
        impl<const V: u16> From<::versioned::Versioned<$t, V>> for $t {
            fn from(t: ::versioned::Versioned<$t, V>) -> Self {
                let (t,) = t.into();
                t
            }
        }

        impl<const V1: u16, const V2: u16>
            From<::versioned::Versioned<::versioned::Versioned<$t, V1>, V2>> for $t
        {
            fn from(t: ::versioned::Versioned<::versioned::Versioned<$t, V1>, V2>) -> Self {
                let (t,) = t.into();
                t
            }
        }
    };
}

/// Macro that implements [From] trait for a newtype struct and its versioned wrapper
#[macro_export]
macro_rules! impl_from_for_newtype {
    ($t:ty, $tv:ty) => {
        impl From<$t> for $tv {
            fn from(t: $t) -> Self {
                t.0.into()
            }
        }

        impl From<$tv> for $t {
            fn from(t: $tv) -> Self {
                let (t,) = t.into();
                Self(t)
            }
        }
    };
}

/// Macro that implements [From] trait for 2-way conversion
/// between the orignal type and the target json type, via an
/// intermidate versioned type that is convertible from and to
/// both orignal and target types.
/// It also implements BinProtSerializationType and JsonSerializationType
/// for convenience
#[macro_export]
macro_rules! impl_from_with_proxy {
    ($t:ty, $t_versioned:ty, $t_json:ty) => {
        impl From<$t> for $t_json {
            fn from(t: $t) -> Self {
                let intermidate: $t_versioned = t.into();
                intermidate.into()
            }
        }

        impl From<$t_json> for $t {
            fn from(t: $t_json) -> Self {
                let intermidate: $t_versioned = t.into();
                intermidate.into()
            }
        }
    };
}

/// Macro that implements [From] trait for 2-way conversion
/// between the orignal type and the versioned target type, using the
/// intermidate type that is convertible from and to both
/// orignal and target types
#[macro_export]
macro_rules! impl_from_for_versioned_with_proxy {
    ($t:ty, $ti:ty, $t2:ty) => {
        impl From<$t> for $t2 {
            fn from(t: $t) -> Self {
                let intermidate: $ti = t.into();
                intermidate.into()
            }
        }

        impl From<$t2> for $t {
            fn from(t: $t2) -> Self {
                let (intermidate,): ($ti,) = t.into();
                intermidate.into()
            }
        }
    };
}

/// Macro that implements [From] trait for 2-way conversion
/// between the orignal type and the generic target type, using the
/// intermidate type that is convertible from and to both
/// orignal and target types
#[macro_export]
macro_rules! impl_from_for_generic_with_proxy {
    ($t:ty, $ti:ty, $t2:ty) => {
        impl From<$t> for $t2 {
            fn from(t: $t) -> Self {
                let intermidate: $ti = t.into();
                intermidate.into()
            }
        }

        impl From<$t2> for $t {
            fn from(t: $t2) -> Self {
                let (intermidate,): ($ti,) = t.into();
                intermidate.into()
            }
        }
    };
}