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
use crate::{hint, types, util, Parcel, Error, Settings};
use std::io::prelude::*;
use std;

/// A newtype wrapping `Vec<T>` but with a custom length prefix type.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Vec<S: types::Integer, T: Parcel>
{
    /// The inner `Vec<T>`.
    pub elements: std::vec::Vec<T>,
    _a: std::marker::PhantomData<S>,
}

impl<S: types::Integer, T: Parcel> Vec<S,T>
{
    /// Creates a new `Vec` from a list of elements.
    pub fn new(elements: std::vec::Vec<T>) -> Self {
        Vec { elements: elements, _a: std::marker::PhantomData }
    }
}

impl<S: types::Integer, T: Parcel> Parcel for Vec<S, T>
{
    const TYPE_NAME: &'static str = "protocol::Vec<S,T>";

    fn read_field(read: &mut dyn Read,
                  settings: &Settings,
                  hints: &mut hint::Hints) -> Result<Self, Error> {
        let elements = util::read_list_ext::<S,T>(read, settings, hints)?;
        Ok(Self::new(elements))
    }

    fn write_field(&self, write: &mut dyn Write,
                   settings: &Settings,
                   hints: &mut hint::Hints) -> Result<(), Error> {
        util::write_list_ext::<S,T,_>(self.elements.iter(), write, settings, hints)
    }
}


impl<S, T> std::fmt::Debug for Vec<S, T>
    where S: types::Integer, T: Parcel + std::fmt::Debug {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.elements.fmt(fmt)
    }
}

impl<S, T> std::ops::Deref for Vec<S, T>
    where S: types::Integer, T: Parcel {
    type Target = [T];

    fn deref(&self) -> &[T] { &self.elements[..] }
}

impl<S, T> std::ops::DerefMut for Vec<S, T>
    where S: types::Integer, T: Parcel {
    fn deref_mut(&mut self) -> &mut [T] { &mut self.elements }
}

impl<S, T> AsRef<[T]> for Vec<S, T>
    where S: types::Integer, T: Parcel {
    fn as_ref(&self) -> &[T] { &self.elements[..] }
}

impl<S, T> AsMut<[T]> for Vec<S, T>
    where S: types::Integer, T: Parcel {
    fn as_mut(&mut self) -> &mut [T] { &mut self.elements }
}

/// Stuff relating to `std::vec::Vec<T>`.
mod std_vec {
    use crate::{hint, util, Error, Parcel, Settings};
    use std::io::prelude::*;

    impl<T: Parcel> Parcel for Vec<T>
    {
        const TYPE_NAME: &'static str = "Vec<T>";

        fn read_field(read: &mut dyn Read,
                      settings: &Settings,
                      hints: &mut hint::Hints) -> Result<Self, Error> {
            util::read_list(read, settings, hints)
        }

        fn write_field(&self,
                       write: &mut dyn Write,
                       settings: &Settings,
                       hints: &mut hint::Hints) -> Result<(), Error> {
            util::write_list(self.iter(), write, settings, hints)
        }
    }
}