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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use typenum::Unsigned;
use bm::{Error, ValueOf, ReadBackend, WriteBackend};
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};
use alloc::vec::Vec;
use crate::{ElementalVariableVecRef, ElementalVariableVec,
            IntoTree, IntoCompactListTree, IntoCompositeListTree,
            FromTree, FromCompactListTree, FromCompositeListTree,
            Compact, CompactRef, CompatibleConstruct};

/// Vec value with maximum length.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct MaxVec<T, ML>(pub Vec<T>, PhantomData<ML>);

impl<T, ML> Deref for MaxVec<T, ML> {
    type Target = Vec<T>;

    fn deref(&self) -> &Vec<T> {
        &self.0
    }
}

impl<T, ML> DerefMut for MaxVec<T, ML> {
    fn deref_mut(&mut self) -> &mut Vec<T> {
        &mut self.0
    }
}

impl<T, ML> AsRef<[T]> for MaxVec<T, ML> {
    fn as_ref(&self) -> &[T] {
        &self.0
    }
}

impl<T, ML> Default for MaxVec<T, ML> {
    fn default() -> Self {
        Self(Vec::new(), PhantomData)
    }
}

impl<T, ML> From<Vec<T>> for MaxVec<T, ML> {
    fn from(vec: Vec<T>) -> Self {
        Self(vec, PhantomData)
    }
}

impl<T, ML> Into<Vec<T>> for MaxVec<T, ML> {
    fn into(self) -> Vec<T> {
        self.0
    }
}

#[cfg(feature = "serde")]
impl<T: serde::Serialize, N: Unsigned> serde::Serialize for MaxVec<T, N> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
        S: serde::Serializer,
    {
        self.0.serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, T: serde::Deserialize<'de>, N: Unsigned> serde::Deserialize<'de> for MaxVec<T, N> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
        D: serde::Deserializer<'de>,
    {
        let vec = Vec::<T>::deserialize(deserializer)?;
        if vec.len() > N::to_usize() {
            return Err(<D::Error as serde::de::Error>::custom("invalid length"))
        }

        Ok(Self(vec, PhantomData))
    }
}

#[cfg(feature = "parity-codec")]
impl<T: parity_codec::Encode, N: Unsigned> parity_codec::Encode for MaxVec<T, N> {
    fn encode_to<W: parity_codec::Output>(&self, dest: &mut W) {
        self.0.encode_to(dest)
    }
}

#[cfg(feature = "parity-codec")]
impl<T: parity_codec::Decode, N: Unsigned> parity_codec::Decode for MaxVec<T, N> {
    fn decode<I: parity_codec::Input>(input: &mut I) -> Option<Self> {
        let decoded = Vec::<T>::decode(input)?;
        if decoded.len() <= N::to_usize() {
            Some(Self(decoded, PhantomData))
        } else {
            None
        }
    }
}

impl<T, ML: Unsigned> IntoTree for MaxVec<T, ML> where
    for<'b> ElementalVariableVecRef<'b, T>: IntoCompositeListTree,
{
    fn into_tree<DB: WriteBackend>(&self, db: &mut DB) -> Result<ValueOf<DB::Construct>, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct,
    {
        ElementalVariableVecRef(&self.0).into_composite_list_tree(db, Some(ML::to_usize()))
    }
}

impl<T, ML: Unsigned> FromTree for MaxVec<T, ML> where
    for<'a> ElementalVariableVec<T>: FromCompositeListTree,
{
    fn from_tree<DB: ReadBackend>(root: &ValueOf<DB::Construct>, db: &mut DB) -> Result<Self, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct,
    {
        let value = ElementalVariableVec::<T>::from_composite_list_tree(
            root, db, Some(ML::to_usize())
        )?;
        Ok(MaxVec(value.0, PhantomData))
    }
}

impl<'a, T, ML: Unsigned> IntoTree for CompactRef<'a, MaxVec<T, ML>> where
    for<'b> ElementalVariableVecRef<'b, T>: IntoCompactListTree,
{
    fn into_tree<DB: WriteBackend>(&self, db: &mut DB) -> Result<ValueOf<DB::Construct>, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct,
    {
        ElementalVariableVecRef(&self.0).into_compact_list_tree(db, Some(ML::to_usize()))
    }
}

impl<T, ML: Unsigned> IntoTree for Compact<MaxVec<T, ML>> where
    for<'b> ElementalVariableVecRef<'b, T>: IntoCompactListTree,
{
    fn into_tree<DB: WriteBackend>(&self, db: &mut DB) -> Result<ValueOf<DB::Construct>, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct,
    {
        ElementalVariableVecRef(&self.0).into_compact_list_tree(db, Some(ML::to_usize()))
    }
}

impl<T, ML: Unsigned> FromTree for Compact<MaxVec<T, ML>> where
    for<'a> ElementalVariableVec<T>: FromCompactListTree,
{
    fn from_tree<DB: ReadBackend>(root: &ValueOf<DB::Construct>, db: &mut DB) -> Result<Self, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct,
    {
        let value = ElementalVariableVec::<T>::from_compact_list_tree(
            root, db, Some(ML::to_usize())
        )?;
        Ok(Self(MaxVec(value.0, PhantomData)))
    }
}

impl<T> IntoTree for [T] where
    for<'a> ElementalVariableVecRef<'a, T>: IntoCompositeListTree,
{
    fn into_tree<DB: WriteBackend>(&self, db: &mut DB) -> Result<ValueOf<DB::Construct>, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct,
    {
        ElementalVariableVecRef(&self).into_composite_list_tree(db, None)
    }
}

impl<T> IntoTree for Vec<T> where
    for<'a> ElementalVariableVecRef<'a, T>: IntoCompositeListTree,
{
    fn into_tree<DB: WriteBackend>(&self, db: &mut DB) -> Result<ValueOf<DB::Construct>, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct,
    {
        ElementalVariableVecRef(&self).into_composite_list_tree(db, None)
    }
}

impl<T> FromTree for Vec<T> where
    ElementalVariableVec<T>: FromCompositeListTree,
{
    fn from_tree<DB: ReadBackend>(root: &ValueOf<DB::Construct>, db: &mut DB) -> Result<Self, Error<DB::Error>> where
        DB::Construct: CompatibleConstruct,
    {
        ElementalVariableVec::from_composite_list_tree(root, db, None).map(|ret| ret.0)
    }
}