djin_protocol/types/
vec.rs

1use crate::{hint, types, util, Parcel, Error, Settings};
2use std::io::prelude::*;
3use std;
4
5/// A newtype wrapping `Vec<T>` but with a custom length prefix type.
6#[derive(Clone, Debug, PartialEq)]
7pub struct Vec<S: types::Integer, T: Parcel>
8{
9    /// The inner `Vec<T>`.
10    pub elements: std::vec::Vec<T>,
11    _a: std::marker::PhantomData<S>,
12}
13
14impl<S: types::Integer, T: Parcel> Vec<S,T>
15{
16    /// Creates a new `Vec` from a list of elements.
17    pub fn new(elements: std::vec::Vec<T>) -> Self {
18        Vec { elements: elements, _a: std::marker::PhantomData }
19    }
20}
21
22impl<S: types::Integer, T: Parcel> Parcel for Vec<S, T>
23{
24    const TYPE_NAME: &'static str = "djin_protocol::Vec<S,T>";
25
26    fn read_field(read: &mut dyn Read,
27                  settings: &Settings,
28                  hints: &mut hint::Hints) -> Result<Self, Error> {
29        let elements = util::read_list_ext::<S,T>(read, settings, hints)?;
30        Ok(Self::new(elements))
31    }
32
33    fn write_field(&self, write: &mut dyn Write,
34                   settings: &Settings,
35                   hints: &mut hint::Hints) -> Result<(), Error> {
36        util::write_list_ext::<S,T,_>(self.elements.iter(), write, settings, hints)
37    }
38}
39
40/// Stuff relating to `std::vec::Vec<T>`.
41mod std_vec {
42    use crate::{hint, util, Error, Parcel, Settings};
43    use std::io::prelude::*;
44
45    impl<T: Parcel> Parcel for Vec<T>
46    {
47        const TYPE_NAME: &'static str = "Vec<T>";
48
49        fn read_field(read: &mut dyn Read,
50                      settings: &Settings,
51                      hints: &mut hint::Hints) -> Result<Self, Error> {
52            util::read_list(read, settings, hints)
53        }
54
55        fn write_field(&self,
56                       write: &mut dyn Write,
57                       settings: &Settings,
58                       hints: &mut hint::Hints) -> Result<(), Error> {
59            util::write_list(self.iter(), write, settings, hints)
60        }
61    }
62}
63