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