use crate::ContentPath;
use std::{
fmt::Debug,
io::{Read, Seek, SeekFrom},
ops::{Deref, DerefMut},
};
pub trait ContentType: Copy + Eq + PartialEq + Debug + Ord + PartialOrd {
const COUNT: u16;
fn as_u16(&self) -> u16;
fn from_u16(value: u16) -> Option<Self>;
}
pub trait Content<T: ContentType> {
fn content_type(&self) -> Option<T> {
None
}
fn path(&self) -> &ContentPath;
fn size(&self) -> u64;
fn read(&mut self, offset: u64, count: u32) -> Option<&[u8]>;
}
impl ContentType for bool {
const COUNT: u16 = 2;
fn as_u16(&self) -> u16 {
*self as u16
}
fn from_u16(value: u16) -> Option<Self> {
if value == 0 {
Some(false)
} else if value == 1 {
Some(true)
} else {
None
}
}
}
#[derive(Debug, Copy, Clone)]
pub(crate) struct ContentPtr<T: ContentType> {
content: *mut dyn Content<T>,
}
impl<T: ContentType> ContentPtr<T> {
#[inline(always)]
pub(crate) fn new(content: &mut dyn Content<T>) -> Self {
Self {
content: unsafe { std::mem::transmute::<*mut dyn Content<T>, *mut (dyn Content<T> + 'static)>(content as *mut _) },
}
}
#[inline(always)]
pub(crate) fn as_mut(&mut self) -> &mut dyn Content<T> {
unsafe { &mut *self.content }
}
#[inline(always)]
pub(crate) fn as_ref(&self) -> &dyn Content<T> {
unsafe { &*self.content }
}
}
pub struct OwnedContentPtr<T: ContentType> {
content: *mut dyn Content<T>,
}
impl<T: ContentType> OwnedContentPtr<T> {
#[inline(always)]
pub(crate) fn new(content: ContentPtr<T>) -> Self {
Self { content: content.content }
}
}
impl<T: ContentType> Deref for OwnedContentPtr<T> {
type Target = dyn Content<T>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*self.content }
}
}
impl<T: ContentType> DerefMut for OwnedContentPtr<T> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *self.content }
}
}
pub struct ContentReader<T: ContentType> {
content: OwnedContentPtr<T>,
offset: u64,
}
impl<T: ContentType> ContentReader<T> {
pub fn new(content: OwnedContentPtr<T>) -> Self {
Self { content, offset: 0 }
}
}
impl<T: ContentType> Read for ContentReader<T> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
if buf.is_empty() {
return Ok(0);
}
let len = self.content.size();
if self.offset >= len {
return Ok(0); }
let remaining = len - self.offset;
let want = (buf.len() as u64).min(remaining).min(u32::MAX as u64) as u32;
match self.content.read(self.offset, want) {
Some(src) => {
let n = src.len().min(buf.len());
buf[..n].copy_from_slice(&src[..n]);
self.offset += n as u64;
Ok(n)
}
None => Err(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"Content::read failed before end of content",
)),
}
}
}
impl<T: ContentType> Seek for ContentReader<T> {
fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
let (base, delta) = match pos {
SeekFrom::Start(o) => {
self.offset = o;
return Ok(o);
}
SeekFrom::End(o) => (self.content.size(), o),
SeekFrom::Current(o) => (self.offset, o),
};
let new = if delta >= 0 {
base.checked_add(delta as u64)
} else {
base.checked_sub(delta.unsigned_abs())
};
match new {
Some(p) => {
self.offset = p;
Ok(p)
}
None => Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"seek to a negative or overflowing position",
)),
}
}
}