#string-literal #macro #literals #mac-address #networking #ip-address #parser

macro const-addrs

A set of macros for creating networking types from a string literal

2 releases

0.1.1 Feb 18, 2024
0.1.0 Feb 18, 2024

#451 in Procedural macros

Download history 52/week @ 2024-06-06 74/week @ 2024-06-13 60/week @ 2024-06-20 20/week @ 2024-06-27 4/week @ 2024-07-04 2/week @ 2024-07-11 1/week @ 2024-07-25 30/week @ 2024-08-08 86/week @ 2024-08-15 56/week @ 2024-08-22 55/week @ 2024-08-29 131/week @ 2024-09-05 61/week @ 2024-09-12 47/week @ 2024-09-19

305 downloads per month

MIT license

15KB
208 lines

const-addrs

A set of macros for creating networking types from a string literal.

Each of the macros will parse using the FromStr implementation for the appropriate type. The generated code will use a const constructor for the type, if one exists.

use std::net::Ipv4Addr;
use const_addrs::ip4;

let a = ip4!("192.168.1.1");
let b = Ipv4Addr::new(192,168,1,1);
assert_eq!(a, b);

And turns invalid strings into compile-time errors:

error: invalid IPv4 address syntax
  --> bad.rs:10:18
   |
10 |     let a = ip4!("192.1681.1");
   |                  ^^^^^^^^^^^^

There are macros for:

Type macro
std::net::IpAddr ip!
std::net::Ipv4Addr ip4!
std::net::Ipv6Addr ip6!
std::net::SocketAddr sock!
std::net::SocketAddrV4 sock4!
std::net::SocketAddrV6 sock6!
ipnetwork::IpNetwork net!
ipnetwork::Ipv4Network net4!
ipnetwork::Ipv6Network net6!
macaddr::MacAddr mac!
macaddr::MacAddr6 mac6!
macaddr::MacAddr8 mac8!

Note: using ipnetwork::* types requires you to have the ipnetwork crate in your depdencies. These types can be enabled with the ipnet feature. default)

Note: using macaddr::* requires the macaddr crate in your depdencies. These types can be enabled with the mac feature.

When possible the expanded macro uses const constructors, allowing for simple string representations of network types without the cost of runtime parsing.

Dependencies

~0.3–0.9MB
~20K SLoC