diff --git a/Cargo.lock b/Cargo.lock index ac65510b669..c7e3843d9a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2718,7 +2718,6 @@ dependencies = [ name = "libp2p-autonat" version = "0.13.1" dependencies = [ - "async-trait", "asynchronous-codec", "either", "futures", @@ -2814,11 +2813,10 @@ dependencies = [ [[package]] name = "libp2p-dns" -version = "0.42.1" +version = "0.43.0" dependencies = [ "async-std", "async-std-resolver", - "async-trait", "futures", "hickory-resolver", "libp2p-core", @@ -3221,7 +3219,6 @@ dependencies = [ name = "libp2p-rendezvous" version = "0.15.1" dependencies = [ - "async-trait", "asynchronous-codec", "bimap", "futures", @@ -3247,7 +3244,6 @@ version = "0.28.0" dependencies = [ "anyhow", "async-std", - "async-trait", "cbor4ii", "futures", "futures-bounded", @@ -3344,7 +3340,6 @@ dependencies = [ name = "libp2p-swarm-test" version = "0.5.0" dependencies = [ - "async-trait", "futures", "futures-timer", "libp2p-core", diff --git a/Cargo.toml b/Cargo.toml index eecd6a3759c..3030a6e449b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,7 +78,7 @@ libp2p-autonat = { version = "0.13.1", path = "protocols/autonat" } libp2p-connection-limits = { version = "0.4.1", path = "misc/connection-limits" } libp2p-core = { version = "0.42.1", path = "core" } libp2p-dcutr = { version = "0.12.1", path = "protocols/dcutr" } -libp2p-dns = { version = "0.42.1", path = "transports/dns" } +libp2p-dns = { version = "0.43.0", path = "transports/dns" } libp2p-floodsub = { version = "0.45.0", path = "protocols/floodsub" } libp2p-gossipsub = { version = "0.48.0", path = "protocols/gossipsub" } libp2p-identify = { version = "0.46.0", path = "protocols/identify" } diff --git a/protocols/autonat/Cargo.toml b/protocols/autonat/Cargo.toml index 294cdd54de4..bfae6be42cb 100644 --- a/protocols/autonat/Cargo.toml +++ b/protocols/autonat/Cargo.toml @@ -16,7 +16,6 @@ categories = ["network-programming", "asynchronous"] [dependencies] -async-trait = { version = "0.1", optional = true } asynchronous-codec = { workspace = true } either = { version = "1.9.0", optional = true } futures = { workspace = true } @@ -43,7 +42,7 @@ libp2p-swarm = { workspace = true, features = ["macros"] } [features] default = ["v1", "v2"] -v1 = ["dep:libp2p-request-response", "dep:web-time", "dep:async-trait"] +v1 = ["dep:libp2p-request-response", "dep:web-time"] v2 = ["dep:either", "dep:futures-bounded", "dep:thiserror", "dep:rand_core"] # Passing arguments to the docsrs builder in order to properly document cfg's. diff --git a/protocols/autonat/src/v1/protocol.rs b/protocols/autonat/src/v1/protocol.rs index 6aa0c99167b..6cbca5b5463 100644 --- a/protocols/autonat/src/v1/protocol.rs +++ b/protocols/autonat/src/v1/protocol.rs @@ -18,9 +18,8 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use std::io; +use std::{future::Future, io}; -use async_trait::async_trait; use asynchronous_codec::{FramedRead, FramedWrite}; use futures::{ io::{AsyncRead, AsyncWrite}, @@ -39,72 +38,83 @@ pub const DEFAULT_PROTOCOL_NAME: StreamProtocol = StreamProtocol::new("/libp2p/a #[derive(Clone)] pub struct AutoNatCodec; -#[async_trait] impl request_response::Codec for AutoNatCodec { type Protocol = StreamProtocol; type Request = DialRequest; type Response = DialResponse; - async fn read_request(&mut self, _: &StreamProtocol, io: &mut T) -> io::Result + fn read_request( + &mut self, + _: &StreamProtocol, + io: &mut T, + ) -> impl Send + Future> where T: AsyncRead + Send + Unpin, { - let message = FramedRead::new(io, codec()) - .next() - .await - .ok_or(io::ErrorKind::UnexpectedEof)??; - let request = DialRequest::from_proto(message)?; - - Ok(request) + async move { + let message = FramedRead::new(io, codec()) + .next() + .await + .ok_or(io::ErrorKind::UnexpectedEof)??; + let request = DialRequest::from_proto(message)?; + + Ok(request) + } } - async fn read_response( + fn read_response( &mut self, _: &StreamProtocol, io: &mut T, - ) -> io::Result + ) -> impl Send + Future> where T: AsyncRead + Send + Unpin, { - let message = FramedRead::new(io, codec()) - .next() - .await - .ok_or(io::ErrorKind::UnexpectedEof)??; - let response = DialResponse::from_proto(message)?; - - Ok(response) + async move { + let message = FramedRead::new(io, codec()) + .next() + .await + .ok_or(io::ErrorKind::UnexpectedEof)??; + let response = DialResponse::from_proto(message)?; + + Ok(response) + } } - async fn write_request( + fn write_request( &mut self, _: &StreamProtocol, io: &mut T, data: Self::Request, - ) -> io::Result<()> + ) -> impl Send + Future> where T: AsyncWrite + Send + Unpin, { - let mut framed = FramedWrite::new(io, codec()); - framed.send(data.into_proto()).await?; - framed.close().await?; + async move { + let mut framed = FramedWrite::new(io, codec()); + framed.send(data.into_proto()).await?; + framed.close().await?; - Ok(()) + Ok(()) + } } - async fn write_response( + fn write_response( &mut self, _: &StreamProtocol, io: &mut T, data: Self::Response, - ) -> io::Result<()> + ) -> impl Send + Future> where T: AsyncWrite + Send + Unpin, { - let mut framed = FramedWrite::new(io, codec()); - framed.send(data.into_proto()).await?; - framed.close().await?; + async move { + let mut framed = FramedWrite::new(io, codec()); + framed.send(data.into_proto()).await?; + framed.close().await?; - Ok(()) + Ok(()) + } } } diff --git a/protocols/rendezvous/Cargo.toml b/protocols/rendezvous/Cargo.toml index 9521913cd30..47b68ed7e0f 100644 --- a/protocols/rendezvous/Cargo.toml +++ b/protocols/rendezvous/Cargo.toml @@ -12,7 +12,6 @@ categories = ["network-programming", "asynchronous"] [dependencies] asynchronous-codec = { workspace = true } -async-trait = "0.1" bimap = "0.6.3" futures = { workspace = true, features = ["std"] } futures-timer = "3.0.3" diff --git a/protocols/rendezvous/src/codec.rs b/protocols/rendezvous/src/codec.rs index 60f9f14f332..e56699e2bc0 100644 --- a/protocols/rendezvous/src/codec.rs +++ b/protocols/rendezvous/src/codec.rs @@ -18,9 +18,8 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use std::{fmt, io}; +use std::{fmt, future::Future, io}; -use async_trait::async_trait; use asynchronous_codec::{BytesMut, Decoder, Encoder, FramedRead, FramedWrite}; use futures::{AsyncRead, AsyncWrite, SinkExt, StreamExt}; use libp2p_core::{peer_record, signed_envelope, PeerRecord, SignedEnvelope}; @@ -241,66 +240,77 @@ impl Decoder for Codec { #[derive(Clone, Default)] pub struct Codec {} -#[async_trait] impl libp2p_request_response::Codec for Codec { type Protocol = StreamProtocol; type Request = Message; type Response = Message; - async fn read_request(&mut self, _: &Self::Protocol, io: &mut T) -> io::Result + fn read_request( + &mut self, + _: &Self::Protocol, + io: &mut T, + ) -> impl Send + Future> where T: AsyncRead + Unpin + Send, { - let message = FramedRead::new(io, self.clone()) - .next() - .await - .ok_or(io::ErrorKind::UnexpectedEof)??; + async move { + let message = FramedRead::new(io, self.clone()) + .next() + .await + .ok_or(io::ErrorKind::UnexpectedEof)??; - Ok(message) + Ok(message) + } } - async fn read_response( + fn read_response( &mut self, _: &Self::Protocol, io: &mut T, - ) -> io::Result + ) -> impl Send + Future> where T: AsyncRead + Unpin + Send, { - let message = FramedRead::new(io, self.clone()) - .next() - .await - .ok_or(io::ErrorKind::UnexpectedEof)??; + async move { + let message = FramedRead::new(io, self.clone()) + .next() + .await + .ok_or(io::ErrorKind::UnexpectedEof)??; - Ok(message) + Ok(message) + } } - async fn write_request( + fn write_request( &mut self, _: &Self::Protocol, io: &mut T, req: Self::Request, - ) -> io::Result<()> + ) -> impl Send + Future> where T: AsyncWrite + Unpin + Send, { - FramedWrite::new(io, self.clone()).send(req).await?; + async move { + FramedWrite::new(io, self.clone()).send(req).await?; - Ok(()) + Ok(()) + } } - async fn write_response( + fn write_response( &mut self, _: &Self::Protocol, io: &mut T, res: Self::Response, - ) -> io::Result<()> + ) -> impl Send + Future> where T: AsyncWrite + Unpin + Send, { - FramedWrite::new(io, self.clone()).send(res).await?; + async move { + FramedWrite::new(io, self.clone()).send(res).await?; - Ok(()) + Ok(()) + } } } diff --git a/protocols/request-response/CHANGELOG.md b/protocols/request-response/CHANGELOG.md index 34fc27b7432..e796cd2deb5 100644 --- a/protocols/request-response/CHANGELOG.md +++ b/protocols/request-response/CHANGELOG.md @@ -6,6 +6,8 @@ - Allow configurable request and response sizes for `json` and `cbor` codec. See [PR 5792](https://github.com/libp2p/rust-libp2p/pull/5792). +- Remove async-trait for RPIT. See [PR 5812](https:/github.com/libp2p/rust-libp2p/pull/5812). + ## 0.27.1 - Deprecate `void` crate. diff --git a/protocols/request-response/Cargo.toml b/protocols/request-response/Cargo.toml index 5cd711dd051..a699fab9e8b 100644 --- a/protocols/request-response/Cargo.toml +++ b/protocols/request-response/Cargo.toml @@ -11,7 +11,6 @@ keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] [dependencies] -async-trait = "0.1" cbor4ii = { version = "0.3.2", features = ["serde1", "use_std"], optional = true } futures = { workspace = true } libp2p-core = { workspace = true } diff --git a/protocols/request-response/src/cbor.rs b/protocols/request-response/src/cbor.rs index eac1944bb09..3e5b4dafa48 100644 --- a/protocols/request-response/src/cbor.rs +++ b/protocols/request-response/src/cbor.rs @@ -47,9 +47,10 @@ pub type Behaviour = crate::Behaviour>; mod codec { - use std::{collections::TryReserveError, convert::Infallible, io, marker::PhantomData}; + use std::{ + collections::TryReserveError, convert::Infallible, future::Future, io, marker::PhantomData, + }; - use async_trait::async_trait; use cbor4ii::core::error::DecodeError; use futures::prelude::*; use libp2p_swarm::StreamProtocol; @@ -97,7 +98,6 @@ mod codec { } } - #[async_trait] impl crate::Codec for Codec where Req: Send + Serialize + DeserializeOwned, @@ -107,64 +107,80 @@ mod codec { type Request = Req; type Response = Resp; - async fn read_request(&mut self, _: &Self::Protocol, io: &mut T) -> io::Result + fn read_request( + &mut self, + _: &Self::Protocol, + io: &mut T, + ) -> impl Send + Future> where T: AsyncRead + Unpin + Send, { - let mut vec = Vec::new(); + async move { + let mut vec = Vec::new(); - io.take(self.request_size_maximum) - .read_to_end(&mut vec) - .await?; + io.take(self.request_size_maximum) + .read_to_end(&mut vec) + .await?; - cbor4ii::serde::from_slice(vec.as_slice()).map_err(decode_into_io_error) + cbor4ii::serde::from_slice(vec.as_slice()).map_err(decode_into_io_error) + } } - async fn read_response(&mut self, _: &Self::Protocol, io: &mut T) -> io::Result + fn read_response( + &mut self, + _: &Self::Protocol, + io: &mut T, + ) -> impl Send + Future> where T: AsyncRead + Unpin + Send, { - let mut vec = Vec::new(); + async move { + let mut vec = Vec::new(); - io.take(self.response_size_maximum) - .read_to_end(&mut vec) - .await?; + io.take(self.response_size_maximum) + .read_to_end(&mut vec) + .await?; - cbor4ii::serde::from_slice(vec.as_slice()).map_err(decode_into_io_error) + cbor4ii::serde::from_slice(vec.as_slice()).map_err(decode_into_io_error) + } } - async fn write_request( + fn write_request( &mut self, _: &Self::Protocol, io: &mut T, req: Self::Request, - ) -> io::Result<()> + ) -> impl Send + Future> where T: AsyncWrite + Unpin + Send, { - let data: Vec = - cbor4ii::serde::to_vec(Vec::new(), &req).map_err(encode_into_io_error)?; + async move { + let data: Vec = + cbor4ii::serde::to_vec(Vec::new(), &req).map_err(encode_into_io_error)?; - io.write_all(data.as_ref()).await?; + io.write_all(data.as_ref()).await?; - Ok(()) + Ok(()) + } } - async fn write_response( + fn write_response( &mut self, _: &Self::Protocol, io: &mut T, resp: Self::Response, - ) -> io::Result<()> + ) -> impl Send + Future> where T: AsyncWrite + Unpin + Send, { - let data: Vec = - cbor4ii::serde::to_vec(Vec::new(), &resp).map_err(encode_into_io_error)?; + async move { + let data: Vec = + cbor4ii::serde::to_vec(Vec::new(), &resp).map_err(encode_into_io_error)?; - io.write_all(data.as_ref()).await?; + io.write_all(data.as_ref()).await?; - Ok(()) + Ok(()) + } } } diff --git a/protocols/request-response/src/codec.rs b/protocols/request-response/src/codec.rs index d396a75ad7b..19e302e653f 100644 --- a/protocols/request-response/src/codec.rs +++ b/protocols/request-response/src/codec.rs @@ -20,13 +20,11 @@ use std::io; -use async_trait::async_trait; use futures::prelude::*; /// A `Codec` defines the request and response types /// for a request-response [`Behaviour`](crate::Behaviour) protocol or /// protocol family and how they are encoded / decoded on an I/O stream. -#[async_trait] pub trait Codec { /// The type of protocol(s) or protocol versions being negotiated. type Protocol: AsRef + Send + Clone; @@ -37,43 +35,43 @@ pub trait Codec { /// Reads a request from the given I/O stream according to the /// negotiated protocol. - async fn read_request( + fn read_request( &mut self, protocol: &Self::Protocol, io: &mut T, - ) -> io::Result + ) -> impl Send + Future> where T: AsyncRead + Unpin + Send; /// Reads a response from the given I/O stream according to the /// negotiated protocol. - async fn read_response( + fn read_response( &mut self, protocol: &Self::Protocol, io: &mut T, - ) -> io::Result + ) -> impl Send + Future> where T: AsyncRead + Unpin + Send; /// Writes a request to the given I/O stream according to the /// negotiated protocol. - async fn write_request( + fn write_request( &mut self, protocol: &Self::Protocol, io: &mut T, req: Self::Request, - ) -> io::Result<()> + ) -> impl Send + Future> where T: AsyncWrite + Unpin + Send; /// Writes a response to the given I/O stream according to the /// negotiated protocol. - async fn write_response( + fn write_response( &mut self, protocol: &Self::Protocol, io: &mut T, res: Self::Response, - ) -> io::Result<()> + ) -> impl Send + Future> where T: AsyncWrite + Unpin + Send; } diff --git a/protocols/request-response/src/json.rs b/protocols/request-response/src/json.rs index f151b16bf5f..975dd91021f 100644 --- a/protocols/request-response/src/json.rs +++ b/protocols/request-response/src/json.rs @@ -49,7 +49,6 @@ pub type Behaviour = crate::Behaviour>; mod codec { use std::{io, marker::PhantomData}; - use async_trait::async_trait; use futures::prelude::*; use libp2p_swarm::StreamProtocol; use serde::{de::DeserializeOwned, Serialize}; @@ -96,7 +95,6 @@ mod codec { } } - #[async_trait] impl crate::Codec for Codec where Req: Send + Serialize + DeserializeOwned, @@ -106,62 +104,78 @@ mod codec { type Request = Req; type Response = Resp; - async fn read_request(&mut self, _: &Self::Protocol, io: &mut T) -> io::Result + fn read_request( + &mut self, + _: &Self::Protocol, + io: &mut T, + ) -> impl Send + Future> where T: AsyncRead + Unpin + Send, { - let mut vec = Vec::new(); + async move { + let mut vec = Vec::new(); - io.take(self.request_size_maximum) - .read_to_end(&mut vec) - .await?; + io.take(self.request_size_maximum) + .read_to_end(&mut vec) + .await?; - Ok(serde_json::from_slice(vec.as_slice())?) + Ok(serde_json::from_slice(vec.as_slice())?) + } } - async fn read_response(&mut self, _: &Self::Protocol, io: &mut T) -> io::Result + fn read_response( + &mut self, + _: &Self::Protocol, + io: &mut T, + ) -> impl Send + Future> where T: AsyncRead + Unpin + Send, { - let mut vec = Vec::new(); + async move { + let mut vec = Vec::new(); - io.take(self.response_size_maximum) - .read_to_end(&mut vec) - .await?; + io.take(self.response_size_maximum) + .read_to_end(&mut vec) + .await?; - Ok(serde_json::from_slice(vec.as_slice())?) + Ok(serde_json::from_slice(vec.as_slice())?) + } } - async fn write_request( + fn write_request( &mut self, _: &Self::Protocol, io: &mut T, req: Self::Request, - ) -> io::Result<()> + ) -> impl Send + Future> where T: AsyncWrite + Unpin + Send, { - let data = serde_json::to_vec(&req)?; + async move { + let data = serde_json::to_vec(&req)?; - io.write_all(data.as_ref()).await?; + io.write_all(data.as_ref()).await?; - Ok(()) + Ok(()) + } } - async fn write_response( + fn write_response( &mut self, _: &Self::Protocol, io: &mut T, resp: Self::Response, - ) -> io::Result<()> + ) -> impl Send + Future> where T: AsyncWrite + Unpin + Send, { - let data = serde_json::to_vec(&resp)?; + async move { + let data = serde_json::to_vec(&resp)?; - io.write_all(data.as_ref()).await?; + io.write_all(data.as_ref()).await?; - Ok(()) + Ok(()) + } } } } diff --git a/protocols/request-response/tests/error_reporting.rs b/protocols/request-response/tests/error_reporting.rs index 2108b6006c5..b882d29beab 100644 --- a/protocols/request-response/tests/error_reporting.rs +++ b/protocols/request-response/tests/error_reporting.rs @@ -2,7 +2,6 @@ use std::{io, iter, pin::pin, time::Duration}; use anyhow::{bail, Result}; use async_std::task::sleep; -use async_trait::async_trait; use futures::prelude::*; use libp2p_identity::PeerId; use libp2p_request_response as request_response; @@ -417,106 +416,113 @@ impl TryFrom for Action { } } -#[async_trait] impl Codec for TestCodec { type Protocol = StreamProtocol; type Request = Action; type Response = Action; - async fn read_request( + fn read_request( &mut self, _protocol: &Self::Protocol, io: &mut T, - ) -> io::Result + ) -> impl Send + Future> where T: AsyncRead + Unpin + Send, { - let mut buf = Vec::new(); - io.read_to_end(&mut buf).await?; + async move { + let mut buf = Vec::new(); + io.read_to_end(&mut buf).await?; - if buf.is_empty() { - return Err(io::ErrorKind::UnexpectedEof.into()); - } + if buf.is_empty() { + return Err(io::ErrorKind::UnexpectedEof.into()); + } - assert_eq!(buf.len(), 1); + assert_eq!(buf.len(), 1); - match buf[0].try_into()? { - Action::FailOnReadRequest => { - Err(io::Error::new(io::ErrorKind::Other, "FailOnReadRequest")) + match buf[0].try_into()? { + Action::FailOnReadRequest => { + Err(io::Error::new(io::ErrorKind::Other, "FailOnReadRequest")) + } + action => Ok(action), } - action => Ok(action), } } - async fn read_response( + fn read_response( &mut self, _protocol: &Self::Protocol, io: &mut T, - ) -> io::Result + ) -> impl Send + Future> where T: AsyncRead + Unpin + Send, { - let mut buf = Vec::new(); - io.read_to_end(&mut buf).await?; + async move { + let mut buf = Vec::new(); + io.read_to_end(&mut buf).await?; - if buf.is_empty() { - return Err(io::ErrorKind::UnexpectedEof.into()); - } + if buf.is_empty() { + return Err(io::ErrorKind::UnexpectedEof.into()); + } - assert_eq!(buf.len(), 1); + assert_eq!(buf.len(), 1); - match buf[0].try_into()? { - Action::FailOnReadResponse => { - Err(io::Error::new(io::ErrorKind::Other, "FailOnReadResponse")) + match buf[0].try_into()? { + Action::FailOnReadResponse => { + Err(io::Error::new(io::ErrorKind::Other, "FailOnReadResponse")) + } + Action::TimeoutOnReadResponse => loop { + sleep(Duration::MAX).await; + }, + action => Ok(action), } - Action::TimeoutOnReadResponse => loop { - sleep(Duration::MAX).await; - }, - action => Ok(action), } } - async fn write_request( + fn write_request( &mut self, _protocol: &Self::Protocol, io: &mut T, req: Self::Request, - ) -> io::Result<()> + ) -> impl Send + Future> where T: AsyncWrite + Unpin + Send, { - match req { - Action::FailOnWriteRequest => { - Err(io::Error::new(io::ErrorKind::Other, "FailOnWriteRequest")) - } - action => { - let bytes = [action.into()]; - io.write_all(&bytes).await?; - Ok(()) + async move { + match req { + Action::FailOnWriteRequest => { + Err(io::Error::new(io::ErrorKind::Other, "FailOnWriteRequest")) + } + action => { + let bytes = [action.into()]; + io.write_all(&bytes).await?; + Ok(()) + } } } } - async fn write_response( + fn write_response( &mut self, _protocol: &Self::Protocol, io: &mut T, res: Self::Response, - ) -> io::Result<()> + ) -> impl Send + Future> where T: AsyncWrite + Unpin + Send, { - match res { - Action::FailOnWriteResponse => { - Err(io::Error::new(io::ErrorKind::Other, "FailOnWriteResponse")) - } - Action::TimeoutOnWriteResponse => loop { - sleep(Duration::MAX).await; - }, - action => { - let bytes = [action.into()]; - io.write_all(&bytes).await?; - Ok(()) + async move { + match res { + Action::FailOnWriteResponse => { + Err(io::Error::new(io::ErrorKind::Other, "FailOnWriteResponse")) + } + Action::TimeoutOnWriteResponse => loop { + sleep(Duration::MAX).await; + }, + action => { + let bytes = [action.into()]; + io.write_all(&bytes).await?; + Ok(()) + } } } } diff --git a/swarm-test/CHANGELOG.md b/swarm-test/CHANGELOG.md index 1fd213e12f6..317922074d1 100644 --- a/swarm-test/CHANGELOG.md +++ b/swarm-test/CHANGELOG.md @@ -3,7 +3,8 @@ - Add `tokio` runtime support and make `tokio` and `async-std` runtimes optional behind features. See [PR 5551]. - Update default for idle-connection-timeout to 10s on `SwarmExt::new_ephemeral` methods. - See [PR 4967](https://github.com/libp2p/rust-libp2p/pull/4967). + See [PR 4967](https://github.com/libp2p/rust-libp2p/pull/4967). +- Remove async-trait for RPIT. See [PR 5812](https:/github.com/libp2p/rust-libp2p/pull/5812). [PR 5551]: https://github.com/libp2p/rust-libp2p/pull/5551 diff --git a/swarm-test/Cargo.toml b/swarm-test/Cargo.toml index 4a0d5ee8c71..7f86ba6e6c3 100644 --- a/swarm-test/Cargo.toml +++ b/swarm-test/Cargo.toml @@ -12,7 +12,6 @@ categories = ["network-programming", "asynchronous"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -async-trait = "0.1.80" libp2p-core = { workspace = true } libp2p-identity = { workspace = true, features = ["rand"] } libp2p-plaintext = { workspace = true } diff --git a/swarm-test/src/lib.rs b/swarm-test/src/lib.rs index 0bc417dd8b1..15a3df96061 100644 --- a/swarm-test/src/lib.rs +++ b/swarm-test/src/lib.rs @@ -18,9 +18,12 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use std::{fmt::Debug, future::IntoFuture, time::Duration}; +use std::{ + fmt::Debug, + future::{Future, IntoFuture}, + time::Duration, +}; -use async_trait::async_trait; use futures::{ future::{BoxFuture, Either}, FutureExt, StreamExt, @@ -34,7 +37,6 @@ use libp2p_swarm::{ /// An extension trait for [`Swarm`] that makes it /// easier to set up a network of [`Swarm`]s for tests. -#[async_trait] pub trait SwarmExt { type NB: NetworkBehaviour; @@ -67,7 +69,7 @@ pub trait SwarmExt { /// By default, this iterator will not yield any addresses. /// To add listen addresses as external addresses, use /// [`ListenFuture::with_memory_addr_external`] or [`ListenFuture::with_tcp_addr_external`]. - async fn connect(&mut self, other: &mut Swarm) + fn connect(&mut self, other: &mut Swarm) -> impl Send + Future where T: NetworkBehaviour + Send, ::ToSwarm: Debug; @@ -80,10 +82,10 @@ pub trait SwarmExt { /// /// Because we don't have access to the other [`Swarm`], /// we can't guarantee that it makes progress. - async fn dial_and_wait(&mut self, addr: Multiaddr) -> PeerId; + fn dial_and_wait(&mut self, addr: Multiaddr) -> impl Send + Future; /// Wait for specified condition to return `Some`. - async fn wait(&mut self, predicate: P) -> E + fn wait(&mut self, predicate: P) -> impl Send + Future where P: Fn(SwarmEvent<::ToSwarm>) -> Option, P: Send; @@ -97,14 +99,18 @@ pub trait SwarmExt { /// Returns the next [`SwarmEvent`] or times out after 10 seconds. /// /// If the 10s timeout does not fit your usecase, please fall back to `StreamExt::next`. - async fn next_swarm_event(&mut self) -> SwarmEvent<::ToSwarm>; + fn next_swarm_event( + &mut self, + ) -> impl Send + Unpin + Future::ToSwarm>>; /// Returns the next behaviour event or times out after 10 seconds. /// /// If the 10s timeout does not fit your usecase, please fall back to `StreamExt::next`. - async fn next_behaviour_event(&mut self) -> ::ToSwarm; + fn next_behaviour_event( + &mut self, + ) -> impl Send + Unpin + Future::ToSwarm>; - async fn loop_on_next(self); + fn loop_on_next(self) -> impl Send + Future; } /// Drives two [`Swarm`]s until a certain number of events are emitted. @@ -215,7 +221,6 @@ impl TryIntoOutput> } } -#[async_trait] impl SwarmExt for Swarm where B: NetworkBehaviour + Send, @@ -277,55 +282,59 @@ where ) } - async fn connect(&mut self, other: &mut Swarm) + fn connect(&mut self, other: &mut Swarm) -> impl Send + Future where T: NetworkBehaviour + Send, ::ToSwarm: Debug, { - let external_addresses = other.external_addresses().cloned().collect(); + async move { + let external_addresses = other.external_addresses().cloned().collect(); - let dial_opts = DialOpts::peer_id(*other.local_peer_id()) - .addresses(external_addresses) - .condition(PeerCondition::Always) - .build(); + let dial_opts = DialOpts::peer_id(*other.local_peer_id()) + .addresses(external_addresses) + .condition(PeerCondition::Always) + .build(); - self.dial(dial_opts).unwrap(); + self.dial(dial_opts).unwrap(); - let mut dialer_done = false; - let mut listener_done = false; + let mut dialer_done = false; + let mut listener_done = false; - loop { - match futures::future::select(self.next_swarm_event(), other.next_swarm_event()).await { - Either::Left((SwarmEvent::ConnectionEstablished { .. }, _)) => { - dialer_done = true; - } - Either::Right((SwarmEvent::ConnectionEstablished { .. }, _)) => { - listener_done = true; - } - Either::Left((other, _)) => { - tracing::debug!( - dialer=?other, - "Ignoring event from dialer" - ); - } - Either::Right((other, _)) => { - tracing::debug!( - listener=?other, - "Ignoring event from listener" - ); + loop { + match futures::future::select(self.next_swarm_event(), other.next_swarm_event()) + .await + { + Either::Left((SwarmEvent::ConnectionEstablished { .. }, _)) => { + dialer_done = true; + } + Either::Right((SwarmEvent::ConnectionEstablished { .. }, _)) => { + listener_done = true; + } + Either::Left((other, _)) => { + tracing::debug!( + dialer=?other, + "Ignoring event from dialer" + ); + } + Either::Right((other, _)) => { + tracing::debug!( + listener=?other, + "Ignoring event from listener" + ); + } } - } - if dialer_done && listener_done { - return; + if dialer_done && listener_done { + return; + } } } } - async fn dial_and_wait(&mut self, addr: Multiaddr) -> PeerId { + fn dial_and_wait(&mut self, addr: Multiaddr) -> impl Send + Future { self.dial(addr.clone()).unwrap(); - self.wait(|e| match e { + self.wait(move |e| match e { SwarmEvent::ConnectionEstablished { endpoint, peer_id, .. } => (endpoint.get_remote_address() == &addr).then_some(peer_id), @@ -337,18 +346,19 @@ where None } }) - .await } - async fn wait(&mut self, predicate: P) -> E + fn wait(&mut self, predicate: P) -> impl Send + Future where P: Fn(SwarmEvent<::ToSwarm>) -> Option, P: Send, { - loop { - let event = self.next_swarm_event().await; - if let Some(e) = predicate(event) { - break e; + async move { + loop { + let event = self.next_swarm_event().await; + if let Some(e) = predicate(event) { + break e; + } } } } @@ -361,33 +371,44 @@ where } } - async fn next_swarm_event(&mut self) -> SwarmEvent<::ToSwarm> { - match futures::future::select( - futures_timer::Delay::new(Duration::from_secs(10)), - self.select_next_some(), - ) - .await - { - Either::Left(((), _)) => panic!("Swarm did not emit an event within 10s"), - Either::Right((event, _)) => { - tracing::trace!(?event); + fn next_swarm_event( + &mut self, + ) -> impl Send + Unpin + Future::ToSwarm>> + { + Box::pin(async move { + match futures::future::select( + futures_timer::Delay::new(Duration::from_secs(10)), + self.select_next_some(), + ) + .await + { + Either::Left(((), _)) => panic!("Swarm did not emit an event within 10s"), + Either::Right((event, _)) => { + tracing::trace!(?event); - event + event + } } - } + }) } - async fn next_behaviour_event(&mut self) -> ::ToSwarm { - loop { - if let Ok(event) = self.next_swarm_event().await.try_into_behaviour_event() { - return event; + fn next_behaviour_event( + &mut self, + ) -> impl Send + Unpin + Future::ToSwarm> { + Box::pin(async move { + loop { + if let Ok(event) = self.next_swarm_event().await.try_into_behaviour_event() { + return event; + } } - } + }) } - async fn loop_on_next(mut self) { - while let Some(event) = self.next().await { - tracing::trace!(?event); + fn loop_on_next(mut self) -> impl Send + Future { + async move { + while let Some(event) = self.next().await { + tracing::trace!(?event); + } } } } diff --git a/transports/dns/CHANGELOG.md b/transports/dns/CHANGELOG.md index b46b0413403..ca283695b7f 100644 --- a/transports/dns/CHANGELOG.md +++ b/transports/dns/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.43.0 + +- Remove async-trait for RPIT. See [PR 5812](https:/github.com/libp2p/rust-libp2p/pull/5812). + ## 0.42.1 - Upgrade `async-std-resolver` and `hickory-resolver`. diff --git a/transports/dns/Cargo.toml b/transports/dns/Cargo.toml index 2a12c34a383..42542613eee 100644 --- a/transports/dns/Cargo.toml +++ b/transports/dns/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-dns" edition = "2021" rust-version = { workspace = true } description = "DNS transport implementation for libp2p" -version = "0.42.1" +version = "0.43.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -12,7 +12,6 @@ categories = ["network-programming", "asynchronous"] [dependencies] async-std-resolver = { workspace = true, features = ["system-config"], optional = true } -async-trait = "0.1.80" futures = { workspace = true } libp2p-core = { workspace = true } libp2p-identity = { workspace = true } diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index 7e3cf5d3c37..882a5cb2a6e 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -156,7 +156,6 @@ use std::{ task::{Context, Poll}, }; -use async_trait::async_trait; use futures::{future::BoxFuture, prelude::*}; pub use hickory_resolver::{ config::{ResolverConfig, ResolverOpts}, @@ -583,34 +582,56 @@ fn invalid_data(e: impl Into>) -> io::E io::Error::new(io::ErrorKind::InvalidData, e) } -#[async_trait::async_trait] #[doc(hidden)] pub trait Resolver { - async fn lookup_ip(&self, name: String) -> Result; - async fn ipv4_lookup(&self, name: String) -> Result; - async fn ipv6_lookup(&self, name: String) -> Result; - async fn txt_lookup(&self, name: String) -> Result; + fn lookup_ip( + &self, + name: String, + ) -> impl Send + Future>; + fn ipv4_lookup( + &self, + name: String, + ) -> impl Send + Future>; + fn ipv6_lookup( + &self, + name: String, + ) -> impl Send + Future>; + fn txt_lookup( + &self, + name: String, + ) -> impl Send + Future>; } -#[async_trait] impl Resolver for hickory_resolver::Resolver where C: ConnectionProvider, { - async fn lookup_ip(&self, name: String) -> Result { - self.lookup_ip(name).await + fn lookup_ip( + &self, + name: String, + ) -> impl Send + Future> { + self.lookup_ip(name) } - async fn ipv4_lookup(&self, name: String) -> Result { - self.ipv4_lookup(name).await + fn ipv4_lookup( + &self, + name: String, + ) -> impl Send + Future> { + self.ipv4_lookup(name) } - async fn ipv6_lookup(&self, name: String) -> Result { - self.ipv6_lookup(name).await + fn ipv6_lookup( + &self, + name: String, + ) -> impl Send + Future> { + self.ipv6_lookup(name) } - async fn txt_lookup(&self, name: String) -> Result { - self.txt_lookup(name).await + fn txt_lookup( + &self, + name: String, + ) -> impl Send + Future> { + self.txt_lookup(name) } }