#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

#608 in Procedural macros

Download history 1/week @ 2024-07-27 61/week @ 2024-08-10 68/week @ 2024-08-17 74/week @ 2024-08-24 55/week @ 2024-08-31 139/week @ 2024-09-07 46/week @ 2024-09-14 64/week @ 2024-09-21 140/week @ 2024-09-28 125/week @ 2024-10-05 47/week @ 2024-10-12 75/week @ 2024-10-19 84/week @ 2024-10-26 70/week @ 2024-11-02 87/week @ 2024-11-09

328 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.8MB
~19K SLoC