1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use crate::{NotEnoughResources, context::Context, io::AsyncRead, selector::Direction};
use super::{NetError, socket::Socket};
/// Read half of a TCP/Unix stream.
#[must_use]
pub struct BorrowedReadHalf<'s> {
socket: &'s Socket,
}
impl<'s> BorrowedReadHalf<'s> {
pub(super) const fn new(socket: &'s Socket) -> Self {
Self { socket }
}
/// Waits until the stream is readable.
///
/// The stream may still turn out to be not readable, so [`try_read`](Self::try_read)
/// can return [`Errno::AGAIN`](rustix::io::Errno::AGAIN) after it.
///
/// # Errors
///
/// Returns an error if all `FDS` fd slots are taken.
///
/// # Cancel safety
///
/// This method is cancel safe.
pub async fn readable<
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::Read)
.await
}
/// Reads into a given buffer without waiting, returns the number of read bytes.
///
/// Returns `0` if the stream is closed (or if the buffer is empty).
///
/// # Errors
///
/// Returns [`Errno::AGAIN`](rustix::io::Errno::AGAIN) if the stream is not readable,
/// or an error if the syscall fails.
pub fn try_read(&self, buf: &mut [u8]) -> rustix::io::Result<usize> {
self.socket.try_recv(buf)
}
}
impl AsyncRead for BorrowedReadHalf<'_> {
type Error = NetError;
fn read<
'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 mut [u8],
) -> impl Future<Output = Result<usize, NetError>> {
self.socket.recv(&ctx.storage.io_waiters, ctx.selector, buf)
}
}