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
use super::Shard;
pub mod helper {
pub fn calc_offset(offset : Option<usize>) -> usize {
match offset {
Some(x) => x,
None => 0
}
}
}
pub fn make_zero_len_shard() -> Shard {
Box::new([])
}
pub fn make_zero_len_shards(count : usize) -> Vec<Shard> {
let mut result = Vec::with_capacity(count);
for _ in 0..count {
result.push(make_zero_len_shard());
}
result
}
pub fn make_blank_shard(size : usize) -> Shard {
vec![0; size].into_boxed_slice()
}
pub fn make_blank_shards(size : usize, count : usize) -> Vec<Shard> {
let mut result = Vec::with_capacity(count);
for _ in 0..count {
result.push(make_blank_shard(size));
}
result
}
pub fn shards_to_option_shards(shards : &Vec<Shard>)
-> Vec<Option<Shard>> {
let mut result = Vec::with_capacity(shards.len());
for v in shards.iter() {
let inner : Box<[u8]> = v.clone();
result.push(Some(inner));
}
result
}
pub fn shards_into_option_shards(shards : Vec<Shard>)
-> Vec<Option<Shard>> {
let mut result = Vec::with_capacity(shards.len());
for v in shards.into_iter() {
result.push(Some(v));
}
result
}
pub fn option_shards_to_shards(shards : &Vec<Option<Shard>>,
start : Option<usize>,
count : Option<usize>)
-> Vec<Shard> {
let offset = helper::calc_offset(start);
let count = match count {
None => shards.len(),
Some(x) => x
};
if shards.len() < offset + count {
panic!("Too few shards, number of shards : {}, offset + count : {}", shards.len(), offset + count);
}
let mut result = Vec::with_capacity(shards.len());
for i in offset..offset + count {
let shard = match shards[i] {
Some(ref x) => x,
None => panic!("Missing shard, index : {}", i),
};
let inner : Box<[u8]> = shard.clone();
result.push(inner);
}
result
}
pub fn option_shards_into_shards(shards : Vec<Option<Shard>>)
-> Vec<Shard> {
let mut result = Vec::with_capacity(shards.len());
for shard in shards.into_iter() {
let shard = match shard {
Some(x) => x,
None => panic!("Missing shard"),
};
result.push(shard);
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_zero_len_shard() {
assert_eq!(vec![].into_boxed_slice(), make_zero_len_shard());
}
#[test]
fn test_zero_len_shards() {
assert_eq!(vec![vec![].into_boxed_slice(), vec![].into_boxed_slice()],
make_zero_len_shards(2));
}
}