use crate::cell::MelinoeCell;
use super::{par_chunks::ParChunks, ShardChunks};
#[repr(transparent)]
pub struct WriterShard<'a, 'brand, T> {
cells: &'a mut [MelinoeCell<'brand, T>],
}
impl<'a, 'brand, T> WriterShard<'a, 'brand, T> {
#[inline]
#[must_use]
pub fn new(cells: &'a mut [MelinoeCell<'brand, T>]) -> Self {
Self { cells }
}
#[inline]
#[must_use]
pub const fn len(&self) -> usize {
self.cells.len()
}
#[inline]
#[must_use]
pub const fn is_empty(&self) -> bool {
self.cells.is_empty()
}
#[inline]
#[must_use]
pub fn as_slice(&self) -> &[T] {
let ptr = MelinoeCell::slice_as_unsafe_cell(self.cells).get();
unsafe { &*(ptr as *const [T]) }
}
#[inline]
#[must_use]
pub fn as_mut_slice(&mut self) -> &mut [T] {
let ptr = MelinoeCell::slice_as_unsafe_cell(self.cells).get();
unsafe { &mut *ptr }
}
#[inline]
#[must_use]
pub fn get(&self, index: usize) -> Option<&T> {
self.as_slice().get(index)
}
#[inline]
pub fn iter(&self) -> core::slice::Iter<'_, T> {
self.as_slice().iter()
}
#[inline]
#[must_use]
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
self.as_mut_slice().get_mut(index)
}
#[inline]
pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, T> {
self.as_mut_slice().iter_mut()
}
#[inline]
#[must_use]
pub fn split_at(self, mid: usize) -> (Self, Self) {
let (left, right) = self.cells.split_at_mut(mid);
(Self::new(left), Self::new(right))
}
#[inline]
pub fn chunks(self, chunk_size: usize) -> ShardChunks<'a, 'brand, T> {
ShardChunks {
rest: Some(self.cells),
chunk: chunk_size.max(1),
}
}
#[inline]
pub fn par_chunks(self, chunk_size: usize) -> ParChunks<'a, 'brand, T> {
ParChunks::new(self.cells, chunk_size)
}
#[inline]
#[must_use]
pub fn as_cells(&self) -> &[MelinoeCell<'brand, T>] {
self.cells
}
#[inline]
#[must_use]
pub fn into_cells(self) -> &'a mut [MelinoeCell<'brand, T>] {
self.cells
}
}
impl<'a, 'brand, T> core::fmt::Debug for WriterShard<'a, 'brand, T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("WriterShard")
.field("len", &self.cells.len())
.finish_non_exhaustive()
}
}
impl<'s, 'a, 'brand, T> IntoIterator for &'s WriterShard<'a, 'brand, T> {
type Item = &'s T;
type IntoIter = core::slice::Iter<'s, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.as_slice().iter()
}
}
impl<'s, 'a, 'brand, T> IntoIterator for &'s mut WriterShard<'a, 'brand, T> {
type Item = &'s mut T;
type IntoIter = core::slice::IterMut<'s, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.as_mut_slice().iter_mut()
}
}