zc_rlnc/primitives/
mod.rs1pub mod field;
3pub mod packet;
4use field::Field;
5
6use crate::common::BOUNDARY_MARKER;
7
8#[derive(Debug)]
11pub struct Chunks<F: Field> {
12 inner: Vec<Chunk<F>>,
13 chunk_size: usize,
14}
15
16#[derive(Debug, thiserror::Error)]
18pub enum ChunksError {
19 #[error("data is empty")]
21 EmptyData,
22 #[error("chunk count is zero")]
24 ZeroChunkCount,
25 #[error("chunk size is zero")]
27 ZeroChunkSize,
28}
29
30impl<F: Field> Chunks<F> {
31 pub fn new(data: &[u8], chunk_count: usize) -> Result<Self, ChunksError> {
35 if data.is_empty() {
36 return Err(ChunksError::EmptyData);
37 }
38
39 if chunk_count == 0 {
40 return Err(ChunksError::ZeroChunkCount);
41 }
42
43 let mut data = Vec::from(data.as_ref());
44 data.push(BOUNDARY_MARKER);
45
46 let chunk_size = data.len().div_ceil(chunk_count);
48
49 let chunk_size = chunk_size.div_ceil(F::SAFE_CAPACITY) * F::SAFE_CAPACITY;
51 let padded_len = chunk_size * chunk_count;
52
53 data.resize(padded_len, 0);
55
56 let chunks = data.chunks_exact(chunk_size).map(Chunk::from_bytes).collect();
57
58 Ok(Self { inner: chunks, chunk_size })
59 }
60
61 pub fn chunk_size(&self) -> usize {
63 self.chunk_size
64 }
65
66 pub fn inner(&self) -> &[Chunk<F>] {
68 &self.inner
69 }
70
71 pub fn len(&self) -> usize {
73 self.inner.len()
74 }
75
76 pub fn is_empty(&self) -> bool {
78 self.inner.is_empty()
79 }
80}
81
82#[derive(Debug, Clone)]
84pub struct Chunk<F: Field> {
85 symbols: Vec<F>,
86 #[allow(unused)]
87 size: usize,
88}
89
90impl<F: Field> Chunk<F> {
91 pub(crate) fn from_bytes(bytes: &[u8]) -> Self {
94 let size = bytes.len();
95 Self { symbols: bytes.chunks(F::SAFE_CAPACITY).map(|c| F::from_bytes(c)).collect(), size }
96 }
97
98 pub(crate) fn symbols(&self) -> &[F] {
100 &self.symbols
101 }
102}