use o3::buffer::storage::Shared;
use o3::buffer::{self, resident, storage::NonEmpty};
#[must_use = "publish the response fragment or retain its owner"]
#[repr(transparent)]
pub struct Chunk<'d>(NonEmpty<Shared<resident::State<'d>>>);
#[must_use = "recover the rejected empty owner"]
#[derive(Debug)]
pub struct Empty<'d>(Shared<resident::State<'d>>);
impl<'d> Chunk<'d> {
pub fn try_new(owner: Shared<resident::State<'d>>) -> Result<Self, Empty<'d>> {
match NonEmpty::try_new(owner) {
Ok(owner) => Ok(Self(owner)),
Err(rejected) => Err(Empty(rejected.into_parts().0)),
}
}
pub fn owner(&self) -> &Shared<resident::State<'d>> {
self.0.as_inner()
}
pub fn into_owner(self) -> NonEmpty<Shared<resident::State<'d>>> {
self.0
}
}
impl<'d> Empty<'d> {
pub const fn owner(&self) -> &Shared<resident::State<'d>> {
&self.0
}
}
impl buffer::Residency for Chunk<'_> {
fn resident(&self) -> usize {
self.0.resident()
}
}
impl<'d, E> TryFrom<Result<Chunk<'d>, E>> for Chunk<'d> {
type Error = E;
fn try_from(item: Result<Self, E>) -> Result<Self, E> {
item
}
}
impl<'a: 'd, 'd, const N: usize> From<&'a [u8; N]> for Chunk<'d> {
fn from(bytes: &'a [u8; N]) -> Self {
let bytes: &'d [u8; N] = bytes;
Self(NonEmpty::from(bytes))
}
}
const _: () = {
assert!(size_of::<Chunk<'static>>() == size_of::<Shared<resident::State<'static>>>());
assert!(
size_of::<Option<Chunk<'static>>>()
== size_of::<Option<Shared<resident::State<'static>>>>()
);
};