Rustfmt and use tabs

This commit is contained in:
Pierre Krieger 2018-01-11 11:11:49 +01:00
parent b8829d7cb1
commit 6837a3928d
No known key found for this signature in database
GPG Key ID: A03CE3AD81F08F7C
2 changed files with 246 additions and 225 deletions

View File

@ -18,14 +18,14 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
use std::io::{Read, Write}; use futures::{Async, Future, Poll, Stream, Then as FutureThen};
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use std::sync::{Arc, Mutex};
use futures::{Future, Stream, Poll, Async, Then as FutureThen};
use futures::sync::{oneshot, mpsc};
use futures::stream::Then as StreamThen; use futures::stream::Then as StreamThen;
use multiaddr::{Multiaddr, AddrComponent}; use futures::sync::{mpsc, oneshot};
use multiaddr::{AddrComponent, Multiaddr};
use rw_stream_sink::RwStreamSink; use rw_stream_sink::RwStreamSink;
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use std::io::{Read, Write};
use std::sync::{Arc, Mutex};
use stdweb::{self, Reference}; use stdweb::{self, Reference};
use stdweb::web::TypedArray; use stdweb::web::TypedArray;
use swarm::Transport; use swarm::Transport;
@ -52,7 +52,12 @@ impl Transport for BrowserWsConfig {
type RawConn = BrowserWsConn; type RawConn = BrowserWsConn;
type Listener = Box<Stream<Item = (Self::ListenerUpgrade, Multiaddr), Error = IoError>>; // TODO: use `!` type Listener = Box<Stream<Item = (Self::ListenerUpgrade, Multiaddr), Error = IoError>>; // TODO: use `!`
type ListenerUpgrade = Box<Future<Item = Self::RawConn, Error = IoError>>; // TODO: use `!` type ListenerUpgrade = Box<Future<Item = Self::RawConn, Error = IoError>>; // TODO: use `!`
type Dial = FutureThen<oneshot::Receiver<Result<BrowserWsConn, IoError>>, Result<BrowserWsConn, IoError>, fn(Result<Result<BrowserWsConn, IoError>, oneshot::Canceled>) -> Result<BrowserWsConn, IoError>>; type Dial = FutureThen<
oneshot::Receiver<Result<BrowserWsConn, IoError>>,
Result<BrowserWsConn, IoError>,
fn(Result<Result<BrowserWsConn, IoError>, oneshot::Canceled>)
-> Result<BrowserWsConn, IoError>,
>;
#[inline] #[inline]
fn listen_on(self, a: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> { fn listen_on(self, a: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> {
@ -101,8 +106,10 @@ impl Transport for BrowserWsConfig {
if let Some(buffer) = message_data.downcast::<TypedArray<u8>>() { if let Some(buffer) = message_data.downcast::<TypedArray<u8>>() {
let _ = message_tx.unbounded_send(Ok(buffer.to_vec())); let _ = message_tx.unbounded_send(Ok(buffer.to_vec()));
} else { } else {
let _ = message_tx.unbounded_send(Err(IoError::new(IoErrorKind::InvalidData, let _ = message_tx.unbounded_send(Err(IoError::new(
"received ws message of unknown type"))); IoErrorKind::InvalidData,
"received ws message of unknown type",
)));
} }
} }
}; };
@ -119,15 +126,18 @@ impl Transport for BrowserWsConfig {
// Note that `open_tx` can be empty (and a panic happens) if the `open` event // Note that `open_tx` can be empty (and a panic happens) if the `open` event
// is triggered twice, or is triggered after the `close` event. We never reuse the // is triggered twice, or is triggered after the `close` event. We never reuse the
// same websocket twice, so this is not supposed to happen. // same websocket twice, so this is not supposed to happen.
let tx = open_tx.lock().unwrap().take().expect("the websocket can only open once"); let tx = open_tx
.lock()
.unwrap()
.take()
.expect("the websocket can only open once");
// `message_rx` can be empty if the `open` event is triggered twice, which again // `message_rx` can be empty if the `open` event is triggered twice, which again
// is not supposed to happen. // is not supposed to happen.
let message_rx = message_rx.take().expect("the websocket can only open once"); let message_rx = message_rx.take().expect("the websocket can only open once");
// Send a `BrowserWsConn` to the future that was returned by `dial`. Ignoring errors that // Send a `BrowserWsConn` to the future that was returned by `dial`. Ignoring errors that
// would happen the future has been dropped by the user. // would happen the future has been dropped by the user.
let _ = tx let _ = tx.send(Ok(BrowserWsConn {
.send(Ok(BrowserWsConn {
websocket: websocket_clone.clone(), websocket: websocket_clone.clone(),
incoming_data: RwStreamSink::new(message_rx.then(|result| { incoming_data: RwStreamSink::new(message_rx.then(|result| {
// An `Err` happens here if `message_tx` has been dropped. However // An `Err` happens here if `message_tx` has been dropped. However
@ -135,7 +145,9 @@ impl Transport for BrowserWsConfig {
// long as the `BrowserWsConn` is alive. // long as the `BrowserWsConn` is alive.
match result { match result {
Ok(r) => r, Ok(r) => r,
Err(_) => unreachable!("the message channel outlives the BrowserWsConn") Err(_) => {
unreachable!("the message channel outlives the BrowserWsConn")
}
} }
})), })),
})); }));
@ -147,12 +159,16 @@ impl Transport for BrowserWsConfig {
// to both the `open` and `message` channels if that happens. // to both the `open` and `message` channels if that happens.
let close_cb = move || { let close_cb = move || {
if let Some(tx) = open_tx.lock().unwrap().take() { if let Some(tx) = open_tx.lock().unwrap().take() {
let _ = tx.send(Err(IoError::new(IoErrorKind::ConnectionRefused, let _ = tx.send(Err(IoError::new(
"close event on the websocket"))); IoErrorKind::ConnectionRefused,
"close event on the websocket",
)));
} }
let _ = message_tx.unbounded_send(Err(IoError::new(IoErrorKind::ConnectionRefused, let _ = message_tx.unbounded_send(Err(IoError::new(
"close event on the websocket"))); IoErrorKind::ConnectionRefused,
"close event on the websocket",
)));
}; };
js! { js! {
@ -185,7 +201,7 @@ impl Transport for BrowserWsConfig {
// captured by the `WebSocket`. Due to this cyclic dependency, `open_tx` should // captured by the `WebSocket`. Due to this cyclic dependency, `open_tx` should
// never be destroyed. // never be destroyed.
// TODO: how do we break this cyclic dependency? difficult question // TODO: how do we break this cyclic dependency? difficult question
Err(_) => unreachable!("the sending side will only close when we drop the future") Err(_) => unreachable!("the sending side will only close when we drop the future"),
} }
})) }))
} }
@ -194,10 +210,13 @@ impl Transport for BrowserWsConfig {
pub struct BrowserWsConn { pub struct BrowserWsConn {
websocket: Reference, websocket: Reference,
// Stream of messages that goes through a `RwStreamSink` in order to become a `AsyncRead`. // Stream of messages that goes through a `RwStreamSink` in order to become a `AsyncRead`.
incoming_data: RwStreamSink<StreamThen<mpsc::UnboundedReceiver<Result<Vec<u8>, IoError>>, incoming_data: RwStreamSink<
StreamThen<
mpsc::UnboundedReceiver<Result<Vec<u8>, IoError>>,
fn(Result<Result<Vec<u8>, IoError>, ()>) -> Result<Vec<u8>, IoError>, fn(Result<Result<Vec<u8>, IoError>, ()>) -> Result<Vec<u8>, IoError>,
Result<Vec<u8>, IoError> Result<Vec<u8>, IoError>,
>>, >,
>,
} }
impl Drop for BrowserWsConn { impl Drop for BrowserWsConn {
@ -208,8 +227,7 @@ impl Drop for BrowserWsConn {
} }
} }
impl AsyncRead for BrowserWsConn { impl AsyncRead for BrowserWsConn {}
}
impl Read for BrowserWsConn { impl Read for BrowserWsConn {
#[inline] #[inline]
@ -242,8 +260,11 @@ impl Write for BrowserWsConn {
match returned { match returned {
stdweb::Value::Bool(true) => Ok(buf.len()), stdweb::Value::Bool(true) => Ok(buf.len()),
stdweb::Value::Bool(false) => Err(IoError::new(IoErrorKind::BrokenPipe, "websocket has been closed by the remote")), stdweb::Value::Bool(false) => Err(IoError::new(
_ => unreachable!() IoErrorKind::BrokenPipe,
"websocket has been closed by the remote",
)),
_ => unreachable!(),
} }
} }