Fix examples and update core-derive. (#1317)

This commit is contained in:
Toralf Wittner
2019-11-25 10:45:04 +01:00
committed by GitHub
parent df71d4a861
commit b7644722ee
12 changed files with 170 additions and 207 deletions

View File

@ -185,7 +185,7 @@ impl<TSubstream> PingHandler<TSubstream> {
impl<TSubstream> ProtocolsHandler for PingHandler<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite + Unpin + 'static,
TSubstream: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
type InEvent = Void;
type OutEvent = PingResult;

View File

@ -93,7 +93,7 @@ impl<TSubstream> Default for Ping<TSubstream> {
impl<TSubstream> NetworkBehaviour for Ping<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite + Unpin + 'static,
TSubstream: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
type ProtocolsHandler = PingHandler<TSubstream>;
type OutEvent = PingEvent;

View File

@ -18,11 +18,11 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use futures::prelude::*;
use futures::{future::BoxFuture, prelude::*};
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, Negotiated};
use log::debug;
use rand::{distributions, prelude::*};
use std::{io, iter, pin::Pin, time::Duration};
use std::{io, iter, time::Duration};
use wasm_timer::Instant;
/// Represents a prototype for an upgrade to handle the ping protocol.
@ -55,36 +55,35 @@ impl UpgradeInfo for Ping {
impl<TSocket> InboundUpgrade<TSocket> for Ping
where
TSocket: AsyncRead + AsyncWrite + Unpin + 'static,
TSocket: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
type Output = ();
type Error = io::Error;
type Future = Pin<Box<dyn Future<Output = Result<(), io::Error>>>>;
type Future = BoxFuture<'static, Result<(), io::Error>>;
fn upgrade_inbound(self, mut socket: Negotiated<TSocket>, _: Self::Info) -> Self::Future {
Box::pin(async move {
async move {
let mut payload = [0u8; 32];
socket.read_exact(&mut payload).await?;
socket.write_all(&payload).await?;
socket.close().await?;
Ok(())
})
}.boxed()
}
}
impl<TSocket> OutboundUpgrade<TSocket> for Ping
where
TSocket: AsyncRead + AsyncWrite + Unpin + 'static,
TSocket: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
type Output = Duration;
type Error = io::Error;
type Future = Pin<Box<dyn Future<Output = Result<Duration, io::Error>>>>;
type Future = BoxFuture<'static, Result<Duration, io::Error>>;
fn upgrade_outbound(self, mut socket: Negotiated<TSocket>, _: Self::Info) -> Self::Future {
let payload: [u8; 32] = thread_rng().sample(distributions::Standard);
debug!("Preparing ping payload {:?}", payload);
Box::pin(async move {
async move {
socket.write_all(&payload).await?;
socket.close().await?;
let started = Instant::now();
@ -96,7 +95,7 @@ where
} else {
Err(io::Error::new(io::ErrorKind::InvalidData, "Ping payload mismatch"))
}
})
}.boxed()
}
}