Skip to content

Commit 09ff28f

Browse files
Replace the macro in libp2p-tcp with a generic, inner implementation
To avoid code duplication, a generic inner implementation is used that does all of the heavy lifting. To abstract over the individual types, we create traits that are private to the module. Consumers of the crate shouldn't be concerned with this because a) none of the generic types are exported b) there are no type parameters in public interfaces
1 parent 96cd509 commit 09ff28f

File tree

19 files changed

+941
-653
lines changed

19 files changed

+941
-653
lines changed

core/src/connection/listeners.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use std::{collections::VecDeque, fmt, pin::Pin};
4545
/// use futures::prelude::*;
4646
/// use libp2p_core::connection::{ListenersEvent, ListenersStream};
4747
///
48-
/// let mut listeners = ListenersStream::new(libp2p_tcp::TcpConfig::new());
48+
/// let mut listeners = ListenersStream::new(libp2p_tcp::async_std::TcpConfig::new());
4949
///
5050
/// // Ask the `listeners` to start listening on the given multiaddress.
5151
/// listeners.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();

core/tests/network_dial_error.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn deny_incoming_connec() {
9393
let mut swarm1: Network<_, _, _, NodeHandlerWrapperBuilder<TestHandler>, _, _> = {
9494
let local_key = identity::Keypair::generate_ed25519();
9595
let local_public_key = local_key.public();
96-
let transport = libp2p_tcp::TcpConfig::new()
96+
let transport = libp2p_tcp::async_std::TcpConfig::new()
9797
.upgrade(upgrade::Version::V1)
9898
.authenticate(libp2p_secio::SecioConfig::new(local_key))
9999
.multiplex(libp2p_mplex::MplexConfig::new())
@@ -104,7 +104,7 @@ fn deny_incoming_connec() {
104104
let mut swarm2 = {
105105
let local_key = identity::Keypair::generate_ed25519();
106106
let local_public_key = local_key.public();
107-
let transport = libp2p_tcp::TcpConfig::new()
107+
let transport = libp2p_tcp::async_std::TcpConfig::new()
108108
.upgrade(upgrade::Version::V1)
109109
.authenticate(libp2p_secio::SecioConfig::new(local_key))
110110
.multiplex(libp2p_mplex::MplexConfig::new())
@@ -170,7 +170,7 @@ fn dial_self() {
170170
let mut swarm = {
171171
let local_key = identity::Keypair::generate_ed25519();
172172
let local_public_key = local_key.public();
173-
let transport = libp2p_tcp::TcpConfig::new()
173+
let transport = libp2p_tcp::async_std::TcpConfig::new()
174174
.upgrade(upgrade::Version::V1)
175175
.authenticate(libp2p_secio::SecioConfig::new(local_key))
176176
.multiplex(libp2p_mplex::MplexConfig::new())
@@ -245,7 +245,7 @@ fn dial_self_by_id() {
245245
let mut swarm: Network<_, _, _, NodeHandlerWrapperBuilder<TestHandler>> = {
246246
let local_key = identity::Keypair::generate_ed25519();
247247
let local_public_key = local_key.public();
248-
let transport = libp2p_tcp::TcpConfig::new()
248+
let transport = libp2p_tcp::async_std::TcpConfig::new()
249249
.upgrade(upgrade::Version::V1)
250250
.authenticate(libp2p_secio::SecioConfig::new(local_key))
251251
.multiplex(libp2p_mplex::MplexConfig::new())
@@ -264,7 +264,7 @@ fn multiple_addresses_err() {
264264
let mut swarm = {
265265
let local_key = identity::Keypair::generate_ed25519();
266266
let local_public_key = local_key.public();
267-
let transport = libp2p_tcp::TcpConfig::new()
267+
let transport = libp2p_tcp::async_std::TcpConfig::new()
268268
.upgrade(upgrade::Version::V1)
269269
.authenticate(libp2p_secio::SecioConfig::new(local_key))
270270
.multiplex(libp2p_mplex::MplexConfig::new())

examples/ipfs-private.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use libp2p::{
4343
pnet::{PnetConfig, PreSharedKey},
4444
secio::SecioConfig,
4545
swarm::NetworkBehaviourEventProcess,
46-
tcp::TcpConfig,
46+
tcp::async_std::TcpConfig,
4747
yamux::Config as YamuxConfig,
4848
Multiaddr, NetworkBehaviour, PeerId, Swarm, Transport,
4949
};

muxers/mplex/tests/async_write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// DEALINGS IN THE SOFTWARE.
2020

2121
use libp2p_core::{muxing, upgrade, Transport};
22-
use libp2p_tcp::TcpConfig;
22+
use libp2p_tcp::async_std::TcpConfig;
2323
use futures::{prelude::*, channel::oneshot};
2424
use std::sync::Arc;
2525

muxers/mplex/tests/two_peers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// DEALINGS IN THE SOFTWARE.
2020

2121
use libp2p_core::{muxing, upgrade, Transport};
22-
use libp2p_tcp::TcpConfig;
22+
use libp2p_tcp::async_std::TcpConfig;
2323
use futures::{channel::oneshot, prelude::*};
2424
use std::sync::Arc;
2525

protocols/deflate/tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use futures::{future, prelude::*};
2222
use libp2p_core::{transport::Transport, upgrade};
2323
use libp2p_deflate::DeflateConfig;
24-
use libp2p_tcp::TcpConfig;
24+
use libp2p_tcp::async_std::TcpConfig;
2525
use quickcheck::{QuickCheck, RngCore, TestResult};
2626

2727
#[test]

protocols/identify/src/identify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ mod tests {
262262
Transport,
263263
upgrade
264264
};
265-
use libp2p_tcp::TcpConfig;
265+
use libp2p_tcp::async_std::TcpConfig;
266266
use libp2p_secio::SecioConfig;
267267
use libp2p_swarm::{Swarm, SwarmEvent};
268268
use libp2p_mplex::MplexConfig;

protocols/identify/src/protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ fn parse_proto_msg(msg: impl AsRef<[u8]>) -> Result<(IdentifyInfo, Multiaddr), i
207207
#[cfg(test)]
208208
mod tests {
209209
use crate::protocol::{IdentifyInfo, RemoteInfo, IdentifyProtocolConfig};
210-
use libp2p_tcp::TcpConfig;
210+
use libp2p_tcp::async_std::TcpConfig;
211211
use futures::{prelude::*, channel::oneshot};
212212
use libp2p_core::{
213213
identity,

protocols/noise/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
//!
3838
//! ```
3939
//! use libp2p_core::{identity, Transport, upgrade};
40-
//! use libp2p_tcp::TcpConfig;
40+
//! use libp2p_tcp::async_std::TcpConfig;
4141
//! use libp2p_noise::{Keypair, X25519, NoiseConfig};
4242
//!
4343
//! # fn main() {

protocols/noise/tests/smoke.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use libp2p_core::identity;
2323
use libp2p_core::upgrade::{self, Negotiated, apply_inbound, apply_outbound};
2424
use libp2p_core::transport::{Transport, ListenerEvent};
2525
use libp2p_noise::{Keypair, X25519, NoiseConfig, RemoteIdentity, NoiseError, NoiseOutput};
26-
use libp2p_tcp::{TcpConfig, TcpTransStream};
26+
use libp2p_tcp::{async_std::TcpConfig, async_std::TcpTransStream};
2727
use log::info;
2828
use quickcheck::QuickCheck;
2929
use std::{convert::TryInto, io};

protocols/ping/tests/ping.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use libp2p_core::{
3131
use libp2p_ping::*;
3232
use libp2p_secio::SecioConfig;
3333
use libp2p_swarm::Swarm;
34-
use libp2p_tcp::TcpConfig;
34+
use libp2p_tcp::async_std::TcpConfig;
3535
use futures::{prelude::*, channel::mpsc};
3636
use std::{io, time::Duration};
3737

protocols/secio/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
//! use libp2p_core::{PeerId, Multiaddr, identity, upgrade};
3535
//! use libp2p_core::transport::Transport;
3636
//! use libp2p_mplex::MplexConfig;
37-
//! use libp2p_tcp::TcpConfig;
37+
//! use libp2p_tcp::async_std::TcpConfig;
3838
//!
3939
//! // Create a local peer identity.
4040
//! let local_keys = identity::Keypair::generate_ed25519();

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
//! Example (Dialing a TCP/IP multi-address):
4848
//!
4949
//! ```rust
50-
//! use libp2p::{Multiaddr, Transport, tcp::TcpConfig};
50+
//! use libp2p::{Multiaddr, Transport, tcp::async_std::TcpConfig};
5151
//! let tcp = TcpConfig::new();
5252
//! let addr: Multiaddr = "/ip4/98.97.96.95/tcp/20500".parse().expect("invalid multiaddr");
5353
//! let _conn = tcp.dial(addr);
@@ -86,7 +86,7 @@
8686
//!
8787
//! ```rust
8888
//! # #[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp", feature = "secio", feature = "yamux"))] {
89-
//! use libp2p::{Transport, core::upgrade, tcp::TcpConfig, secio::SecioConfig, identity::Keypair, yamux};
89+
//! use libp2p::{Transport, core::upgrade, tcp::async_std::TcpConfig, secio::SecioConfig, identity::Keypair, yamux};
9090
//! let tcp = TcpConfig::new();
9191
//! let secio = SecioConfig::new(Keypair::generate_ed25519());
9292
//! let yamux = yamux::Config::default();
@@ -141,7 +141,7 @@
141141
//! [`Keypair`]: identity::Keypair
142142
//! [`PublicKey`]: identity::PublicKey
143143
//! [`Future`]: futures::Future
144-
//! [`TcpConfig`]: tcp::TcpConfig
144+
//! [`TcpConfig`]: tcp::async_std::TcpConfig
145145
//! [`NetworkBehaviour`]: swarm::NetworkBehaviour
146146
//! [`StreamMuxer`]: core::muxing::StreamMuxer
147147
//! [`Yamux`]: yamux::Yamux

transports/tcp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ipnet = "2.0.0"
1818
libp2p-core = { version = "0.16.0", path = "../../core" }
1919
log = "0.4.1"
2020
tokio = { version = "0.2", default-features = false, features = ["tcp"], optional = true }
21+
async-trait = "0.1"
2122

2223
[features]
2324
default = ["async-std"]

0 commit comments

Comments
 (0)