Skip to content

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

Open
@honsunrise

Description

@honsunrise
No description provided.

Activity

yoshuawuyts

yoshuawuyts commented on Nov 19, 2019

@yoshuawuyts
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

ghost commented on Nov 21, 2019

@ghost

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

honsunrise commented on Nov 22, 2019

@honsunrise
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

yoshuawuyts commented on Nov 25, 2019

@yoshuawuyts
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

honsunrise commented on Nov 26, 2019

@honsunrise
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

dizda commented on Jan 17, 2020

@dizda

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

linked a pull request that will close this issue on Jun 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api designOpen design questions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @dizda@yoshuawuyts@honsunrise

      Issue actions

        To implement ToSocketAddrs for a custom type the ToSocketAddrsFuture should be pub use · Issue #539 · async-rs/async-std