tungstenite/
util.rs

1//! Helper traits to ease non-blocking handling.
2
3use std::{
4    io::{Error as IoError, ErrorKind as IoErrorKind},
5    result::Result as StdResult,
6};
7
8use crate::error::Error;
9
10/// Non-blocking IO handling.
11pub trait NonBlockingError: Sized {
12    /// Convert WouldBlock to None and don't touch other errors.
13    fn into_non_blocking(self) -> Option<Self>;
14}
15
16impl NonBlockingError for IoError {
17    fn into_non_blocking(self) -> Option<Self> {
18        match self.kind() {
19            IoErrorKind::WouldBlock => None,
20            _ => Some(self),
21        }
22    }
23}
24
25impl NonBlockingError for Error {
26    fn into_non_blocking(self) -> Option<Self> {
27        match self {
28            Error::Io(e) => e.into_non_blocking().map(Into::into),
29            x => Some(x),
30        }
31    }
32}
33
34/// Non-blocking IO wrapper.
35///
36/// This trait is implemented for `Result<T, E: NonBlockingError>`.
37pub trait NonBlockingResult {
38    /// Type of the converted result: `Result<Option<T>, E>`
39    type Result;
40    /// Perform the non-block conversion.
41    fn no_block(self) -> Self::Result;
42}
43
44impl<T, E> NonBlockingResult for StdResult<T, E>
45where
46    E: NonBlockingError,
47{
48    type Result = StdResult<Option<T>, E>;
49    fn no_block(self) -> Self::Result {
50        match self {
51            Ok(x) => Ok(Some(x)),
52            Err(e) => match e.into_non_blocking() {
53                Some(e) => Err(e),
54                None => Ok(None),
55            },
56        }
57    }
58}