1 stable release

1.0.1 Feb 8, 2023
1.0.0 Feb 6, 2023

#2217 in Procedural macros

MIT license

10KB
152 lines

socket_addr_macros

Macros that can check and parse a SocketAddr at compile-time.

Examples

use socket_addr_macros::socket_addr;

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

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();
    }
}

lib.rs:

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();
    }
}

Dependencies

~500KB