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
use crate::io::util::timeout::read_until_timeout::{read_until_timeout, ReadUntilTimeout};
use crate::io::{read_until_regex_timeout, AsyncCacheRead, ReadUntilRegexTimeout};
use std::time::Duration;

pub trait AsyncReadCacheTimeoutExt: AsyncCacheRead {
    fn read_until_timeout<'a, T: AsRef<[u8]>>(
        &'a mut self,
        delim: T,
        buf: &'a mut Vec<u8>,
        timeout: Duration,
    ) -> ReadUntilTimeout<'a, Self, T>
    where
        Self: Unpin,
    {
        read_until_timeout(self, delim, buf, timeout)
    }

    fn read_until_regex_timeout<'a>(
        &'a mut self,
        pattern: &str,
        buf: &'a mut Vec<u8>,
        timeout: Duration,
    ) -> Result<ReadUntilRegexTimeout<'a, Self>, regex::Error>
    where
        Self: Unpin,
    {
        read_until_regex_timeout(self, pattern, buf, timeout)
    }

    fn read_line_timeout<'a>(
        &'a mut self,
        buf: &'a mut Vec<u8>,
        timeout: Duration,
    ) -> ReadUntilTimeout<'a, Self, &'static [u8]>
    where
        Self: Unpin,
    {
        read_until_timeout(self, b"\n", buf, timeout)
    }

    fn read_line_crlf_timeout<'a>(
        &'a mut self,
        buf: &'a mut Vec<u8>,
        timeout: Duration,
    ) -> ReadUntilTimeout<'a, Self, &'static [u8]>
    where
        Self: Unpin,
    {
        read_until_timeout(self, b"\r\n", buf, timeout)
    }
}

impl<R: AsyncCacheRead + ?Sized> AsyncReadCacheTimeoutExt for R {}