Skip to content

Commit 90721b9

Browse files
committed
transports/tcp: impl Stream for GenTcpTransport
1 parent bfd5fb0 commit 90721b9

File tree

1 file changed

+50
-17
lines changed

1 file changed

+50
-17
lines changed

transports/tcp/src/lib.rs

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub type TokioTcpTransport = GenTcpTransport<tokio::Tcp>;
4545
use futures::{
4646
future::{self, BoxFuture, Ready},
4747
prelude::*,
48-
ready,
48+
ready, stream::FusedStream,
4949
};
5050
use futures_timer::Delay;
5151
use libp2p_core::{
@@ -293,9 +293,9 @@ impl GenTcpConfig {
293293
/// following example:
294294
///
295295
/// ```no_run
296+
/// # use futures::StreamExt;
296297
/// # use libp2p_core::transport::{ListenerId, TransportEvent};
297298
/// # use libp2p_core::{Multiaddr, Transport};
298-
/// # use futures::{stream::StreamExt, future::poll_fn};
299299
/// # use std::pin::Pin;
300300
/// #[cfg(feature = "async-io")]
301301
/// #[async_std::main]
@@ -307,7 +307,7 @@ impl GenTcpConfig {
307307
///
308308
/// let mut tcp1 = TcpTransport::new(GenTcpConfig::new().port_reuse(true));
309309
/// tcp1.listen_on(ListenerId::new(1), listen_addr1.clone()).expect("listener");
310-
/// match poll_fn(|cx| Pin::new(&mut tcp1).poll(cx)).await {
310+
/// match tcp1.select_next_some().await {
311311
/// TransportEvent::NewAddress { listen_addr, .. } => {
312312
/// println!("Listening on {:?}", listen_addr);
313313
/// let mut stream = tcp1.dial(listen_addr2.clone()).unwrap().await?;
@@ -318,7 +318,7 @@ impl GenTcpConfig {
318318
///
319319
/// let mut tcp2 = TcpTransport::new(GenTcpConfig::new().port_reuse(true));
320320
/// tcp2.listen_on(ListenerId::new(1), listen_addr2).expect("listener");
321-
/// match poll_fn(|cx| Pin::new(&mut tcp2).poll(cx)).await {
321+
/// match tcp2.select_next_some().await {
322322
/// TransportEvent::NewAddress { listen_addr, .. } => {
323323
/// println!("Listening on {:?}", listen_addr);
324324
/// let mut socket = tcp2.dial(listen_addr1).unwrap().await?;
@@ -550,6 +550,32 @@ where
550550
}
551551
}
552552

553+
impl<T> Stream for GenTcpTransport<T>
554+
where
555+
T: Provider + Send + 'static,
556+
T::Listener: Unpin,
557+
T::IfWatcher: Unpin,
558+
T::Stream: Unpin,
559+
{
560+
type Item = TransportEvent<<Self as Transport>::ListenerUpgrade, <Self as Transport>::Error>;
561+
562+
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
563+
Transport::poll(self, cx).map(Some)
564+
}
565+
}
566+
impl<T> FusedStream for GenTcpTransport<T>
567+
where
568+
T: Provider + Send + 'static,
569+
T::Listener: Unpin,
570+
T::IfWatcher: Unpin,
571+
T::Stream: Unpin,
572+
{
573+
fn is_terminated(&self) -> bool {
574+
false
575+
}
576+
}
577+
578+
553579
#[derive(Debug)]
554580
pub enum TcpTransportEvent<S> {
555581
/// The transport is listening on a new additional [`Multiaddr`].
@@ -855,7 +881,7 @@ fn ip_to_multiaddr(ip: IpAddr, port: u16) -> Multiaddr {
855881
#[cfg(test)]
856882
mod tests {
857883
use super::*;
858-
use futures::{channel::mpsc, future::poll_fn};
884+
use futures::channel::mpsc;
859885

860886
#[test]
861887
fn multiaddr_to_tcp_conversion() {
@@ -914,7 +940,7 @@ mod tests {
914940
let mut tcp = GenTcpTransport::<T>::new(GenTcpConfig::new());
915941
tcp.listen_on(ListenerId::new(1), addr).unwrap();
916942
loop {
917-
match poll_fn(|cx| Pin::new(&mut tcp).poll(cx)).await {
943+
match tcp.select_next_some().await {
918944
TransportEvent::NewAddress { listen_addr, .. } => {
919945
ready_tx.send(listen_addr).await.unwrap();
920946
}
@@ -984,7 +1010,7 @@ mod tests {
9841010
tcp.listen_on(ListenerId::new(1), addr).unwrap();
9851011

9861012
loop {
987-
match poll_fn(|cx| Pin::new(&mut tcp).poll(cx)).await {
1013+
match tcp.select_next_some().await {
9881014
TransportEvent::NewAddress { listen_addr, .. } => {
9891015
let mut iter = listen_addr.iter();
9901016
match iter.next().expect("ip address") {
@@ -1052,7 +1078,7 @@ mod tests {
10521078
let mut tcp = GenTcpTransport::<T>::new(GenTcpConfig::new());
10531079
tcp.listen_on(ListenerId::new(1), addr).unwrap();
10541080
loop {
1055-
match poll_fn(|cx| Pin::new(&mut tcp).poll(cx)).await {
1081+
match tcp.select_next_some().await {
10561082
TransportEvent::NewAddress { listen_addr, .. } => {
10571083
ready_tx.send(listen_addr).await.ok();
10581084
}
@@ -1073,7 +1099,7 @@ mod tests {
10731099
let dest_addr = ready_rx.next().await.unwrap();
10741100
let mut tcp = GenTcpTransport::<T>::new(GenTcpConfig::new().port_reuse(true));
10751101
tcp.listen_on(ListenerId::new(1), addr).unwrap();
1076-
match poll_fn(|cx| Pin::new(&mut tcp).poll(cx)).await {
1102+
match tcp.select_next_some().await {
10771103
TransportEvent::NewAddress { .. } => {
10781104
// Obtain a future socket through dialing
10791105
let mut socket = tcp.dial(dest_addr).unwrap().await.unwrap();
@@ -1122,16 +1148,22 @@ mod tests {
11221148
fn port_reuse_listening() {
11231149
env_logger::try_init().ok();
11241150

1125-
async fn listen_twice<T: Provider>(addr: Multiaddr) {
1151+
async fn listen_twice<T>(addr: Multiaddr)
1152+
where
1153+
T: Provider + Sized + Send + Sync + Unpin + 'static,
1154+
T::Listener: Sync,
1155+
T::IfWatcher: Sync,
1156+
T::Stream: Sync,
1157+
{
11261158
let mut tcp = GenTcpTransport::<T>::new(GenTcpConfig::new().port_reuse(true));
11271159
tcp.listen_on(ListenerId::new(1), addr).unwrap();
1128-
match poll_fn(|cx| Pin::new(&mut tcp).poll(cx)).await {
1160+
match tcp.select_next_some().await {
11291161
TransportEvent::NewAddress {
11301162
listen_addr: addr1, ..
11311163
} => {
11321164
// Listen on the same address a second time.
11331165
tcp.listen_on(ListenerId::new(1), addr1.clone()).unwrap();
1134-
match poll_fn(|cx| Pin::new(&mut tcp).poll(cx)).await {
1166+
match tcp.select_next_some().await {
11351167
TransportEvent::NewAddress {
11361168
listen_addr: addr2, ..
11371169
} => {
@@ -1170,13 +1202,14 @@ mod tests {
11701202
fn listen_port_0() {
11711203
env_logger::try_init().ok();
11721204

1173-
async fn listen<T: Provider>(addr: Multiaddr) -> Multiaddr {
1205+
async fn listen<T>(addr: Multiaddr) -> Multiaddr
1206+
where
1207+
T: Provider,
1208+
T::IfWatcher: Sync
1209+
{
11741210
let mut tcp = GenTcpTransport::<T>::new(GenTcpConfig::new());
11751211
tcp.listen_on(ListenerId::new(1), addr).unwrap();
1176-
poll_fn(|cx| Pin::new(&mut tcp).poll(cx))
1177-
.await
1178-
.into_new_address()
1179-
.expect("listen address")
1212+
tcp.select_next_some().await.into_new_address().expect("listen address")
11801213
}
11811214

11821215
fn test(addr: Multiaddr) {

0 commit comments

Comments
 (0)