Cleanup and remove unnecessary trait objects

This commit is contained in:
Vurich
2017-10-23 11:45:35 +02:00
parent 0e4375fc90
commit 384d15e24a
3 changed files with 187 additions and 150 deletions

View File

@ -3,39 +3,45 @@
extern crate futures;
extern crate libp2p_transport as transport;
use futures::{Future, IntoFuture, BoxFuture};
use transport::{ProtocolId, MultiAddr, Socket};
use futures::{Future, IntoFuture};
use transport::{ProtocolId, Socket};
use transport::multiaddr::Multiaddr;
/// Produces a future for each incoming `Socket`.
pub trait Handler<S: Socket> {
type Future: IntoFuture<Item=(), Error=()>;
type Future: IntoFuture<Item = (), Error = ()>;
/// Handle the incoming socket, producing a future which should resolve
/// Handle the incoming socket, producing a future which should resolve
/// when the handler is finished.
fn handle(&self, socket: S) -> Self::Future;
fn boxed(self) -> BoxHandler<S> where
Self: Sized + Send + 'static,
<Self::Future as IntoFuture>::Future: Send + 'static
fn boxed(self) -> BoxHandler<S>
where
Self: Sized + Send + 'static,
<Self::Future as IntoFuture>::Future: Send + 'static,
{
BoxHandler(Box::new(move |socket|
self.handle(socket).into_future().boxed()
))
BoxHandler(Box::new(move |socket| {
Box::new(self.handle(socket).into_future()) as _
}))
}
}
impl<S: Socket, F, U> Handler<S> for F
where F: Fn(S) -> U, U: IntoFuture<Item=(), Error=()>
impl<S: Socket, F, U> Handler<S> for F
where
F: Fn(S) -> U,
U: IntoFuture<Item = (), Error = ()>,
{
type Future = U;
fn handle(&self, socket: S) -> U { (self)(socket) }
fn handle(&self, socket: S) -> U {
(self)(socket)
}
}
/// A boxed handler.
pub struct BoxHandler<S: Socket>(Box<Handler<S, Future=BoxFuture<(), ()>>>);
pub struct BoxHandler<S: Socket>(Box<Handler<S, Future = Box<Future<Item = (), Error = ()>>>>);
impl<S: Socket> Handler<S> for BoxHandler<S> {
type Future = BoxFuture<(), ()>;
type Future = Box<Future<Item = (), Error = ()>>;
fn handle(&self, socket: S) -> Self::Future {
self.0.handle(socket)
@ -49,7 +55,7 @@ pub trait Mux: Sync {
/// Attach an incoming socket.
fn push(&self, socket: Self::Socket);
/// Set the socket handler for a given protocol id.
fn set_handler(&self, proto: ProtocolId, handler: BoxHandler<Self::Socket>);
@ -59,19 +65,21 @@ pub trait Mux: Sync {
/// Unimplemented. Maps peer IDs to connected addresses, protocols, and data.
pub trait PeerStore {}
/// This is a common abstraction over the low-level bits of libp2p.
///
/// It handles connecting over, adding and removing transports,
/// wraps an arbitrary event loop, and manages protocol IDs.
pub trait Host {
type Socket: Socket;
type Mux: Mux<Socket = Self::Socket>;
type Multiaddrs: IntoIterator<Item = Multiaddr>;
/// Get a handle to the peer store.
fn peer_store(&self) -> &PeerStore;
/// Get a handle to the underlying muxer.
fn mux(&self) -> &Mux<Socket=Self::Socket>;
fn mux(&self) -> &Self::Mux;
/// Set the socket handler for a given protocol id.
fn set_handler(&self, proto: ProtocolId, handler: BoxHandler<Self::Socket>) {
@ -84,5 +92,5 @@ pub trait Host {
}
/// Addresses we're listening on.
fn listen_addrs(&self) -> Vec<MultiAddr>;
}
fn listen_addrs(&self) -> Self::Multiaddrs;
}