3 unstable releases

0.2.0 May 9, 2021
0.1.2 Jan 15, 2018
0.1.1 Jan 15, 2018
0.1.0 Oct 18, 2017

#537 in Procedural macros

Download history 31/week @ 2024-07-27 32/week @ 2024-08-03 31/week @ 2024-08-10 53/week @ 2024-08-17 38/week @ 2024-08-24 47/week @ 2024-08-31 42/week @ 2024-09-07 37/week @ 2024-09-14 64/week @ 2024-09-21 38/week @ 2024-09-28 11/week @ 2024-10-05 17/week @ 2024-10-12 15/week @ 2024-10-19 30/week @ 2024-10-26 23/week @ 2024-11-02 5/week @ 2024-11-09

75 downloads per month
Used in 7 crates (6 directly)

LGPL-2.0

8KB
163 lines

net-literals: Rust macros for writing IP/socket address literals.

This crate allows you to write IP and socket addresses as strings which are parsed (and checked for validity) at compile time. It makes use of the proc-macro hack and is compatible with stable rust. Inspired by the maplit crate.

Example:

#[macro_use]
extern crate net_literals;

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
use std::str::FromStr;

fn main() {
    assert_eq!(
        ip!("1.2.3.4"),
        IpAddr::from_str("1.2.3.4").unwrap()
    );

    assert_eq!(
        ipv4!("1.2.3.4"),
        Ipv4Addr::from_str("1.2.3.4").unwrap()
    );

    assert_eq!(
        ipv6!("0011:2233:4455:6677:8899:aabb:ccdd:eeff"),
        Ipv6Addr::from_str("0011:2233:4455:6677:8899:aabb:ccdd:eeff").unwrap()
    );

    assert_eq!(
        addr!("2.3.4.5:666"),
        SocketAddr::from_str("2.3.4.5:666").unwrap()
    );

    assert_eq!(
        addrv4!("2.3.4.5:666"),
        SocketAddrV4::from_str("2.3.4.5:666").unwrap()
    );

    assert_eq!(
        addrv6!("[::2345]:666"),
        SocketAddrV6::from_str("[::2345]:666").unwrap()
    );
}

Dependencies

~1.5MB
~37K SLoC