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

To implement ToSocketAddrs for a custom type the ToSocketAddrsFuture should be pub use #539

Open
honsunrise opened this issue Nov 15, 2019 · 7 comments · May be fixed by #1028
Open

To implement ToSocketAddrs for a custom type the ToSocketAddrsFuture should be pub use #539

honsunrise opened this issue Nov 15, 2019 · 7 comments · May be fixed by #1028
Labels
api design Open design questions

Comments

@honsunrise
Copy link

No description provided.

@yoshuawuyts
Copy link
Contributor

Ref https://docs.rs/async-std/1.0.1/async_std/net/trait.ToSocketAddrs.html. From a glance it seems this is indeed accurate. Since the result type from ToSocketAddrs is opaque, we could probably change it to be similar to FromStream::from_stream without breaking anything.

@bandheight out of curiosity: what's your use case for implementing ToSocketAddrs?

@ghost
Copy link

ghost commented Nov 21, 2019

I'd be okay with changing the return type of to_socket_addrs to Pin<Box<dyn Future>> here. Performance impact of the change would be effectively none.

@honsunrise
Copy link
Author

@yoshuawuyts I implemented an enum type to represent multiple address types.

pub enum Address {
    /// IP Address
    SocketAddr(SocketAddr),
    /// Domain name address, eg. example.com:8080
    Domain(String, u16),
}

If this type implement the trait ToSocketAddrs, it will be passed directly to TcpListener::bind and other similar functions.

@yoshuawuyts
Copy link
Contributor

@bandheight does the following work for you?

async fn bind_socket(addr: Address) -> io::Result<TcpListener> {
    match addr {
        SocketAddr(addr) => TcpListener::bind(addr).await?,
        SocketAddr((addr, port) => TcpListener::bind((&*addr, port)).await?,
    }
}

If we don't already support ToSocketAddr for (String, u16) then we should add that!

@honsunrise
Copy link
Author

@yoshuawuyts This code is worked.
And it is also my current solution, but I want to make my code look more neat and elegant.
So implement the trait ToSocketAddrs is probably the best solution.

pub struct DomainName(pub String, pub u16);

...
// implement Deserialize for DomainName
...

#[derive(Deserialize)]
pub enum Address {
    /// IP Address
    SocketAddr(SocketAddr),
    /// Domain name address, eg. example.com:8080
    Domain(DomainName),
}

#[derive(Deserialize)]
pub struct Config {
    pub addr: Address,
}

...
let config: Config = <deserialize from config file>;
...

// This is neat and elegant.
let listener = TcpListener::bind(config.addr);

I vote for it that support ToSocketAddr for (String, u16).

@dizda
Copy link

dizda commented Jan 17, 2020

@dizda
Copy link

dizda commented Jan 17, 2020

Shadowsocks does it for a similar purpose, but with std::net::ToSocketAddrs.

cf. https://github.com/shadowsocks/shadowsocks-rust/blob/f4feba9fc1c8a968d90a592c5f4e86ffc6dd322b/src/relay/socks5.rs#L332

@eof228 eof228 linked a pull request Jun 19, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api design Open design questions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants