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

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

#298 in Procedural macros

Download history 221/week @ 2024-02-16 40/week @ 2024-02-23 8/week @ 2024-03-01 24/week @ 2024-03-08 27/week @ 2024-03-15 8/week @ 2024-03-22 48/week @ 2024-03-29 36/week @ 2024-04-05

127 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