1 stable release

1.0.1 Feb 8, 2023
1.0.0 Feb 6, 2023

#1128 in Procedural macros

Download history 29/week @ 2024-02-25 2/week @ 2024-03-03 5/week @ 2024-03-10 47/week @ 2024-03-17 1/week @ 2024-03-24 21/week @ 2024-03-31

74 downloads per month

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

~485KB