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
use crate::prelude::*;
use std::vec::IntoIter;

#[cfg(feature = "write")]
impl<T: Writable> Writable for Vec<T> {
    type WriterArray = VecArrayWriter<T::WriterArray>;
    fn write_root<O: EncodeOptions>(&self, stream: &mut WriterStream<'_, O>) -> RootTypeId {
        profile!("write_root");
        match self.len() {
            0 => RootTypeId::Array0,
            1 => {
                stream.write_with_id(|stream| (&self[0]).write_root(stream));
                RootTypeId::Array1
            }
            _ => {
                // TODO: Seems kind of redundant to have both the array len,
                // and the bytes len. Though, it's not for obvious reasons.
                // Maybe sometimes we can infer from context. Eg: bool always
                // requires the same number of bits per item
                write_usize(self.len(), stream);

                // TODO: When there are types that are already
                // primitive (eg: Vec<f64>) it doesn't make sense
                // to buffer at this level. Specialization may
                // be useful here.
                //
                // TODO: See below, and just call buffer on the vec
                // and flush it!
                let mut writer = T::WriterArray::default();
                for item in self {
                    writer.buffer(item);
                }

                stream.write_with_id(|stream| writer.flush(stream));

                RootTypeId::ArrayN
            }
        }
    }
}

#[cfg(feature = "read")]
impl<T: Readable> Readable for Vec<T>
// Overly verbose because of `?` requiring `From` See also ec4fa3ba-def5-44eb-9065-e80b59530af6
where
    ReadError: From<<<T as Readable>::ReaderArray as ReaderArray>::Error>,
{
    type ReaderArray = Option<VecArrayReader<T::ReaderArray>>;
    fn read(sticks: DynRootBranch<'_>, options: &impl DecodeOptions) -> ReadResult<Self> {
        profile!("Readable::read");
        match sticks {
            DynRootBranch::Array0 => Ok(Vec::new()),
            DynRootBranch::Array1(inner) => {
                let inner = T::read(*inner, options)?;
                Ok(vec![inner])
            }
            DynRootBranch::Array { len, values } => {
                let mut v = Vec::with_capacity(len);
                // TODO: Some of what the code is actually doing here is silly.
                // Actual ReaderArray's may be IntoIter, which moved out of a Vec
                // that we wanted in the first place. Specialization here would be nice.
                let mut reader = T::ReaderArray::new(values, options)?;
                for _ in 0..len {
                    v.push(reader.read_next()?);
                }
                Ok(v)
            }
            _ => Err(ReadError::SchemaMismatch),
        }
    }
}

#[cfg(feature = "write")]
#[derive(Debug, Default)]
pub struct VecArrayWriter<T> {
    // TODO: usize
    len: <u64 as Writable>::WriterArray,
    // Using Option here enables recursion when necessary.
    values: Option<T>,
}

// TODO: usize
enum FixedOrVariableLength {
    Fixed(usize),
    Variable(IntoIter<u64>),
}

impl FixedOrVariableLength {
    fn next(&mut self) -> usize {
        match self {
            Self::Fixed(v) => *v,
            Self::Variable(i) => i.read_next_infallible() as usize,
        }
    }
}

#[cfg(feature = "read")]
pub struct VecArrayReader<T> {
    len: FixedOrVariableLength,
    values: T,
}

#[cfg(feature = "write")]
impl<T: Writable> WriterArray<Vec<T>> for VecArrayWriter<T::WriterArray> {
    fn buffer<'a, 'b: 'a>(&'a mut self, value: &'b Vec<T>) {
        // TODO: Consider whether buffer should actually just
        // do something non-flat, (like literally push the Vec<T> into another Vec<T>)
        // and the flattening could happen later at flush time. This may reduce memory cost.
        // Careful though.
        // I feel though that somehow this outer buffer type
        // could fix the specialization problem above for single-vec
        // values.
        self.len.buffer(&(value.len() as u64));
        let values = self.values.get_or_insert_with(Default::default);
        for item in value {
            values.buffer(item);
        }
    }
    fn flush<O: EncodeOptions>(self, stream: &mut WriterStream<'_, O>) -> ArrayTypeId {
        profile!("flush");
        let Self { len, values } = self;
        if let Some(values) = values {
            if len.iter().all(|l| *l == len[0]) {
                write_usize(len[0] as usize, stream);
                stream.write_with_id(|stream| values.flush(stream));
                return ArrayTypeId::ArrayFixed;
            }
            // TODO: Consider an all-0 type // See also: 84d15459-35e4-4f04-896f-0f4ea9ce52a9
            stream.write_with_id(|stream| len.flush(stream));
            stream.write_with_id(|stream| values.flush(stream));
        } else {
            stream.write_with_id(|_| ArrayTypeId::Void);
        }

        ArrayTypeId::ArrayVar
    }
}

#[cfg(feature = "read")]
impl<T: ReaderArray> ReaderArray for Option<VecArrayReader<T>> {
    type Read = Vec<T::Read>;
    type Error = T::Error;

    fn new(sticks: DynArrayBranch<'_>, options: &impl DecodeOptions) -> ReadResult<Self> {
        profile!("ReaderArray::new");

        match sticks {
            DynArrayBranch::Array0 => Ok(None),
            DynArrayBranch::Array { len, values } => {
                let (values, len) = parallel(|| T::new(*values, options), || <<u64 as Readable>::ReaderArray as ReaderArray>::new(*len, options), options);
                let values = values?;
                let len = FixedOrVariableLength::Variable(len?);
                Ok(Some(VecArrayReader { len, values }))
            }
            DynArrayBranch::ArrayFixed { len, values } => Ok(if len == 0 {
                None
            } else {
                let len = FixedOrVariableLength::Fixed(len);
                let values = T::new(*values, options)?;
                Some(VecArrayReader { len, values })
            }),
            _ => Err(ReadError::SchemaMismatch),
        }
    }
    fn read_next(&mut self) -> Result<Self::Read, Self::Error> {
        if let Some(inner) = self {
            let len = inner.len.next();
            let mut result = Vec::with_capacity(len);
            for _ in 0..len {
                result.push(inner.values.read_next()?);
            }
            Ok(result)
        } else {
            Ok(Vec::new())
        }
    }
}