Skip to content

Commit cea2d5f

Browse files
committed
review: address Thomas review
1 parent 324cdac commit cea2d5f

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

libp2p/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ pub use libp2p_tls as tls;
121121
#[doc(inline)]
122122
pub use libp2p_uds as uds;
123123
#[cfg(feature = "upnp")]
124+
#[cfg(not(target_arch = "wasm32"))]
124125
#[doc(inline)]
125126
pub use libp2p_upnp as upnp;
126127
#[cfg(feature = "wasm-ext")]

protocols/upnp/src/behaviour.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,37 +51,35 @@ const MAPPING_DURATION: u64 = 3600;
5151
const MAPPING_TIMEOUT: u64 = MAPPING_DURATION / 2;
5252

5353
/// Map a port on the gateway.
54-
fn map_port<P: Provider + 'static>(
54+
async fn map_port<P: Provider + 'static>(
5555
gateway: Arc<P::Gateway>,
5656
mapping: Mapping,
5757
permanent: bool,
58-
) -> BoxFuture<'static, Event> {
58+
) -> Event {
5959
let duration = if permanent { 0 } else { MAPPING_DURATION };
6060

61-
P::add_port(
61+
match P::add_port(
6262
gateway,
6363
mapping.protocol,
6464
mapping.internal_addr,
6565
Duration::from_secs(duration),
6666
)
67-
.map(move |result| match result {
67+
.await
68+
{
6869
Ok(()) => Event::Mapped(mapping),
6970
Err(err) => Event::MapFailure(mapping, err),
70-
})
71-
.boxed()
71+
}
7272
}
7373

7474
/// Remove a port mapping on the gateway.
75-
fn remove_port_mapping<P: Provider + 'static>(
75+
async fn remove_port_mapping<P: Provider + 'static>(
7676
gateway: Arc<P::Gateway>,
7777
mapping: Mapping,
78-
) -> BoxFuture<'static, Event> {
79-
P::remove_port(gateway, mapping.protocol, mapping.internal_addr.port())
80-
.map(move |result| match result {
81-
Ok(()) => Event::Removed(mapping),
82-
Err(err) => Event::RemovalFailure(mapping, err),
83-
})
84-
.boxed()
78+
) -> Event {
79+
match P::remove_port(gateway, mapping.protocol, mapping.internal_addr.port()).await {
80+
Ok(()) => Event::Removed(mapping),
81+
Err(err) => Event::RemovalFailure(mapping, err),
82+
}
8583
}
8684

8785
/// A [`Provider::Gateway`] event.
@@ -260,11 +258,10 @@ where
260258
multiaddr: multiaddr.clone(),
261259
};
262260

263-
self.pending_events.push(map_port::<P>(
264-
gateway.clone(),
265-
mapping.clone(),
266-
self.config.permanent,
267-
));
261+
self.pending_events.push(
262+
map_port::<P>(gateway.clone(), mapping.clone(), self.config.permanent)
263+
.boxed(),
264+
);
268265

269266
self.mappings.insert(mapping, MappingState::Pending);
270267
}
@@ -281,8 +278,9 @@ where
281278
}) => {
282279
if let GatewayState::Available((gateway, _external_addr)) = &self.state {
283280
if let Some((mapping, _state)) = self.mappings.remove_entry(&listener_id) {
284-
self.pending_events
285-
.push(remove_port_mapping::<P>(gateway.clone(), mapping.clone()));
281+
self.pending_events.push(
282+
remove_port_mapping::<P>(gateway.clone(), mapping.clone()).boxed(),
283+
);
286284
self.mappings.insert(mapping, MappingState::Pending);
287285
}
288286
}
@@ -435,8 +433,9 @@ where
435433
mapping.internal_addr,
436434
mapping.protocol
437435
);
438-
self.pending_events
439-
.push(remove_port_mapping::<P>(gateway.clone(), mapping));
436+
self.pending_events.push(
437+
remove_port_mapping::<P>(gateway.clone(), mapping).boxed(),
438+
);
440439
}
441440
}
442441
}
@@ -445,20 +444,22 @@ where
445444
for (mapping, state) in self.mappings.iter_mut() {
446445
match state {
447446
MappingState::Inactive => {
448-
self.pending_events.push(map_port::<P>(
449-
gateway.clone(),
450-
mapping.clone(),
451-
self.config.permanent,
452-
));
447+
self.pending_events.push(
448+
map_port::<P>(
449+
gateway.clone(),
450+
mapping.clone(),
451+
self.config.permanent,
452+
)
453+
.boxed(),
454+
);
453455
*state = MappingState::Pending;
454456
}
455457
MappingState::Active(timeout) => {
456458
if Pin::new(timeout).poll(cx).is_ready() {
457-
self.pending_events.push(map_port::<P>(
458-
gateway.clone(),
459-
mapping.clone(),
460-
false,
461-
));
459+
self.pending_events.push(
460+
map_port::<P>(gateway.clone(), mapping.clone(), false)
461+
.boxed(),
462+
);
462463
}
463464
}
464465
MappingState::Pending | MappingState::Permanent => {}

protocols/upnp/src/provider.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl fmt::Display for Protocol {
5555
#[async_trait]
5656
pub trait Provider {
5757
/// The gateway of obtained from [`Provider::search_gateway`].
58-
type Gateway;
58+
type Gateway: Send + Sync;
5959

6060
/// Search for the gateway endpoint on the local network.
6161
async fn search_gateway(config: Config) -> Result<(Self::Gateway, Ipv4Addr), Box<dyn Error>>;

0 commit comments

Comments
 (0)