diff --git a/src/lib.rs b/src/lib.rs index d6185a1..3413dc2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,3 +27,4 @@ pub mod handshake; mod input_buffer; mod stream; +mod util; diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index cd90502..f6e7c2e 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -6,14 +6,14 @@ mod message; pub use self::message::Message; use std::collections::VecDeque; -use std::io::{Read, Write, Error as IoError, ErrorKind as IoErrorKind}; +use std::io::{Read, Write}; use std::mem::replace; -use std::result::Result as StdResult; use error::{Error, Result}; use self::message::{IncompleteMessage, IncompleteMessageType}; use self::frame::{Frame, FrameSocket}; use self::frame::coding::{OpCode, Data as OpData, Control as OpCtl, CloseCode}; +use util::NonBlockingResult; /// Indicates a Client or Server role of the websocket #[derive(Debug, Clone, Copy)] @@ -357,50 +357,6 @@ impl WebSocketState { } } -/// Non-blocking IO handling. -trait NonBlockingError: Sized { - 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. -trait NonBlockingResult { - type Result; - 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), - } - } - } -} - #[cfg(test)] mod tests { use super::{WebSocket, Role, Message}; diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..9a38223 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,48 @@ +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 { + 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. +pub trait NonBlockingResult { + type Result; + 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), + } + } + } +}