Move `no_block()` related stuff into seperate mod

pull/7/head
Daniel Abramov 8 years ago
parent 3bb4331f4f
commit 0938a17552
  1. 1
      src/lib.rs
  2. 48
      src/protocol/mod.rs
  3. 48
      src/util.rs

@ -27,3 +27,4 @@ pub mod handshake;
mod input_buffer; mod input_buffer;
mod stream; mod stream;
mod util;

@ -6,14 +6,14 @@ mod message;
pub use self::message::Message; pub use self::message::Message;
use std::collections::VecDeque; 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::mem::replace;
use std::result::Result as StdResult;
use error::{Error, Result}; use error::{Error, Result};
use self::message::{IncompleteMessage, IncompleteMessageType}; use self::message::{IncompleteMessage, IncompleteMessageType};
use self::frame::{Frame, FrameSocket}; use self::frame::{Frame, FrameSocket};
use self::frame::coding::{OpCode, Data as OpData, Control as OpCtl, CloseCode}; use self::frame::coding::{OpCode, Data as OpData, Control as OpCtl, CloseCode};
use util::NonBlockingResult;
/// Indicates a Client or Server role of the websocket /// Indicates a Client or Server role of the websocket
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -357,50 +357,6 @@ impl WebSocketState {
} }
} }
/// Non-blocking IO handling.
trait NonBlockingError: Sized {
fn into_non_blocking(self) -> Option<Self>;
}
impl NonBlockingError for IoError {
fn into_non_blocking(self) -> Option<Self> {
match self.kind() {
IoErrorKind::WouldBlock => None,
_ => Some(self),
}
}
}
impl NonBlockingError for Error {
fn into_non_blocking(self) -> Option<Self> {
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<T, E> NonBlockingResult for StdResult<T, E>
where E : NonBlockingError
{
type Result = StdResult<Option<T>, 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)] #[cfg(test)]
mod tests { mod tests {
use super::{WebSocket, Role, Message}; use super::{WebSocket, Role, Message};

@ -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<Self>;
}
impl NonBlockingError for IoError {
fn into_non_blocking(self) -> Option<Self> {
match self.kind() {
IoErrorKind::WouldBlock => None,
_ => Some(self),
}
}
}
impl NonBlockingError for Error {
fn into_non_blocking(self) -> Option<Self> {
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<T, E> NonBlockingResult for StdResult<T, E>
where E : NonBlockingError
{
type Result = StdResult<Option<T>, 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),
}
}
}
}
Loading…
Cancel
Save