shrupl/src/file/chunk.rs

51 lines
1.2 KiB
Rust
Raw Normal View History

use std::fmt;
pub struct Chunk {
data: *const [u8],
patch_uri: String,
offset: u64,
}
impl fmt::Debug for Chunk {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Chunk")
.field("patch_uri", &self.patch_uri)
.field("offset", &self.offset)
.field("data_len", &self.data.len())
.finish_non_exhaustive()
}
}
impl Chunk {
pub fn new(data: &[u8], patch_uri: String, offset: u64) -> Self {
let data: *const [u8] = data as *const [u8];
Self {
data,
patch_uri,
offset,
}
}
pub unsafe fn get_data(&self) -> &[u8] {
unsafe { &*self.data }
}
pub 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_patch_uri(&self) -> &str {
&self.patch_uri
}
pub fn get_offset(&self) -> u64 {
self.offset
}
}