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
//! Defines the type-safe vector.

use crate::{
    common::*,
    impls,
    size::{Dyn, IntoSize, Size},
};

/// The type-safe vector with type-level length.
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Vect<T, S>
where
    S: Size,
{
    pub(crate) data: Vec<T>,
    pub(crate) _phantom: PhantomData<S>,
}

impl<T> Vect<T, U0> {
    /// Creates an empty vector with static length.
    pub fn new() -> Self {
        Self {
            data: vec![],
            _phantom: PhantomData,
        }
    }

    /// Creates an empty vector with static length and with specified capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            data: Vec::with_capacity(capacity),
            _phantom: PhantomData,
        }
    }

    /// Gets the number of elements.
    pub fn len(&self) -> usize {
        U0::USIZE
    }
}

impl<T> Vect<T, Dyn> {
    /// Creates an empty vector with dynamic length.
    pub fn new() -> Self {
        Self {
            data: vec![],
            _phantom: PhantomData,
        }
    }

    /// Creates a vector from [Vec](Vec).
    pub fn from_vec(data: Vec<T>) -> Self {
        Self {
            data,
            _phantom: PhantomData,
        }
    }

    /// Creates an empty vector with dynamic length and with specified capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            data: Vec::with_capacity(capacity),
            _phantom: PhantomData,
        }
    }

    /// Gets the number of elements.
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Converts a vector with static length.
    ///
    /// The vector size must be equal to the specified static size.
    /// Otherwise it returns `None`.
    pub fn into_static<S>(self) -> Option<Vect<T, S>>
    where
        S: Unsigned + Size,
    {
        let data = self.data;
        if data.len() == S::USIZE {
            Some(Vect {
                data,
                _phantom: PhantomData,
            })
        } else {
            None
        }
    }
}

impl<T, U, B> Vect<T, UInt<U, B>>
where
    U: Unsigned,
    B: Bit,
{
    /// Gets the number of elements.
    pub fn len(&self) -> usize {
        UInt::<U, B>::USIZE
    }
}

impl<T, S> Vect<T, S>
where
    S: Size,
{
    /// Appends an element to the end of the vector.
    pub fn push(self, elem: T) -> impls::PushImplOp<Self, T>
    where
        (): impls::PushImpl<Self, T>,
    {
        <() as impls::PushImpl<Self, T>>::impl_push(self, elem)
    }

    /// Removes an element from the end of the vector.
    pub fn pop(self) -> impls::PopImplOp<Self>
    where
        (): impls::PopImpl<Self>,
    {
        <() as impls::PopImpl<Self>>::impl_pop(self)
    }

    /// Returns a reference to an element depending on the index.
    ///
    /// If both length and index have static sizes, it returns `&T`. Otherwise, it returns `Option<&T>`.
    pub fn get<'a, I>(&'a self, index: I) -> impls::GetImplOp<'a, Self, I::Output>
    where
        I: IntoSize,
        (): impls::GetImpl<'a, Self, I::Output>,
    {
        <() as impls::GetImpl<'a, Self, I::Output>>::impl_get(self, index.into_size())
    }

    /// Inserts an element at specified index.
    ///
    /// If both length and index have static sizes, it checks if the index is valid in compile time.
    /// Otherwise, it panics if the index is out of bound.
    pub fn insert<I>(self, index: I, elem: T) -> impls::InsertImplOp<Self, I::Output, T>
    where
        I: IntoSize,
        (): impls::InsertImpl<Self, I::Output, T>,
    {
        <() as impls::InsertImpl<Self, I::Output, T>>::impl_insert(self, index.into_size(), elem)
    }

    /// Removes an element at specified index.
    ///
    /// If both length and index have static sizes, it checks if the index is valid in compile time.
    /// Otherwise, it panics if the index is out of bound.
    pub fn remove<I>(self, index: I) -> impls::RemoveImplOp<Self, I::Output>
    where
        I: IntoSize,
        (): impls::RemoveImpl<Self, I::Output>,
    {
        <() as impls::RemoveImpl<Self, I::Output>>::impl_remove(self, index.into_size())
    }

    /// Converts to a vector with dynamic length type.
    pub fn into_dyn(self) -> Vect<T, Dyn> {
        Vect {
            data: self.data,
            _phantom: PhantomData,
        }
    }

    /// Converts to [Vec](Vec).
    pub fn into_vec(self) -> Vec<T> {
        self.data
    }
}