tape_api/
types.rs

1use steel::*;
2use crate::consts::*;
3use crate::utils::*;
4
5#[derive(Debug, Clone, Copy)]
6pub struct Chunk([u8; CHUNK_SIZE]);
7
8impl Chunk {
9    pub fn as_bytes(&self) -> &[u8; CHUNK_SIZE] {
10        &self.0
11    }
12}
13
14#[derive(Debug, Clone, Copy)]
15pub struct Segment([u8; SEGMENT_SIZE]);
16
17impl Segment {
18    pub fn try_from_bytes(data: &[u8]) -> Result<Self, ProgramError> {
19        if data.len() > SEGMENT_SIZE {
20            return Err(ProgramError::InvalidArgument);
21        }
22        Ok(Self(padded_array::<SEGMENT_SIZE>(data)))
23    }
24
25    pub fn chunks(&self) -> impl Iterator<Item = Chunk> + '_ {
26        self.0
27            .chunks(CHUNK_SIZE)
28            .map(|chunk| Chunk(chunk.try_into().expect("Chunk size mismatch")))
29    }
30}