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
#[cfg(feature = "write")]
use crate::internal::encodings::varint::encode_prefix_varint;
use crate::prelude::*;
use std::vec::IntoIter;
#[cfg(feature = "write")]
impl<'a, T: Writable<'a>> Writable<'a> for Vec<T> {
type WriterArray = VecArrayWriter<'a, T::WriterArray>;
fn write_root<'b: 'a>(&'b self, stream: &mut impl WriterStream) -> RootTypeId {
match self.len() {
0 => RootTypeId::Array0,
1 => {
stream.write_with_id(|stream| (&self[0]).write_root(stream));
RootTypeId::Array1
}
_ => {
encode_prefix_varint(self.len() as u64, stream.bytes());
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> {
type ReaderArray = Option<VecArrayReader<T::ReaderArray>>;
fn read(sticks: DynRootBranch<'_>, options: &impl DecodeOptions) -> ReadResult<Self> {
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);
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<'a, T> {
len: <u64 as Writable<'a>>::WriterArray,
values: Option<T>,
}
#[cfg(feature = "read")]
pub struct VecArrayReader<T> {
len: IntoIter<u64>,
values: T,
}
#[cfg(feature = "write")]
impl<'a, T: WriterArray<'a>> WriterArray<'a> for VecArrayWriter<'a, T> {
type Write = Vec<T::Write>;
fn buffer<'b: 'a>(&mut self, value: &'b Self::Write) {
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(self, stream: &mut impl WriterStream) -> ArrayTypeId {
let Self { len, values } = self;
if let Some(values) = values {
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>;
fn new(sticks: DynArrayBranch<'_>, options: &impl DecodeOptions) -> ReadResult<Self> {
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 = len?;
Ok(Some(VecArrayReader { len, values }))
}
_ => Err(ReadError::SchemaMismatch),
}
}
fn read_next(&mut self) -> Self::Read {
if let Some(inner) = self {
let len = inner.len.read_next();
let mut result = Vec::with_capacity(len as usize);
for _ in 0..len {
result.push(inner.values.read_next());
}
result
} else {
Vec::new()
}
}
}