use std::{iter::FusedIterator, ops::ControlFlow};
use o3::buffer::{bytes::scope, resident, segments::Source, storage, write::Slice};
use crate::{
owner::{handoff, handoff::stage},
payload::owned,
};
mod sealed;
pub(crate) use sealed::Sealed;
pub trait Body<'d>: Sized + Sealed {
type Bound: resident::bound::Bound + 'd;
type Selected<S, B>: Into<handoff::Selection<'d, S, B>>;
#[doc(hidden)]
const INLINE: bool = false;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn as_slice(&self) -> Option<&[u8]>;
fn segments(&self) -> impl Iterator<Item = &[u8]> + FusedIterator {
self.as_slice().filter(|body| !body.is_empty()).into_iter()
}
#[doc(hidden)]
fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
self,
slot: &handoff::Slot<'req, 'd, H, O>,
) -> Result<Self::Selected<H::Selection, O>, owned::Error>;
#[doc(hidden)]
fn try_resolve_into<
'req,
H: scope::Source,
O: stage::Body<'req, 'd>,
L: std::borrow::BorrowMut<usize>,
>(
self,
slot: &handoff::Slot<'req, 'd, H, O>,
_: &mut Slice<'_, L>,
) -> Result<ControlFlow<usize, Self::Selected<H::Selection, O>>, owned::Error> {
Ok(ControlFlow::Continue(self.try_resolve(slot)?))
}
}
impl<'d> Body<'d> for [u8; 0] {
type Bound = resident::bound::AtMost<0>;
type Selected<S, B> = handoff::Selection<'d, S, B>;
const INLINE: bool = true;
fn len(&self) -> usize {
<[u8]>::len(self)
}
fn as_slice(&self) -> Option<&[u8]> {
Some(self)
}
fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
self,
slot: &handoff::Slot<'req, 'd, H, O>,
) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
match self {
[] => Ok(slot.clear()),
}
}
}
impl<'d, T: AsRef<[u8]> + ?Sized> Body<'d> for &T
where
Self: Sealed,
{
type Bound = resident::bound::Unbounded;
type Selected<S, B> = handoff::Selection<'d, S, B>;
const INLINE: bool = true;
fn len(&self) -> usize {
self.as_ref().len()
}
fn as_slice(&self) -> Option<&[u8]> {
Some(self.as_ref())
}
fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
self,
slot: &handoff::Slot<'req, 'd, H, O>,
) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
handoff::Selection::try_borrow(slot, self.as_ref())
}
fn try_resolve_into<
'req,
H: scope::Source,
O: stage::Body<'req, 'd>,
L: std::borrow::BorrowMut<usize>,
>(
self,
slot: &handoff::Slot<'req, 'd, H, O>,
out: &mut Slice<'_, L>,
) -> Result<ControlFlow<usize, handoff::Selection<'d, H::Selection, O>>, owned::Error> {
handoff::Selection::try_borrow_into(slot, self.as_ref(), out)
}
}
impl<'d> Body<'d> for Vec<u8> {
type Bound = resident::bound::Unbounded;
type Selected<S, B> = Option<storage::NonEmpty<Vec<u8>>>;
const INLINE: bool = true;
fn len(&self) -> usize {
self.as_slice().len()
}
fn as_slice(&self) -> Option<&[u8]> {
Some(self.as_slice())
}
fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
self,
_: &handoff::Slot<'req, 'd, H, O>,
) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
Ok(match storage::NonEmpty::try_new(self) {
Ok(body) => Some(body),
Err(empty) => {
drop(empty);
None
}
})
}
}
impl<'d> Body<'d> for storage::NonEmpty<Vec<u8>> {
type Bound = resident::bound::Unbounded;
type Selected<S, B> = Self;
const INLINE: bool = true;
fn len(&self) -> usize { storage::NonEmpty::len(self).get() }
fn is_empty(&self) -> bool { false }
fn as_slice(&self) -> Option<&[u8]> { Some(self.as_ref()) }
fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
self,
_: &handoff::Slot<'req, 'd, H, O>,
) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
Ok(self)
}
}
impl<'d> Body<'d> for String {
type Bound = resident::bound::Unbounded;
type Selected<S, B> = Option<storage::NonEmpty<Vec<u8>>>;
const INLINE: bool = true;
fn len(&self) -> usize {
String::len(self)
}
fn as_slice(&self) -> Option<&[u8]> {
Some(self.as_bytes())
}
fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
self,
slot: &handoff::Slot<'req, 'd, H, O>,
) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
self.into_bytes().try_resolve(slot)
}
}
impl<'d> Body<'d> for storage::Owned {
type Bound = resident::bound::Unbounded;
type Selected<S, B> = handoff::Selection<'d, S, B>;
fn len(&self) -> usize {
self.as_ref().len()
}
fn as_slice(&self) -> Option<&[u8]> {
Some(self.as_ref())
}
fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
self,
slot: &handoff::Slot<'req, 'd, H, O>,
) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
Ok(slot.shared(self.freeze()))
}
}
impl<'d> Body<'d> for storage::Shared {
type Bound = resident::bound::Unbounded;
type Selected<S, B> = handoff::Selection<'d, S, B>;
fn len(&self) -> usize {
self.as_ref().len()
}
fn as_slice(&self) -> Option<&[u8]> {
Some(self.as_ref())
}
fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
self,
slot: &handoff::Slot<'req, 'd, H, O>,
) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
Ok(slot.shared(self))
}
}
impl<'d> Body<'d> for resident::Shared<'d> {
type Bound = resident::bound::Unbounded;
type Selected<S, B> = handoff::Selection<'d, S, B>;
fn len(&self) -> usize {
self.as_slice().len()
}
fn as_slice(&self) -> Option<&[u8]> {
Some(self.as_slice())
}
fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
self,
slot: &handoff::Slot<'req, 'd, H, O>,
) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
Ok(slot.segmented(Source::from(self)))
}
}
impl<'d> Body<'d> for Source<'d> {
type Bound = resident::bound::Unbounded;
type Selected<S, B> = handoff::Selection<'d, S, B>;
fn len(&self) -> usize {
Source::len(self)
}
fn as_slice(&self) -> Option<&[u8]> {
self.contiguous()
}
fn segments(&self) -> impl Iterator<Item = &[u8]> + FusedIterator {
Source::segments(self)
}
fn try_resolve<'req, H: scope::Source, O: stage::Body<'req, 'd>>(
self,
slot: &handoff::Slot<'req, 'd, H, O>,
) -> Result<Self::Selected<H::Selection, O>, owned::Error> {
Ok(slot.segmented(self))
}
}