Skip to content

Commit

Permalink
feat(dns): make {tokio,async_std}::Transport::custom infallible
Browse files Browse the repository at this point in the history
Resolves: #4462.

Pull-Request: #4464.
  • Loading branch information
p0mvn authored Oct 20, 2023
1 parent a73b5d9 commit 781405b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
4 changes: 4 additions & 0 deletions transports/dns/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 0.41.0 - unreleased

- Make `tokio::Transport::custom` and `async_std::Transport::custom` constructors infallible.
See [PR 4464].

[PR 4464]: https://github.com/libp2p/rust-libp2p/pull/4464

## 0.40.1

Expand Down
30 changes: 11 additions & 19 deletions transports/dns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,15 @@ pub mod async_std {
/// Creates a new [`Transport`] from the OS's DNS configuration and defaults.
pub async fn system(inner: T) -> Result<Transport<T>, io::Error> {
let (cfg, opts) = system_conf::read_system_conf()?;
Self::custom(inner, cfg, opts).await
Ok(Self::custom(inner, cfg, opts).await)
}

/// Creates a [`Transport`] with a custom resolver configuration and options.
pub async fn custom(
inner: T,
cfg: ResolverConfig,
opts: ResolverOpts,
) -> Result<Transport<T>, io::Error> {
Ok(Transport {
pub async fn custom(inner: T, cfg: ResolverConfig, opts: ResolverOpts) -> Transport<T> {
Transport {
inner: Arc::new(Mutex::new(inner)),
resolver: async_std_resolver::resolver(cfg, opts).await,
})
}
}
}
}
Expand All @@ -108,9 +104,9 @@ pub mod tokio {

impl<T> Transport<T> {
/// Creates a new [`Transport`] from the OS's DNS configuration and defaults.
pub fn system(inner: T) -> Result<crate::Transport<T, TokioAsyncResolver>, std::io::Error> {
pub fn system(inner: T) -> Result<Transport<T>, std::io::Error> {
let (cfg, opts) = system_conf::read_system_conf()?;
Self::custom(inner, cfg, opts)
Ok(Self::custom(inner, cfg, opts))
}

/// Creates a [`Transport`] with a custom resolver configuration
Expand All @@ -119,12 +115,11 @@ pub mod tokio {
inner: T,
cfg: trust_dns_resolver::config::ResolverConfig,
opts: trust_dns_resolver::config::ResolverOpts,
) -> Result<crate::Transport<T, TokioAsyncResolver>, std::io::Error> {
// TODO: Make infallible in next breaking release. Or deprecation?
Ok(Transport {
) -> Transport<T> {
Transport {
inner: Arc::new(Mutex::new(inner)),
resolver: TokioAsyncResolver::tokio(cfg, opts),
})
}
}
}
}
Expand Down Expand Up @@ -764,8 +759,7 @@ mod tests {
let config = ResolverConfig::quad9();
let opts = ResolverOpts::default();
async_std_crate::task::block_on(
async_std::Transport::custom(CustomTransport, config, opts)
.then(|dns| run(dns.unwrap())),
async_std::Transport::custom(CustomTransport, config, opts).then(run),
);
}

Expand All @@ -781,9 +775,7 @@ mod tests {
.build()
.unwrap();

rt.block_on(run(
tokio::Transport::custom(CustomTransport, config, opts).unwrap()
));
rt.block_on(run(tokio::Transport::custom(CustomTransport, config, opts)));
}
}
}

0 comments on commit 781405b

Please sign in to comment.