externally_driven_async_rt/net/
borrowed_write_half.rs1use crate::{io::AsyncWrite, selector::Direction};
2
3use super::{NetError, socket::Socket};
4
5#[must_use]
7pub struct BorrowedWriteHalf<'s, 'sel, const FDS: usize> {
8 socket: &'s Socket<'sel, FDS>,
9}
10
11impl<'s, 'sel, const FDS: usize> BorrowedWriteHalf<'s, 'sel, FDS> {
12 pub(super) const fn new(socket: &'s Socket<'sel, FDS>) -> Self {
13 Self { socket }
14 }
15
16 pub async fn writable(&mut self) {
25 self.socket.ready(Direction::Write).await;
26 }
27
28 pub fn try_write(&self, buf: &[u8]) -> rustix::io::Result<usize> {
35 self.socket.try_send(buf)
36 }
37}
38
39impl<const FDS: usize> AsyncWrite for BorrowedWriteHalf<'_, '_, FDS> {
40 type Error = NetError;
41
42 fn write<'a>(&'a mut self, buf: &'a [u8]) -> impl Future<Output = Result<usize, NetError>> {
43 self.socket.send(buf)
44 }
45
46 fn flush(&mut self) -> impl Future<Output = Result<(), NetError>> {
47 core::future::ready(Ok(()))
48 }
49}