use dope_core::io::{socket::msg, transfer};
use o3::buffer::raw::Chunks as _;
use crate::wire::send::{self, Either, Payload, sources::plain::Slice};
pub struct Vectored<'a, const N: usize = { msg::MAX_IOVECS }> {
message: msg::Vectored<'a, N>,
}
impl<'a, const N: usize> Vectored<'a, N> {
pub(in crate::wire::send) fn chunks(&self) -> msg::raw::Cursor<'a> {
self.message.chunks()
}
pub fn from_message(msg: msg::Vectored<'a, N>) -> Self {
Self { message: msg }
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.message.is_empty()
}
#[must_use]
pub fn bytes(&self) -> transfer::Len {
self.message.bytes()
}
}
pub(in crate::wire::send) struct Vectors<'a> {
pub(in crate::wire::send) message: msg::Message<'a>,
pub(in crate::wire::send) bytes: transfer::NonZero,
}
impl<'scope, 'bytes: 'scope, const N: usize> Payload<'scope> for Vectored<'bytes, N> {
fn select(&self) -> Option<impl send::raw::Ready<'scope>> {
let bytes = transfer::NonZero::try_from(self.message.bytes()).ok()?;
Some(match self.message.contiguous() {
Some(view) => Either::Left(Slice { view, bytes }),
None => Either::Right(Vectors {
message: self.message.message(),
bytes,
}),
})
}
}