-
Notifications
You must be signed in to change notification settings - Fork 339
Implement an async version of ToSocketAddrs #74
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2d39708
Implement an async version of ToSocketAddrs
DCjanus 4ad633e
fix documentation issue
DCjanus 7cba6c4
genius hack: pretending to be `impl Future`
DCjanus 8baf8df
replace `std::net::ToSocketAddrs` with `async-std::net::ToSocketAddrs`
DCjanus e1b4935
Move unit tests into the tests directory
11cdff7
Stylistic changes
815271b
Remove re-exports in async_std::net
dc4d7bd
fix broken link
DCjanus bfdf6ff
some mirror changes
DCjanus 242cc9f
remove unnecessary format
DCjanus 63c2c75
migrate: `std::net::ToSocketAddrs` -> `async_std::net::ToSocketAddrs`
DCjanus ff40561
fix typo(tutorial)
DCjanus d2c61b6
remove unnecessary type bound
DCjanus 6db0085
lifetime for future
DCjanus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; | ||
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6}; | ||
use std::pin::Pin; | ||
|
||
use cfg_if::cfg_if; | ||
use futures::future::{ready, Ready}; | ||
|
||
use crate::future::Future; | ||
use crate::io; | ||
use crate::task::blocking; | ||
use crate::task::{Context, Poll}; | ||
use std::marker::PhantomData; | ||
|
||
cfg_if! { | ||
if #[cfg(feature = "docs")] { | ||
#[doc(hidden)] | ||
pub struct ImplFuture<'a, T>(std::marker::PhantomData<&'a T>); | ||
|
||
macro_rules! ret { | ||
($a:lifetime, $f:tt, $i:ty) => (ImplFuture<$a, io::Result<$i>>); | ||
} | ||
} else { | ||
macro_rules! ret { | ||
($a:lifetime, $f:tt, $i:ty) => ($f<$a, $i>); | ||
} | ||
} | ||
} | ||
|
||
/// A trait for objects which can be converted or resolved to one or more [`SocketAddr`] values. | ||
/// | ||
/// This trait is an async version of [`std::net::ToSocketAddrs`]. | ||
/// | ||
/// [`std::net::ToSocketAddrs`]: https://doc.rust-lang.org/std/net/trait.ToSocketAddrs.html | ||
/// [`SocketAddr`]: https://doc.rust-lang.org/std/net/enum.SocketAddr.html | ||
pub trait ToSocketAddrs { | ||
/// Returned iterator over socket addresses which this type may correspond to. | ||
type Iter: Iterator<Item = SocketAddr>; | ||
|
||
/// Converts this object to an iterator of resolved `SocketAddr`s. | ||
/// | ||
/// The returned iterator may not actually yield any values depending on the outcome of any | ||
/// resolution performed. | ||
/// | ||
/// Note that this function may block a backend thread while resolution is performed. | ||
fn to_socket_addrs(&self) -> ret!('_, ToSocketAddrsFuture, Self::Iter); | ||
} | ||
|
||
#[doc(hidden)] | ||
#[allow(missing_debug_implementations)] | ||
pub enum ToSocketAddrsFuture<'a, I: Iterator<Item = SocketAddr>> { | ||
Phantom(PhantomData<&'a ()>), | ||
Join(blocking::JoinHandle<io::Result<I>>), | ||
Ready(Ready<io::Result<I>>), | ||
} | ||
|
||
impl<I: Iterator<Item = SocketAddr>> Future for ToSocketAddrsFuture<'_, I> { | ||
type Output = io::Result<I>; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
DCjanus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
match self.get_mut() { | ||
ToSocketAddrsFuture::Join(f) => Pin::new(&mut *f).poll(cx), | ||
ToSocketAddrsFuture::Ready(f) => Pin::new(&mut *f).poll(cx), | ||
_ => unreachable!(), | ||
} | ||
} | ||
} | ||
|
||
impl ToSocketAddrs for SocketAddr { | ||
type Iter = std::option::IntoIter<SocketAddr>; | ||
|
||
fn to_socket_addrs(&self) -> ret!('_, ToSocketAddrsFuture, Self::Iter) { | ||
ToSocketAddrsFuture::Ready(ready(std::net::ToSocketAddrs::to_socket_addrs(self))) | ||
} | ||
} | ||
|
||
impl ToSocketAddrs for SocketAddrV4 { | ||
type Iter = std::option::IntoIter<SocketAddr>; | ||
|
||
fn to_socket_addrs(&self) -> ret!('_, ToSocketAddrsFuture, Self::Iter) { | ||
ToSocketAddrsFuture::Ready(ready(std::net::ToSocketAddrs::to_socket_addrs(self))) | ||
} | ||
} | ||
|
||
impl ToSocketAddrs for SocketAddrV6 { | ||
type Iter = std::option::IntoIter<SocketAddr>; | ||
|
||
fn to_socket_addrs(&self) -> ret!('_, ToSocketAddrsFuture, Self::Iter) { | ||
ToSocketAddrsFuture::Ready(ready(std::net::ToSocketAddrs::to_socket_addrs(self))) | ||
} | ||
} | ||
|
||
impl ToSocketAddrs for (IpAddr, u16) { | ||
type Iter = std::option::IntoIter<SocketAddr>; | ||
|
||
fn to_socket_addrs(&self) -> ret!('_, ToSocketAddrsFuture, Self::Iter) { | ||
ToSocketAddrsFuture::Ready(ready(std::net::ToSocketAddrs::to_socket_addrs(self))) | ||
} | ||
} | ||
|
||
impl ToSocketAddrs for (Ipv4Addr, u16) { | ||
type Iter = std::option::IntoIter<SocketAddr>; | ||
|
||
fn to_socket_addrs(&self) -> ret!('_, ToSocketAddrsFuture, Self::Iter) { | ||
ToSocketAddrsFuture::Ready(ready(std::net::ToSocketAddrs::to_socket_addrs(self))) | ||
} | ||
} | ||
|
||
impl ToSocketAddrs for (Ipv6Addr, u16) { | ||
type Iter = std::option::IntoIter<SocketAddr>; | ||
|
||
fn to_socket_addrs(&self) -> ret!('_, ToSocketAddrsFuture, Self::Iter) { | ||
ToSocketAddrsFuture::Ready(ready(std::net::ToSocketAddrs::to_socket_addrs(self))) | ||
} | ||
} | ||
|
||
impl ToSocketAddrs for (&str, u16) { | ||
type Iter = std::vec::IntoIter<SocketAddr>; | ||
|
||
fn to_socket_addrs(&self) -> ret!('_, ToSocketAddrsFuture, Self::Iter) { | ||
let host = self.0.to_string(); | ||
let port = self.1; | ||
let join = blocking::spawn(async move { | ||
std::net::ToSocketAddrs::to_socket_addrs(&(host.as_str(), port)) | ||
}); | ||
ToSocketAddrsFuture::Join(join) | ||
} | ||
} | ||
|
||
impl ToSocketAddrs for str { | ||
type Iter = std::vec::IntoIter<SocketAddr>; | ||
|
||
fn to_socket_addrs(&self) -> ret!('_, ToSocketAddrsFuture, Self::Iter) { | ||
let socket_addrs = self.to_string(); | ||
let join = | ||
blocking::spawn(async move { std::net::ToSocketAddrs::to_socket_addrs(&socket_addrs) }); | ||
ToSocketAddrsFuture::Join(join) | ||
} | ||
} | ||
|
||
impl<'a> ToSocketAddrs for &'a [SocketAddr] { | ||
type Iter = std::iter::Cloned<std::slice::Iter<'a, SocketAddr>>; | ||
|
||
fn to_socket_addrs(&self) -> ret!('_, ToSocketAddrsFuture, Self::Iter) { | ||
ToSocketAddrsFuture::Ready(ready(std::net::ToSocketAddrs::to_socket_addrs(self))) | ||
} | ||
} | ||
|
||
impl<T: ToSocketAddrs + ?Sized> ToSocketAddrs for &T { | ||
type Iter = T::Iter; | ||
|
||
fn to_socket_addrs(&self) -> ret!('_, ToSocketAddrsFuture, Self::Iter) { | ||
(**self).to_socket_addrs() | ||
} | ||
} | ||
|
||
impl ToSocketAddrs for String { | ||
type Iter = std::vec::IntoIter<SocketAddr>; | ||
|
||
fn to_socket_addrs(&self) -> ret!('_, ToSocketAddrsFuture, Self::Iter) { | ||
ToSocketAddrs::to_socket_addrs(self.as_str()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.