ctf_pwn/io/util/timeout/
async_read_timeout_ext.rs

1use crate::io::util::timeout::read_exact_timeout::{read_exact_timeout, ReadExactTimeout};
2use crate::io::util::timeout::read_to_end_timeout::{read_to_end_timeout, ReadToEndTimeout};
3use crate::io::{read_timeout, ReadTimeout};
4use std::time::Duration;
5use tokio::io::AsyncRead;
6
7pub trait AsyncReadTimeoutExt: AsyncRead {
8    fn read_timeout<'a>(&'a mut self, buf: &'a mut [u8], timeout: Duration) -> ReadTimeout<'a, Self>
9    where
10        Self: Unpin,
11    {
12        read_timeout(self, buf, timeout)
13    }
14
15    fn read_fill_timeout<'a>(
16        &'a mut self,
17        buf: &'a mut [u8],
18        timeout: Duration,
19    ) -> ReadExactTimeout<'a, Self>
20    where
21        Self: Unpin,
22    {
23        read_exact_timeout(self, buf, timeout, false)
24    }
25
26    fn read_exact_timeout<'a>(
27        &'a mut self,
28        buf: &'a mut [u8],
29        timeout: Duration,
30    ) -> ReadExactTimeout<'a, Self>
31    where
32        Self: Unpin,
33    {
34        read_exact_timeout(self, buf, timeout, true)
35    }
36
37    fn read_to_end_timeout<'a>(
38        &'a mut self,
39        buf: &'a mut Vec<u8>,
40        timeout: Duration,
41        throw_on_timeout: bool,
42    ) -> ReadToEndTimeout<'a, Self>
43    where
44        Self: Unpin,
45    {
46        read_to_end_timeout(self, buf, timeout, throw_on_timeout)
47    }
48}
49
50impl<R: AsyncRead + ?Sized> AsyncReadTimeoutExt for R {}