Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dns): make {tokio,async_std}::Transport::custom infallible #4464

Merged
merged 11 commits into from
Oct 20, 2023
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)));
}
}
}