use crate::buffer::{self, pool, pool::owners, write::consume::Error};
#[must_use = "commit publishes the admitted source"]
pub struct Append<'a, I, L = pool::Layout, M = pool::Owned>
where
owners::Plain<L>: buffer::raw::Backing,
{
pub(in crate::buffer::pool) target: &'a mut pool::Span<owners::Plain<L>, M>,
pub(in crate::buffer::pool) input: owners::Measured<I>,
}
impl<'a, I, L, M> Append<'a, I, L, M>
where
owners::Plain<L>: buffer::raw::Backing,
{
pub fn commit(self)
where
I: buffer::raw::Transfer,
{
let target = self.target.slots.data(self.target.key);
let start = self.target.len();
unsafe { self.input.input().copy_to(target.add(start)) };
self.target.len = (start + self.input.len()) as u32;
}
pub(in crate::buffer::pool) fn try_lease(
target: &'a mut pool::Span<owners::Plain<L>, M>,
input: owners::Measured<I>,
) -> Result<Self, Error> {
let spare = target.capacity().get() as usize - target.len();
Error::check(input.len(), spare)?;
Ok(Self { target, input })
}
pub fn as_slice(&self) -> &[u8] {
self.target.as_slice()
}
pub fn input(&self) -> &I {
self.input.input()
}
}
impl<'a, 'source, L, M, const P: usize, const N: usize>
Append<'a, ([u8; P], [&'source [u8]; N]), L, M>
where
owners::Plain<L>: buffer::raw::Backing,
{
pub fn try_prefixed(
target: &'a mut pool::Span<owners::Plain<L>, M>,
prefix: [u8; P],
sources: [&'source [u8]; N],
) -> Result<Self, Error> {
let input = owners::Measured::try_prefixed((prefix, sources))?;
Self::try_lease(target, input)
}
}
const _: () = {
assert!(size_of::<Append<'_, &[u8]>>() == 4 * size_of::<usize>());
assert!(
size_of::<Append<'_, &[u8], pool::Layout, pool::Borrowed<'_>>>() == 4 * size_of::<usize>()
);
assert!(size_of::<Append<'_, ([u8; 8], [&[u8]; 4])>>() == 11 * size_of::<usize>());
};