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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use std::time::Duration;

/// PollTimeout argument for polling.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub struct PollTimeout(i32);

impl PollTimeout {
    /// Blocks indefinitely.
    ///
    /// > Specifying a negative value in timeout means an infinite timeout.
    pub const NONE: Self = Self(-1);
    /// Returns immediately.
    ///
    /// > Specifying a timeout of zero causes poll() to return immediately, even if no file
    /// > descriptors are ready.
    pub const ZERO: Self = Self(0);
    /// Blocks for at most [`i32::MAX`] milliseconds.
    pub const MAX: Self = Self(i32::MAX);
    /// Returns if `self` equals [`PollTimeout::NONE`].
    pub fn is_none(&self) -> bool {
        // > Specifying a negative value in timeout means an infinite timeout.
        *self <= Self::NONE
    }
    /// Returns if `self` does not equal [`PollTimeout::NONE`].
    pub fn is_some(&self) -> bool {
        !self.is_none()
    }
    /// Returns the timeout in milliseconds if there is some, otherwise returns `None`.
    pub fn as_millis(&self) -> Option<u32> {
        self.is_some().then_some(u32::try_from(self.0).unwrap())
    }
    /// Returns the timeout as a `Duration` if there is some, otherwise returns `None`.
    pub fn duration(&self) -> Option<Duration> {
        self.as_millis()
            .map(|x| Duration::from_millis(u64::from(x)))
    }
}

/// Error type for integer conversions into `PollTimeout`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PollTimeoutTryFromError {
    /// Passing a value less than -1 is invalid on some systems, see
    /// <https://man.freebsd.org/cgi/man.cgi?poll#end>.
    TooNegative,
    /// Passing a value greater than `i32::MAX` is invalid.
    TooPositive,
}

impl std::fmt::Display for PollTimeoutTryFromError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::TooNegative => write!(f, "Passed a negative timeout less than -1."),
            Self::TooPositive => write!(f, "Passed a positive timeout greater than `i32::MAX` milliseconds.")
        }
    }
}

impl std::error::Error for PollTimeoutTryFromError {}

impl<T: Into<PollTimeout>> From<Option<T>> for PollTimeout {
    fn from(x: Option<T>) -> Self {
        x.map_or(Self::NONE, |x| x.into())
    }
}
impl TryFrom<Duration> for PollTimeout {
    type Error = PollTimeoutTryFromError;
    fn try_from(x: Duration) -> std::result::Result<Self, Self::Error> {
        Ok(Self(
            i32::try_from(x.as_millis())
                .map_err(|_| PollTimeoutTryFromError::TooPositive)?,
        ))
    }
}
impl TryFrom<u128> for PollTimeout {
    type Error = PollTimeoutTryFromError;
    fn try_from(x: u128) -> std::result::Result<Self, Self::Error> {
        Ok(Self(
            i32::try_from(x)
                .map_err(|_| PollTimeoutTryFromError::TooPositive)?,
        ))
    }
}
impl TryFrom<u64> for PollTimeout {
    type Error = PollTimeoutTryFromError;
    fn try_from(x: u64) -> std::result::Result<Self, Self::Error> {
        Ok(Self(
            i32::try_from(x)
                .map_err(|_| PollTimeoutTryFromError::TooPositive)?,
        ))
    }
}
impl TryFrom<u32> for PollTimeout {
    type Error = PollTimeoutTryFromError;
    fn try_from(x: u32) -> std::result::Result<Self, Self::Error> {
        Ok(Self(
            i32::try_from(x)
                .map_err(|_| PollTimeoutTryFromError::TooPositive)?,
        ))
    }
}
impl From<u16> for PollTimeout {
    fn from(x: u16) -> Self {
        Self(i32::from(x))
    }
}
impl From<u8> for PollTimeout {
    fn from(x: u8) -> Self {
        Self(i32::from(x))
    }
}
impl TryFrom<i128> for PollTimeout {
    type Error = PollTimeoutTryFromError;
    fn try_from(x: i128) -> std::result::Result<Self, Self::Error> {
        match x {
            ..=-2 => Err(PollTimeoutTryFromError::TooNegative),
            -1.. => Ok(Self(
                i32::try_from(x)
                    .map_err(|_| PollTimeoutTryFromError::TooPositive)?,
            )),
        }
    }
}
impl TryFrom<i64> for PollTimeout {
    type Error = PollTimeoutTryFromError;
    fn try_from(x: i64) -> std::result::Result<Self, Self::Error> {
        match x {
            ..=-2 => Err(PollTimeoutTryFromError::TooNegative),
            -1.. => Ok(Self(
                i32::try_from(x)
                    .map_err(|_| PollTimeoutTryFromError::TooPositive)?,
            )),
        }
    }
}
impl TryFrom<i32> for PollTimeout {
    type Error = PollTimeoutTryFromError;
    fn try_from(x: i32) -> std::result::Result<Self, Self::Error> {
        match x {
            ..=-2 => Err(PollTimeoutTryFromError::TooNegative),
            -1.. => Ok(Self(x)),
        }
    }
}
impl TryFrom<i16> for PollTimeout {
    type Error = PollTimeoutTryFromError;
    fn try_from(x: i16) -> std::result::Result<Self, Self::Error> {
        match x {
            ..=-2 => Err(PollTimeoutTryFromError::TooNegative),
            -1.. => Ok(Self(i32::from(x))),
        }
    }
}
impl TryFrom<i8> for PollTimeout {
    type Error = PollTimeoutTryFromError;
    fn try_from(x: i8) -> std::result::Result<Self, Self::Error> {
        match x {
            ..=-2 => Err(PollTimeoutTryFromError::TooNegative),
            -1.. => Ok(Self(i32::from(x))),
        }
    }
}
impl TryFrom<PollTimeout> for Duration {
    type Error = ();
    fn try_from(x: PollTimeout) -> std::result::Result<Self, ()> {
        x.duration().ok_or(())
    }
}
impl TryFrom<PollTimeout> for u128 {
    type Error = <Self as TryFrom<i32>>::Error;
    fn try_from(x: PollTimeout) -> std::result::Result<Self, Self::Error> {
        Self::try_from(x.0)
    }
}
impl TryFrom<PollTimeout> for u64 {
    type Error = <Self as TryFrom<i32>>::Error;
    fn try_from(x: PollTimeout) -> std::result::Result<Self, Self::Error> {
        Self::try_from(x.0)
    }
}
impl TryFrom<PollTimeout> for u32 {
    type Error = <Self as TryFrom<i32>>::Error;
    fn try_from(x: PollTimeout) -> std::result::Result<Self, Self::Error> {
        Self::try_from(x.0)
    }
}
impl TryFrom<PollTimeout> for u16 {
    type Error = <Self as TryFrom<i32>>::Error;
    fn try_from(x: PollTimeout) -> std::result::Result<Self, Self::Error> {
        Self::try_from(x.0)
    }
}
impl TryFrom<PollTimeout> for u8 {
    type Error = <Self as TryFrom<i32>>::Error;
    fn try_from(x: PollTimeout) -> std::result::Result<Self, Self::Error> {
        Self::try_from(x.0)
    }
}
impl From<PollTimeout> for i128 {
    fn from(x: PollTimeout) -> Self {
        Self::from(x.0)
    }
}
impl From<PollTimeout> for i64 {
    fn from(x: PollTimeout) -> Self {
        Self::from(x.0)
    }
}
impl From<PollTimeout> for i32 {
    fn from(x: PollTimeout) -> Self {
        x.0
    }
}
impl TryFrom<PollTimeout> for i16 {
    type Error = <Self as TryFrom<i32>>::Error;
    fn try_from(x: PollTimeout) -> std::result::Result<Self, Self::Error> {
        Self::try_from(x.0)
    }
}
impl TryFrom<PollTimeout> for i8 {
    type Error = <Self as TryFrom<i32>>::Error;
    fn try_from(x: PollTimeout) -> std::result::Result<Self, Self::Error> {
        Self::try_from(x.0)
    }
}