From b54629e28409e97fff18c3b12859705da3ec75d6 Mon Sep 17 00:00:00 2001 From: Abner Zheng Date: Fri, 23 Feb 2024 11:32:46 +0800 Subject: [PATCH 1/3] replace `#[async_trait]` with native async trait for trait `Resolver` --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + crates/net/dns/Cargo.toml | 1 + crates/net/dns/src/resolver.rs | 9 ++------- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 17f9c318fe3f..68e8d5dae293 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6007,6 +6007,7 @@ dependencies = [ "tokio", "tokio-stream", "tracing", + "trait-variant", "trust-dns-resolver", ] @@ -8726,6 +8727,17 @@ dependencies = [ "tracing-serde", ] +[[package]] +name = "trait-variant" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45e1a477061e97925d81a2f89fb73b2b8038e6baa5a0023bad380ac23b5f4fa6" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.49", +] + [[package]] name = "triehash" version = "0.8.4" diff --git a/Cargo.toml b/Cargo.toml index 0bbdc48802dd..a09d204f08af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -239,6 +239,7 @@ async-trait = "0.1.68" futures = "0.3.26" pin-project = "1.0.12" futures-util = "0.3.25" +trait-variant = "0.1.1" # rpc jsonrpsee = { version = "0.20" } diff --git a/crates/net/dns/Cargo.toml b/crates/net/dns/Cargo.toml index 6c64cfee9953..c78fafd2804a 100644 --- a/crates/net/dns/Cargo.toml +++ b/crates/net/dns/Cargo.toml @@ -32,6 +32,7 @@ trust-dns-resolver = "0.23" # misc data-encoding = "2" async-trait.workspace = true +trait-variant.workspace = true linked_hash_set = "0.1" schnellru.workspace = true thiserror.workspace = true diff --git a/crates/net/dns/src/resolver.rs b/crates/net/dns/src/resolver.rs index 34ff0d9383c3..d5949ed7f940 100644 --- a/crates/net/dns/src/resolver.rs +++ b/crates/net/dns/src/resolver.rs @@ -1,6 +1,5 @@ //! Perform DNS lookups -use async_trait::async_trait; use parking_lot::RwLock; use std::collections::HashMap; use tracing::trace; @@ -8,13 +7,12 @@ pub use trust_dns_resolver::{error::ResolveError, TokioAsyncResolver}; use trust_dns_resolver::{name_server::ConnectionProvider, AsyncResolver}; /// A type that can lookup DNS entries -#[async_trait] -pub trait Resolver: Send + Sync + Unpin + 'static { +#[trait_variant::make(Resolver: Send)] +pub trait LocalResolver: Send + Sync + Unpin + 'static { /// Performs a textual lookup and returns the first text async fn lookup_txt(&self, query: &str) -> Option; } -#[async_trait] impl Resolver for AsyncResolver

{ async fn lookup_txt(&self, query: &str) -> Option { // See: [AsyncResolver::txt_lookup] @@ -67,7 +65,6 @@ impl DnsResolver { } } -#[async_trait] impl Resolver for DnsResolver { async fn lookup_txt(&self, query: &str) -> Option { Resolver::lookup_txt(&self.0, query).await @@ -98,7 +95,6 @@ impl MapResolver { } } -#[async_trait] impl Resolver for MapResolver { async fn lookup_txt(&self, query: &str) -> Option { self.get(query) @@ -110,7 +106,6 @@ impl Resolver for MapResolver { pub(crate) struct TimeoutResolver(pub(crate) std::time::Duration); #[cfg(test)] -#[async_trait] impl Resolver for TimeoutResolver { async fn lookup_txt(&self, _query: &str) -> Option { tokio::time::sleep(self.0).await; From 245924ffb5cdf2d8c775a76fe3f17e15bfbe69fb Mon Sep 17 00:00:00 2001 From: Abner Zheng Date: Fri, 23 Feb 2024 17:50:20 +0800 Subject: [PATCH 2/3] remove 'trait_variant' and use '->impl trait' instead --- Cargo.lock | 12 ------------ Cargo.toml | 1 - crates/net/dns/Cargo.toml | 1 - crates/net/dns/src/resolver.rs | 6 +++--- 4 files changed, 3 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 68e8d5dae293..17f9c318fe3f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6007,7 +6007,6 @@ dependencies = [ "tokio", "tokio-stream", "tracing", - "trait-variant", "trust-dns-resolver", ] @@ -8727,17 +8726,6 @@ dependencies = [ "tracing-serde", ] -[[package]] -name = "trait-variant" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45e1a477061e97925d81a2f89fb73b2b8038e6baa5a0023bad380ac23b5f4fa6" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.49", -] - [[package]] name = "triehash" version = "0.8.4" diff --git a/Cargo.toml b/Cargo.toml index a09d204f08af..0bbdc48802dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -239,7 +239,6 @@ async-trait = "0.1.68" futures = "0.3.26" pin-project = "1.0.12" futures-util = "0.3.25" -trait-variant = "0.1.1" # rpc jsonrpsee = { version = "0.20" } diff --git a/crates/net/dns/Cargo.toml b/crates/net/dns/Cargo.toml index c78fafd2804a..6c64cfee9953 100644 --- a/crates/net/dns/Cargo.toml +++ b/crates/net/dns/Cargo.toml @@ -32,7 +32,6 @@ trust-dns-resolver = "0.23" # misc data-encoding = "2" async-trait.workspace = true -trait-variant.workspace = true linked_hash_set = "0.1" schnellru.workspace = true thiserror.workspace = true diff --git a/crates/net/dns/src/resolver.rs b/crates/net/dns/src/resolver.rs index d5949ed7f940..5839494df1af 100644 --- a/crates/net/dns/src/resolver.rs +++ b/crates/net/dns/src/resolver.rs @@ -2,15 +2,15 @@ use parking_lot::RwLock; use std::collections::HashMap; +use std::future::Future; use tracing::trace; pub use trust_dns_resolver::{error::ResolveError, TokioAsyncResolver}; use trust_dns_resolver::{name_server::ConnectionProvider, AsyncResolver}; /// A type that can lookup DNS entries -#[trait_variant::make(Resolver: Send)] -pub trait LocalResolver: Send + Sync + Unpin + 'static { +pub trait Resolver: Send + Sync + Unpin + 'static { /// Performs a textual lookup and returns the first text - async fn lookup_txt(&self, query: &str) -> Option; + fn lookup_txt(&self, query: &str) -> impl Future> + Send; } impl Resolver for AsyncResolver

{ From 68ba5785d0d10768928df1a1ada8fb754c8f4340 Mon Sep 17 00:00:00 2001 From: Abner Zheng Date: Fri, 23 Feb 2024 19:04:31 +0800 Subject: [PATCH 3/3] clippy --- crates/net/dns/src/resolver.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/net/dns/src/resolver.rs b/crates/net/dns/src/resolver.rs index 5839494df1af..6bf8ce4f230f 100644 --- a/crates/net/dns/src/resolver.rs +++ b/crates/net/dns/src/resolver.rs @@ -1,8 +1,7 @@ //! Perform DNS lookups use parking_lot::RwLock; -use std::collections::HashMap; -use std::future::Future; +use std::{collections::HashMap, future::Future}; use tracing::trace; pub use trust_dns_resolver::{error::ResolveError, TokioAsyncResolver}; use trust_dns_resolver::{name_server::ConnectionProvider, AsyncResolver};