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.
Macros§
- ip
default
- generates
IpAddr
- ip4
default
- generates
Ipv4Addr
- ip6
default
- generates
Ipv6Addr
- mac
mac
- generates
MacAddr
- mac6
mac
- generates
MacAddr6
- mac8
mac
- generates
MacAddr8
- net
ipnet
- generates
IpNetwork
- net4
ipnet
- generates
Ipv4Network
- net6
ipnet
- generates
Ipv6Network
- sock
default
- generates
SocketAddr
- sock4
default
- generates
SocketAddrV4
- sock6
default
- generates
SocketAddrV6