use crate::mode::SampleDepth;
#[derive(Clone, Debug)]
pub struct RenderBuf {
depth: SampleDepth,
data: Box<[u8]>,
}
impl RenderBuf {
pub const MAX_LEN: u64 = u64::try_from(isize::MAX).unwrap_or(u64::MAX);
#[inline]
#[must_use]
pub fn new(depth: SampleDepth, len: u64) -> Self {
let size = depth.as_u64()
.checked_mul(len)
.and_then(|size| usize::try_from(size).ok())
.expect("overflow when computing render buffer size");
let data = vec![0x00; size].into();
Self { depth, data }
}
#[inline]
#[must_use]
pub fn get(&self, index: u64) -> Option<&[u8]> {
let start: usize = index.checked_mul(self.depth.as_u64())?
.try_into()
.ok()?;
let end = start.checked_add(self.depth.as_usize())?;
self.data.get(start..end)
}
#[inline]
#[must_use]
pub fn get_mut(&mut self, index: u64) -> Option<&mut [u8]> {
let start: usize = index.checked_mul(self.depth.as_u64())?
.try_into()
.ok()?;
let end = start.checked_add(self.depth.as_usize())?;
self.data.get_mut(start..end)
}
#[inline]
pub fn clear(&mut self) {
self.data.fill(Default::default())
}
#[inline]
#[must_use]
pub fn len(&self) -> u64 {
self.data.len() as u64 / self.depth.as_u64()
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
#[inline]
#[must_use]
pub const fn data(&self) -> &[u8] {
&self.data
}
#[inline]
#[must_use]
pub const fn data_mut(&mut self) -> &mut [u8] {
&mut self.data
}
}
impl Drop for RenderBuf {
#[inline(always)]
fn drop(&mut self) {}
}