1use crate::Separator;
2
3#[derive(Debug, Clone, Copy)]
5pub struct Chunk {
6 pub index: u64,
8
9 pub size: u64,
11
12 pub separator_hash: u64,
14}
15
16#[derive(Debug)]
18pub struct ChunkIter<Iter> {
19 separators: Iter,
21
22 stream_length: u64,
24
25 last_separator_index: u64,
27}
28
29impl<Iter: Iterator<Item = Separator>> ChunkIter<Iter> {
30 pub fn new(iter: Iter, stream_length: u64) -> Self {
37 Self {
38 separators: iter,
39 stream_length,
40 last_separator_index: 0,
41 }
42 }
43}
44
45impl<Iter: Iterator<Item = Separator>> Iterator for ChunkIter<Iter> {
46 type Item = Chunk;
47
48 fn next(&mut self) -> Option<Self::Item> {
49 if let Some(separator) = self.separators.next() {
50 let chunk_size = separator.index - self.last_separator_index;
51 self.last_separator_index = separator.index;
52 Some(Chunk {
53 index: self.last_separator_index,
54 size: chunk_size,
55 separator_hash: separator.hash,
56 })
57 } else {
58 let chunk_size = self.stream_length - self.last_separator_index;
59 self.last_separator_index = self.stream_length;
60 if chunk_size > 0 {
61 Some(Chunk {
62 index: self.last_separator_index,
63 size: chunk_size,
64 separator_hash: 0, })
66 } else {
67 None
68 }
69 }
70 }
71}