2025-06-13 16:02:37 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
|
2025-06-25 23:15:33 +00:00
|
|
|
use crate::sharry;
|
|
|
|
|
|
2025-06-13 22:27:15 +00:00
|
|
|
pub struct Chunk<'t> {
|
2025-06-25 23:15:33 +00:00
|
|
|
file_id: sharry::FileID,
|
2025-06-10 23:39:08 +00:00
|
|
|
offset: u64,
|
2025-06-18 13:09:34 +00:00
|
|
|
data: &'t [u8],
|
2025-06-10 23:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-13 22:27:15 +00:00
|
|
|
impl fmt::Debug for Chunk<'_> {
|
2025-06-13 16:02:37 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.debug_struct("Chunk")
|
2025-06-18 13:09:34 +00:00
|
|
|
.field("file_id", &self.file_id)
|
2025-06-13 16:02:37 +00:00
|
|
|
.field("offset", &self.offset)
|
2025-06-13 22:27:15 +00:00
|
|
|
.field("data.len()", &self.data.len())
|
2025-06-13 16:02:37 +00:00
|
|
|
.finish_non_exhaustive()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 22:27:15 +00:00
|
|
|
impl<'t> Chunk<'t> {
|
2025-07-03 22:46:19 +00:00
|
|
|
pub(super) fn new_direct(file_id: sharry::FileID, offset: u64, data: &'t [u8]) -> Self {
|
2025-06-10 23:39:08 +00:00
|
|
|
Self {
|
2025-06-18 13:09:34 +00:00
|
|
|
file_id,
|
2025-06-10 23:39:08 +00:00
|
|
|
offset,
|
2025-06-18 13:09:34 +00:00
|
|
|
data,
|
2025-06-10 23:39:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-25 23:15:33 +00:00
|
|
|
pub fn get_file_id(&self) -> &sharry::FileID {
|
2025-06-18 13:09:34 +00:00
|
|
|
&self.file_id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_offset(&self) -> u64 {
|
|
|
|
|
self.offset
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 22:27:15 +00:00
|
|
|
pub fn get_data(&self) -> &[u8] {
|
|
|
|
|
self.data
|
2025-06-10 23:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-03 22:46:19 +00:00
|
|
|
pub(super) fn get_length(&self) -> u64 {
|
2025-06-10 23:39:08 +00:00
|
|
|
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?
|
2025-06-11 00:06:08 +00:00
|
|
|
u64::try_from(len).unwrap_or_else(|e| panic!("usize={len} did not fit into u64: {e}"))
|
2025-06-10 23:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-18 13:09:34 +00:00
|
|
|
pub fn get_behind(&self) -> u64 {
|
|
|
|
|
self.offset + self.get_length()
|
2025-06-10 23:39:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-07-05 01:24:53 +00:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
// use super::*;
|
|
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
|
// fn basic_tests() {
|
|
|
|
|
// let mut foo = [0u8; 10];
|
|
|
|
|
// let fid = sharry::FileID("fid".to_string());
|
|
|
|
|
// }
|
|
|
|
|
}
|