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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use std::fmt;

/// Binary representation of a file descriptor readiness (obtained through epoll)
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord)]
pub struct Ready(pub u16);

impl Ready {
    pub const EMPTY: Ready = Ready(0);
    pub const READABLE: Ready = Ready(0b00001);
    pub const WRITABLE: Ready = Ready(0b00010);
    pub const ERROR: Ready = Ready(0b00100);
    /// Hang UP (see EPOLLHUP in epoll_ctl man page)
    pub const HUP: Ready = Ready(0b01000);
    pub const ALL: Ready = Ready(0b00011);

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.0 == 0
    }

    #[inline]
    pub fn is_readable(&self) -> bool {
        self.contains(Ready::READABLE)
    }

    #[inline]
    pub fn is_writable(&self) -> bool {
        self.contains(Ready::WRITABLE)
    }

    pub fn is_error(&self) -> bool {
        self.contains(Ready::ERROR)
    }

    pub fn is_hup(&self) -> bool {
        self.contains(Ready::HUP)
    }

    #[inline]
    pub fn insert<T: Into<Self>>(&mut self, other: T) {
        let other = other.into();
        self.0 |= other.0;
    }

    #[inline]
    pub fn remove<T: Into<Self>>(&mut self, other: T) {
        let other = other.into();
        self.0 &= !other.0;
    }

    #[inline]
    pub fn contains<T: Into<Self>>(&self, other: T) -> bool {
        let other = other.into();
        (*self & other) == other
    }
}

//pub(crate) const RWINTEREST: mio::Interest = mio::Interest::READABLE | mio::Interest::WRITABLE;

use std::ops;
impl<T: Into<Ready>> ops::BitOr<T> for Ready {
    type Output = Ready;

    #[inline]
    fn bitor(self, other: T) -> Ready {
        Ready(self.0 | other.into().0)
    }
}

impl<T: Into<Ready>> ops::BitOrAssign<T> for Ready {
    #[inline]
    fn bitor_assign(&mut self, other: T) {
        self.0 |= other.into().0;
    }
}

impl<T: Into<Ready>> ops::BitAnd<T> for Ready {
    type Output = Ready;

    #[inline]
    fn bitand(self, other: T) -> Ready {
        Ready(self.0 & other.into().0)
    }
}

impl fmt::Debug for Ready {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        let mut one = false;
        let flags = [
            (Ready::READABLE, "Readable"),
            (Ready::WRITABLE, "Writable"),
            (Ready::ERROR, "Error"),
            (Ready::HUP, "Hup"),
        ];

        for &(flag, msg) in &flags {
            if self.contains(flag) {
                if one {
                    write!(fmt, " | ")?
                }
                write!(fmt, "{msg}")?;

                one = true
            }
        }

        if !one {
            fmt.write_str("(empty)")?;
        }

        Ok(())
    }
}

impl std::convert::From<mio::Interest> for Ready {
    fn from(i: mio::Interest) -> Self {
        let mut r = Ready::EMPTY;
        if i.is_readable() {
            r.insert(Ready::READABLE);
        }
        if i.is_writable() {
            r.insert(Ready::WRITABLE);
        }

        r
    }
}

impl std::convert::From<&mio::event::Event> for Ready {
    fn from(e: &mio::event::Event) -> Self {
        let mut r = Ready::EMPTY;
        if e.is_readable() {
            r.insert(Ready::READABLE);
        }
        if e.is_writable() {
            r.insert(Ready::WRITABLE);
        }
        if e.is_error() {
            r.insert(Ready::ERROR);
        }
        //FIXME: handle HUP events
        if e.is_read_closed() || e.is_write_closed() {
            r.insert(Ready::HUP);
        }

        r
    }
}