use crate::{NotEnoughResources, context::Context, io::AsyncWrite, selector::Direction};
use super::{NetError, socket::Socket};
#[must_use]
pub struct BorrowedWriteHalf<'s> {
socket: &'s Socket,
}
impl<'s> BorrowedWriteHalf<'s> {
pub(super) const fn new(socket: &'s Socket) -> Self {
Self { socket }
}
pub async fn writable<
E: 'static,
const EVENTS: usize,
const TASKS: usize,
const TASK_SIZE: usize,
const ROOT_TASK_SIZE: usize,
const TIMERS: usize,
const FDS: usize,
>(
&mut self,
ctx: &Context<'_, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
) -> Result<(), NotEnoughResources> {
self.socket
.ready(&ctx.storage.io_waiters, ctx.selector, Direction::Write)
.await
}
pub fn try_write(&self, buf: &[u8]) -> rustix::io::Result<usize> {
self.socket.try_send(buf)
}
}
impl AsyncWrite for BorrowedWriteHalf<'_> {
type Error = NetError;
fn write<
'a,
E: 'static,
const EVENTS: usize,
const TASKS: usize,
const TASK_SIZE: usize,
const ROOT_TASK_SIZE: usize,
const TIMERS: usize,
const FDS: usize,
>(
&'a mut self,
ctx: &'a Context<'_, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
buf: &'a [u8],
) -> impl Future<Output = Result<usize, NetError>> {
self.socket.send(&ctx.storage.io_waiters, ctx.selector, buf)
}
fn flush<
'a,
E: 'static,
const EVENTS: usize,
const TASKS: usize,
const TASK_SIZE: usize,
const ROOT_TASK_SIZE: usize,
const TIMERS: usize,
const FDS: usize,
>(
&'a mut self,
_ctx: &'a Context<'_, E, EVENTS, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
) -> impl Future<Output = Result<(), NetError>> {
core::future::ready(Ok(()))
}
}