Skip to content

Add Send trait bounds to the Futures #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ pub type Client<S> = Framed<S, MessageCodec<OwnedMessage>>;
/// headers to see if the server accepted the protocol or other custom header.
/// This crate will not automatically close the connection if the server refused
/// to use the user protocols given to it, you must check that the server accepted.
pub type ClientNew<S> = Box<Future<Item = (Client<S>, Headers), Error = WebSocketError>>;
pub type ClientNew<S> = Box<Future<Item = (Client<S>, Headers), Error = WebSocketError> + Send>;
2 changes: 1 addition & 1 deletion src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub mod async {

/// The most common Future in this library, it is simply some result `I` or
/// a `WebSocketError`. This is analogous to the `WebSocketResult` type.
pub type WebSocketFuture<I> = Box<Future<Item = I, Error = WebSocketError>>;
pub type WebSocketFuture<I> = Box<Future<Item = I, Error = WebSocketError> + Send>;
}

/// Represents a WebSocket error
Expand Down
2 changes: 1 addition & 1 deletion src/server/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub type Server<S> = WsServer<S, TcpListener>;
/// struct which lets the user decide whether to turn the connection into a websocket
/// connection or reject it.
pub type Incoming<S> =
Box<Stream<Item = (Upgrade<S>, SocketAddr), Error = InvalidConnection<S, BytesMut>>>;
Box<Stream<Item = (Upgrade<S>, SocketAddr), Error = InvalidConnection<S, BytesMut>> + Send>;

/// Asynchronous methods for creating an async server and accepting incoming connections.
impl WsServer<NoTlsAcceptor, TcpListener> {
Expand Down
19 changes: 11 additions & 8 deletions src/server/upgrade/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bytes::BytesMut;
use client::async::ClientNew;
use codec::http::HttpServerCodec;
use codec::ws::{Context, MessageCodec};
use futures::sink::Send;
use futures::sink::Send as SinkSend;
use futures::Stream as StreamTrait;
use futures::{Future, Sink};
use hyper::header::Headers;
Expand Down Expand Up @@ -66,7 +66,7 @@ pub type Upgrade<S> = WsUpgrade<S, BytesMut>;
/// module under the name `Upgrade`.
impl<S> WsUpgrade<S, BytesMut>
where
S: Stream + 'static,
S: Stream + Send + 'static,
{
/// Asynchronously accept the websocket handshake, then create a client.
/// This will asynchronously send a response accepting the connection
Expand Down Expand Up @@ -116,19 +116,22 @@ where
/// Asynchronously send a rejection message and deconstruct `self`
/// into it's original stream. The stream being returned is framed with the
/// `HttpServerCodec` since that was used to send the rejection message.
pub fn reject(self) -> Send<Framed<S, HttpServerCodec>> {
pub fn reject(self) -> SinkSend<Framed<S, HttpServerCodec>> {
self.internal_reject(None)
}

/// Asynchronously send a rejection message with custom headers and
/// deconstruct `self` into it's original stream.
/// The stream being returned is framed with the
/// `HttpServerCodec` since that was used to send the rejection message.
pub fn reject_with(self, headers: &Headers) -> Send<Framed<S, HttpServerCodec>> {
pub fn reject_with(self, headers: &Headers) -> SinkSend<Framed<S, HttpServerCodec>> {
self.internal_reject(Some(headers))
}

fn internal_reject(mut self, headers: Option<&Headers>) -> Send<Framed<S, HttpServerCodec>> {
fn internal_reject(
mut self,
headers: Option<&Headers>,
) -> SinkSend<Framed<S, HttpServerCodec>> {
if let Some(custom) = headers {
self.headers.extend(custom.iter());
}
Expand Down Expand Up @@ -206,17 +209,17 @@ pub trait IntoWs {
///
/// Note: this is the asynchronous version, meaning it will not block when
/// trying to read a request.
fn into_ws(self) -> Box<Future<Item = Upgrade<Self::Stream>, Error = Self::Error>>;
fn into_ws(self) -> Box<Future<Item = Upgrade<Self::Stream>, Error = Self::Error> + Send>;
}

impl<S> IntoWs for S
where
S: Stream + 'static,
S: Stream + Send + 'static,
{
type Stream = S;
type Error = (S, Option<Request>, BytesMut, HyperIntoWsError);

fn into_ws(self) -> Box<Future<Item = Upgrade<Self::Stream>, Error = Self::Error>> {
fn into_ws(self) -> Box<Future<Item = Upgrade<Self::Stream>, Error = Self::Error> + Send> {
let future = self
.framed(HttpServerCodec)
.into_future()
Expand Down