shrupl/src/file/chunk.rs

66 lines
1.5 KiB
Rust
Raw Normal View History

use std::fmt;
2025-06-25 23:15:33 +00:00
use crate::sharry;
pub struct Chunk<'t> {
2025-06-25 23:15:33 +00:00
file_id: sharry::FileID,
offset: u64,
data: &'t [u8],
}
impl fmt::Debug for Chunk<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Chunk")
.field("file_id", &self.file_id)
.field("offset", &self.offset)
.field("data.len()", &self.data.len())
.finish_non_exhaustive()
}
}
impl<'t> Chunk<'t> {
pub(super) fn new_direct(file_id: sharry::FileID, offset: u64, data: &'t [u8]) -> Self {
Self {
file_id,
offset,
data,
}
}
2025-06-25 23:15:33 +00:00
pub fn get_file_id(&self) -> &sharry::FileID {
&self.file_id
}
pub fn get_offset(&self) -> u64 {
self.offset
}
pub fn get_data(&self) -> &[u8] {
self.data
}
pub(super) fn get_length(&self) -> u64 {
let len = self.data.len();
// BOOKMARK this might **panic** on platforms where `usize` has more than 64 bit.
// Also, you've allocated more than 2 EiB ... in ONE chunk.
// Whoa! Maybe just chill?
u64::try_from(len).unwrap_or_else(|e| panic!("usize={len} did not fit into u64: {e}"))
}
pub fn get_behind(&self) -> u64 {
self.offset + self.get_length()
}
}
#[cfg(test)]
mod tests {
// use super::*;
// #[test]
// fn basic_tests() {
// let mut foo = [0u8; 10];
// let fid = sharry::FileID("fid".to_string());
// }
}