Skip to content

sophacles/const-addrs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

const-addrs Crates.io License Documentation

Build Status: Build Status Formatting Status Linting Status

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!
ipnet::IpNet ipnet!
ipnet::Ipv4Net ipnet4!
ipnet::Ipv6Net ipnet6!
ipnetwork::IpNetwork net!
ipnetwork::Ipv4Network net4!
ipnetwork::Ipv6Network net6!
macaddr::MacAddr mac!
macaddr::MacAddr6 mac6!
macaddr::MacAddr8 mac8!

Note: using ipnet::* types requires you to have the ipnet crate in your depdencies. These types can be enabled with the ipnet feature.

Note: using ipnetwork::* types requires you to have the ipnetwork crate in your depdencies. These types can be enabled with the ipnetwork feature.

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.

About

Macros to make working with network types a little easer

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •