tungstenite/
util.rs
1use std::{
4 io::{Error as IoError, ErrorKind as IoErrorKind},
5 result::Result as StdResult,
6};
7
8use crate::error::Error;
9
10pub trait NonBlockingError: Sized {
12 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
34pub trait NonBlockingResult {
38 type Result;
40 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}