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

//!
//! Versioned wrapper types for serialization
//!
//! In the bin-prot Mina wire protocol, each nested type has an associated version. This is to allow for backward
//! compatibility if parts of the wire protocol change. This simple wrapper type ensures that this information
//! is included in the serialized output in an indentical way to the mina reference implementation.
//!

#![deny(warnings)]
#![deny(missing_docs)]

pub mod macros;

use serde::{Deserialize, Serialize};

/// A generic version wrapper around another type
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
pub struct Versioned<T, const V: u16> {
    /// Version byte to be encoded first when the whole wrapper is serialized
    pub version: u16,
    /// The wrapped type
    pub t: T,
}

impl<T, const V: u16> Default for Versioned<T, V>
where
    T: Default,
{
    fn default() -> Self {
        Self {
            version: V, // version should always be equal to V
            t: Default::default(),
        }
    }
}

impl<T, const V: u16> Versioned<T, V> {
    /// create a new version type of the given const version
    pub fn new(t: T) -> Self {
        Self { version: V, t }
    }

    /// Return the inner type
    pub fn inner(self) -> T {
        self.t
    }

    /// Return the version number
    pub fn version(&self) -> u16 {
        self.version
    }
}

impl<T, const V: u16> From<T> for Versioned<T, V> {
    #[inline]
    fn from(t: T) -> Self {
        Versioned::new(t)
    }
}

impl<T, const V: u16> From<Versioned<T, V>> for (T,) {
    #[inline]
    fn from(t: Versioned<T, V>) -> Self {
        (t.t,)
    }
}

impl<T, const V1: u16, const V2: u16> From<T> for Versioned<Versioned<T, V1>, V2> {
    #[inline]
    fn from(t: T) -> Self {
        let t: Versioned<T, V1> = t.into();
        t.into()
    }
}

impl<T, const V1: u16, const V2: u16> From<Versioned<Versioned<T, V1>, V2>> for (T,) {
    #[inline]
    fn from(t: Versioned<Versioned<T, V1>, V2>) -> Self {
        let (t,): (Versioned<T, V1>,) = t.into();
        t.into()
    }
}

impl<T, const V1: u16, const V2: u16, const V3: u16> From<T>
    for Versioned<Versioned<Versioned<T, V1>, V2>, V3>
{
    #[inline]
    fn from(t: T) -> Self {
        let t: Versioned<Versioned<T, V1>, V2> = t.into();
        t.into()
    }
}

impl<T, const V1: u16, const V2: u16, const V3: u16>
    From<Versioned<Versioned<Versioned<T, V1>, V2>, V3>> for (T,)
{
    #[inline]
    fn from(t: Versioned<Versioned<Versioned<T, V1>, V2>, V3>) -> Self {
        let (t,): (Versioned<Versioned<T, V1>, V2>,) = t.into();
        t.into()
    }
}

impl<T, const V1: u16, const V2: u16, const V3: u16, const V4: u16> From<T>
    for Versioned<Versioned<Versioned<Versioned<T, V1>, V2>, V3>, V4>
{
    #[inline]
    fn from(t: T) -> Self {
        let t: Versioned<Versioned<Versioned<T, V1>, V2>, V3> = t.into();
        t.into()
    }
}

impl<T, const V1: u16, const V2: u16, const V3: u16, const V4: u16>
    From<Versioned<Versioned<Versioned<Versioned<T, V1>, V2>, V3>, V4>> for (T,)
{
    #[inline]
    fn from(t: Versioned<Versioned<Versioned<Versioned<T, V1>, V2>, V3>, V4>) -> Self {
        let (t,): (Versioned<Versioned<Versioned<T, V1>, V2>, V3>,) = t.into();
        t.into()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_versioned() {
        type I32V1 = Versioned<i32, 2>;

        let i = I32V1::default();
        assert_eq!(i.version(), 2);
        assert_eq!(i.inner(), i32::default());
    }
}