Crate const_addrs

Source
Expand description

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

use std::net::{Ipv4Addr, IpAddr};
use const_addrs::ip;

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

Turning invalid strings into compile-time errors:

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

These macros will parse the string passed to them using its type’s FromStr implementation. See the documentation for each type for formatting details. The macro generated code will use the const constructor(s) for the types, adding no runtime overhead.

For example:

let val = sock!("192.168.1.1:500");

expands to:

let val = ::std::net::SocketAddr::V4(::std::net::SocketAddrV4::new(
    ::std::net::Ipv4Addr::new(192u8, 168u8, 1u8, 1u8),
    500u16,
));

§Features

The crate provides a set of optional features that can be enabled in your cargo.toml file.

  • default — By default, only generate stdlib types
  • ipnet — enables generation of ipnetwork types
  • mac — enables generation of macaddr types

Macros§

ipdefault
generates IpAddr
ip4default
generates Ipv4Addr
ip6default
generates Ipv6Addr
macmac
generates MacAddr
mac6mac
generates MacAddr6
mac8mac
generates MacAddr8
netipnet
generates IpNetwork
net4ipnet
generates Ipv4Network
net6ipnet
generates Ipv6Network
sockdefault
generates SocketAddr
sock4default
generates SocketAddrV4
sock6default
generates SocketAddrV6