use std::io::{self, BufWriter, Write};
use std::marker::PhantomData;
use crate::{
FIXED_HEADER_SIZE, MAGIC, SLAB_TABLE_ENTRY_SIZE, SlabPlaceholder, SlabPrimitive, SlabType,
VERSION,
};
pub trait StreamedSlab {
fn slab_type(&self) -> SlabType;
fn byte_length(&self) -> usize;
fn write(self: Box<Self>, w: &mut dyn Write) -> io::Result<()>;
}
fn write_le_iter<T, It>(iter: It, w: &mut dyn Write) -> io::Result<()>
where
T: SlabPrimitive,
It: Iterator<Item = T>,
{
let mut bw = BufWriter::with_capacity(4096, w);
for elem in iter {
bw.write_all(elem.to_le_bytes().as_ref())?;
}
bw.flush()
}
fn write_le_iter_exact<T, It>(
mut iter: It,
count: usize,
slab_index: usize,
w: &mut dyn Write,
) -> io::Result<()>
where
T: SlabPrimitive,
It: Iterator<Item = T>,
{
let mut bw = BufWriter::with_capacity(4096, w);
let mut written = 0usize;
while written < count {
let Some(elem) = iter.next() else {
panic!(
"slab {slab_index}: iterator passed to add_slab_from_iter yielded \
{written} items, but {count} were declared"
);
};
bw.write_all(elem.to_le_bytes().as_ref())?;
written += 1;
}
if iter.next().is_some() {
panic!(
"slab {slab_index}: iterator passed to add_slab_from_iter yielded \
more than the {count} declared items"
);
}
bw.flush()
}
struct IterSlab<T, It> {
iter: It,
count: usize,
slab_index: usize,
_t: PhantomData<T>,
}
impl<T, It> StreamedSlab for IterSlab<T, It>
where
T: SlabPrimitive,
It: Iterator<Item = T>,
{
fn slab_type(&self) -> SlabType {
T::SLAB_TYPE
}
fn byte_length(&self) -> usize {
self.count * std::mem::size_of::<T>()
}
fn write(self: Box<Self>, w: &mut dyn Write) -> io::Result<()> {
write_le_iter_exact(self.iter, self.count, self.slab_index, w)
}
}
fn write_slice_le<T: SlabPrimitive>(slice: &[T], w: &mut dyn Write) -> io::Result<()> {
if cfg!(target_endian = "little") {
let bytes = unsafe {
std::slice::from_raw_parts(slice.as_ptr() as *const u8, std::mem::size_of_val(slice))
};
w.write_all(bytes)
} else {
write_le_iter(slice.iter().copied(), w)
}
}
struct SliceSlab<'a, T: SlabPrimitive> {
slice: &'a [T],
}
impl<T: SlabPrimitive> StreamedSlab for SliceSlab<'_, T> {
fn slab_type(&self) -> SlabType {
T::SLAB_TYPE
}
fn byte_length(&self) -> usize {
std::mem::size_of_val(self.slice)
}
fn write(self: Box<Self>, w: &mut dyn Write) -> io::Result<()> {
write_slice_le(self.slice, w)
}
}
struct VecSlab<T: SlabPrimitive>(Vec<T>);
impl<T: SlabPrimitive> StreamedSlab for VecSlab<T> {
fn slab_type(&self) -> SlabType {
T::SLAB_TYPE
}
fn byte_length(&self) -> usize {
std::mem::size_of_val(&self.0[..])
}
fn write(self: Box<Self>, w: &mut dyn Write) -> io::Result<()> {
write_slice_le(&self.0, w)
}
}
struct JsonSlab(Vec<u8>);
impl StreamedSlab for JsonSlab {
fn slab_type(&self) -> SlabType {
SlabType::Json
}
fn byte_length(&self) -> usize {
self.0.len()
}
fn write(self: Box<Self>, w: &mut dyn Write) -> io::Result<()> {
w.write_all(&self.0)
}
}
pub struct Builder<'a> {
entries: Vec<Box<dyn StreamedSlab + 'a>>,
}
impl<'a> Builder<'a> {
pub fn new() -> Self {
Self {
entries: Vec::new(),
}
}
pub fn add_slab<T>(&mut self, values: &'a [T]) -> SlabPlaceholder
where
T: SlabPrimitive + 'a,
{
self.add_streamed_slab(Box::new(SliceSlab { slice: values }))
}
pub fn add_slab_from_vec<T>(&mut self, values: Vec<T>) -> SlabPlaceholder
where
T: SlabPrimitive + 'a,
{
self.add_streamed_slab(Box::new(VecSlab(values)))
}
pub fn add_slab_from_iter<T, I>(&mut self, count: usize, iter: I) -> SlabPlaceholder
where
T: SlabPrimitive + 'a,
I: IntoIterator<Item = T>,
I::IntoIter: 'a,
{
self.add_streamed_slab(Box::new(IterSlab::<T, I::IntoIter> {
iter: iter.into_iter(),
count,
slab_index: self.next_slab_index(),
_t: PhantomData,
}))
}
pub fn add_json_slab(&mut self, bytes: Vec<u8>) -> SlabPlaceholder {
self.add_streamed_slab(Box::new(JsonSlab(bytes)))
}
pub fn add_streamed_slab(&mut self, s: Box<dyn StreamedSlab + 'a>) -> SlabPlaceholder {
let idx = self.next_slab_index();
self.entries.push(s);
SlabPlaceholder(idx)
}
fn next_slab_index(&self) -> usize {
self.entries.len() + 1
}
pub fn finish(self, root_json: &[u8]) -> Vec<u8> {
let mut buf = Vec::new();
self.to_writer(root_json, &mut buf)
.expect("writing to a Vec is infallible");
buf
}
pub fn to_writer<W: Write>(self, root_json: &[u8], w: &mut W) -> io::Result<()> {
write_jslb(self.entries, root_json, w)
}
}
impl Default for Builder<'_> {
fn default() -> Self {
Self::new()
}
}
fn write_jslb<W: Write>(
entries: Vec<Box<dyn StreamedSlab + '_>>,
root_json: &[u8],
w: &mut W,
) -> io::Result<()> {
let slab_count = entries.len() + 1;
let root_json_slab_index: u32 = 0;
let slab_table_end = FIXED_HEADER_SIZE + slab_count * SLAB_TABLE_ENTRY_SIZE;
let mut header_rows: Vec<(SlabType, usize, usize)> = Vec::with_capacity(slab_count);
let mut pos = slab_table_end;
let align = SlabType::Json.element_size();
pos = (pos + align - 1) & !(align - 1);
header_rows.push((SlabType::Json, pos, root_json.len()));
pos += root_json.len();
for e in &entries {
let ty = e.slab_type();
let align = ty.element_size();
pos = (pos + align - 1) & !(align - 1);
let len = e.byte_length();
header_rows.push((ty, pos, len));
pos += len;
}
w.write_all(&MAGIC)?;
w.write_all(&VERSION.to_le_bytes())?;
w.write_all(&(slab_count as u32).to_le_bytes())?;
w.write_all(&root_json_slab_index.to_le_bytes())?;
for &(ty, offset, len) in &header_rows {
w.write_all(&(ty as u32).to_le_bytes())?;
w.write_all(&(offset as u32).to_le_bytes())?;
w.write_all(&(len as u32).to_le_bytes())?;
}
let mut written = slab_table_end;
let mut rows = header_rows.into_iter();
let (_, root_offset, root_len) = rows.next().expect("root row");
write_padding(w, root_offset - written)?;
written = root_offset;
w.write_all(root_json)?;
written += root_len;
for (entry, (_, offset, len)) in entries.into_iter().zip(rows) {
write_padding(w, offset - written)?;
written = offset;
entry.write(w)?;
written += len;
}
debug_assert_eq!(written, pos, "planned and actual file length disagree");
Ok(())
}
fn write_padding<W: Write>(w: &mut W, n: usize) -> io::Result<()> {
const PAD: [u8; 8] = [0; 8];
let mut remaining = n;
while remaining > 0 {
let chunk = remaining.min(PAD.len());
w.write_all(&PAD[..chunk])?;
remaining -= chunk;
}
Ok(())
}