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
90
91
92
93
94
95
96
97
98
99
100
101
102
use {EventSet, Selector, PollOpt, Token};
use bytes::{Buf, MutBuf};
pub use std::io::{Read, Write, Result, Error, ErrorKind};
pub trait Evented {
#[doc(hidden)]
fn register(&self, selector: &mut Selector, token: Token, interest: EventSet, opts: PollOpt) -> Result<()>;
#[doc(hidden)]
fn reregister(&self, selector: &mut Selector, token: Token, interest: EventSet, opts: PollOpt) -> Result<()>;
#[doc(hidden)]
fn deregister(&self, selector: &mut Selector) -> Result<()>;
}
pub trait TryRead {
fn try_read_buf<B: MutBuf>(&mut self, buf: &mut B) -> Result<Option<usize>>
where Self : Sized
{
let res = self.try_read(unsafe { buf.mut_bytes() });
if let Ok(Some(cnt)) = res {
unsafe { buf.advance(cnt); }
}
res
}
fn try_read(&mut self, buf: &mut [u8]) -> Result<Option<usize>>;
}
pub trait TryWrite {
fn try_write_buf<B: Buf>(&mut self, buf: &mut B) -> Result<Option<usize>>
where Self : Sized
{
let res = self.try_write(buf.bytes());
if let Ok(Some(cnt)) = res {
buf.advance(cnt);
}
res
}
fn try_write(&mut self, buf: &[u8]) -> Result<Option<usize>>;
}
impl<T: Read> TryRead for T {
fn try_read(&mut self, dst: &mut [u8]) -> Result<Option<usize>> {
self.read(dst).map_non_block()
}
}
impl<T: Write> TryWrite for T {
fn try_write(&mut self, src: &[u8]) -> Result<Option<usize>> {
self.write(src).map_non_block()
}
}
pub trait TryAccept {
type Output;
fn accept(&self) -> Result<Option<Self::Output>>;
}
pub trait MapNonBlock<T> {
fn map_non_block(self) -> Result<Option<T>>;
}
impl<T> MapNonBlock<T> for Result<T> {
fn map_non_block(self) -> Result<Option<T>> {
use std::io::ErrorKind::WouldBlock;
match self {
Ok(value) => Ok(Some(value)),
Err(err) => {
if let WouldBlock = err.kind() {
Ok(None)
} else {
Err(err)
}
}
}
}
}