Expand description

The Rust Standard Library documentation suggests that, for an argument of type ToSocketAddrs you should directly pass a string literal, which is parsed at runtime and thus potentially causing a panic. To solve this, socket_addr! and socket_addr_dyn! macros do the parsing at compile-time and when the address is invalid, cause a compile error.

Example

use socket_addr_macros::socket_addr;

use std::io::Write;
use std::net::TcpListener;

fn main() {
    let listener = TcpListener::bind(socket_addr!(127.0.0.1:8080)).unwrap();

    while let Ok((mut conn, _)) = listener.accept() {
        conn.write(b"hello").unwrap();
    }
}

Macros

Parses an IPv4 or IPv6 address at compile-time and returns a SocketAddr.
Parses an IPv4 or IPv6 address at compile-time and returns either a SocketAddrV4 or a SocketAddrV6 depending on the input.