use crate::{Error, MemorySize, Result};
crate::model::storage::codec::product!(ByteRange {
offset: crate::model::storage::codec::Unsigned => 1,
limit: crate::model::storage::codec::Optional<crate::model::storage::codec::Unsigned> => 2,
});
crate::model::storage::codec::product!(FileBatch {
offset: crate::model::storage::codec::Unsigned => 1,
bytes: crate::model::storage::codec::Bytes => 2,
});
crate::model::storage::codec::product!(FileCompletion {
bytes: crate::model::storage::codec::Unsigned => 1,
});
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ByteRange {
pub offset: u64,
pub limit: Option<u64>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FileBatch {
pub offset: u64,
pub bytes: Vec<u8>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FileCompletion {
pub bytes: u64,
}
impl ByteRange {
pub fn end(self, length: u64) -> Result<u64> {
let remaining = length
.checked_sub(self.offset)
.ok_or(Error::InvalidInput { field: "file byte range", reason: "offset exceeds the immutable file length" })?;
self.offset
.checked_add(self.limit.unwrap_or(remaining).min(remaining))
.ok_or(Error::InvalidInput { field: "file byte range", reason: "byte range overflow" })
}
}
impl MemorySize for ByteRange {
fn memory_size(&self) -> usize {
std::mem::size_of::<Self>()
}
}
impl MemorySize for FileBatch {
fn memory_size(&self) -> usize {
std::mem::size_of::<Self>().saturating_add(self.bytes.capacity())
}
}
impl MemorySize for FileCompletion {
fn memory_size(&self) -> usize {
std::mem::size_of::<Self>()
}
}