//! Helper traits to ease non-blocking handling. use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::result::Result as StdResult; use error::Error; /// Non-blocking IO handling. pub trait NonBlockingError: Sized { /// Convert WouldBlock to None and don't touch other errors. fn into_non_blocking(self) -> Option; } impl NonBlockingError for IoError { fn into_non_blocking(self) -> Option { match self.kind() { IoErrorKind::WouldBlock => None, _ => Some(self), } } } impl NonBlockingError for Error { fn into_non_blocking(self) -> Option { match self { Error::Io(e) => e.into_non_blocking().map(|e| e.into()), x => Some(x), } } } /// Non-blocking IO wrapper. /// /// This trait is implemented for `Result`. pub trait NonBlockingResult { /// Type of the converted result: `Result, E>` type Result; /// Perform the non-block conversion. fn no_block(self) -> Self::Result; } impl NonBlockingResult for StdResult where E : NonBlockingError { type Result = StdResult, E>; fn no_block(self) -> Self::Result { match self { Ok(x) => Ok(Some(x)), Err(e) => match e.into_non_blocking() { Some(e) => Err(e), None => Ok(None), } } } }