externally_driven_async_rt/net/
borrowed_read_half.rs1use crate::{io::AsyncRead, selector::Direction};
2
3use super::{NetError, socket::Socket};
4
5#[must_use]
7pub struct BorrowedReadHalf<'s, 'sel, const FDS: usize> {
8 socket: &'s Socket<'sel, FDS>,
9}
10
11impl<'s, 'sel, const FDS: usize> BorrowedReadHalf<'s, 'sel, FDS> {
12 pub(super) const fn new(socket: &'s Socket<'sel, FDS>) -> Self {
13 Self { socket }
14 }
15
16 pub async fn readable(&mut self) {
25 self.socket.ready(Direction::Read).await;
26 }
27
28 pub fn try_read(&self, buf: &mut [u8]) -> rustix::io::Result<usize> {
37 self.socket.try_recv(buf)
38 }
39}
40
41impl<const FDS: usize> AsyncRead for BorrowedReadHalf<'_, '_, FDS> {
42 type Error = NetError;
43
44 fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> impl Future<Output = Result<usize, NetError>> {
45 self.socket.recv(buf)
46 }
47}