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
78
79
80
81
82
83
84
85
86
87
88
89
use std::io;
use std::fmt;
pub use self::async_read::AsyncRead;
pub use self::async_write::AsyncWrite;
pub use self::read_pattern::{ReadFrom, PatternReader};
pub use self::write_pattern::{WriteInto, PatternWriter};
pub use self::external_size::ExternalSize;
use error::AsyncError;
use pattern::combinators::UnexpectedValue;
pub mod futures {
pub use super::async_read::{ReadBytes, ReadNonEmpty, ReadExact};
pub use super::read_pattern::{ReadEos, ReadUntil, ReadBuf, ReadPartialBuf};
pub use super::read_pattern::{ReadString, ReadFixnum, ReadPattern};
pub use super::read_pattern::{ReadLengthPrefixedBytes, ReadUtf8, ReadAll};
pub use super::async_write::{Flush, WriteBytes, WriteAll};
pub use super::write_pattern::{WritePattern, WriteBuf, WritePartialBuf};
pub use super::write_pattern::{WriteFixnum, WriteFlush};
}
pub mod streams {
pub use super::read_pattern::ReadStream;
}
pub mod misc;
mod async_read;
mod async_write;
mod read_pattern;
mod write_pattern;
mod external_size;
pub type AsyncIoError<T> = AsyncError<T, io::Error>;
impl<T> From<UnexpectedValue<T>> for io::Error
where
T: fmt::Debug,
{
fn from(f: UnexpectedValue<T>) -> Self {
io::Error::new(
io::ErrorKind::InvalidData,
format!("Unexpected value: {:?}", f.0),
)
}
}
#[derive(Debug, Clone)]
pub struct Stateful<S, T> {
pub stream: S,
pub state: T,
}
impl<S, T> Stateful<S, T> {
pub fn map_state<F, U>(self, f: F) -> Stateful<S, U>
where
F: FnOnce(T) -> U,
{
let u = f(self.state);
Stateful {
stream: self.stream,
state: u,
}
}
}
impl<S: io::Read, T> io::Read for Stateful<S, T> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.stream.read(buf)
}
}
impl<S: io::Write, T> io::Write for Stateful<S, T> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.stream.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.stream.flush()
}
}