Crates.io | const-addrs |
lib.rs | const-addrs |
version | 0.1.1 |
source | src |
created_at | 2024-02-18 06:11:49.329626 |
updated_at | 2024-02-18 06:18:31.352898 |
description | A set of macros for creating networking types from a string literal. |
homepage | |
repository | https://github.com/sophacles/const-addrs |
max_upload_size | |
id | 1143831 |
size | 22,790 |
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.