IPv4 and IPv6 network structs.
Add this to your Cargo.toml
:
toml
[dependencies]
ip_network = "0.3"
this to your crate root (necessary just when your project is Rust 2015 edition):
rust
extern crate ip_network;
and then you can use it like this:
```rust use std::net::Ipv4Addr; use ip_network::Ipv4Network;
let ipnetwork = Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24).unwrap(); asserteq!(Ipv4Addr::new(192, 168, 1, 0), ipnetwork.networkaddress()); asserteq!(24, ipnetwork.netmask()); asserteq!(254, ipnetwork.hosts().len()); asserteq!("192.168.1.0/24", ipnetwork.to_string()); ```
Minimal required version of Rust compiler is: - 1.31 for version 0.3 and newer (because of 2018 edition), - 1.26 for version 0.2 (because of support u128 data type), - for older compiler you can use 0.1 version.
When using this crate, you can choose to compile with these features:
To enable serialization and deserialization by Serde framework,
just add serde
feature to package in your Cargo.toml
:
toml
[dependencies]
ip_network = { version = "0.3", features = ["serde"] }
To enable support for diesel [CIDR type] for PostgreSQL,
just add diesel
feature to package in your Cargo.toml
:
toml
[dependencies]
ip_network = { version = "0.3", features = ["diesel"] }
You can then use ip_network::diesel_support::PqCidrExtensionMethods
trait for CIDR operators support.
To enable support for postgres crate [CIDR type],
just add postgres
feature to package in your Cargo.toml
:
toml
[dependencies]
ip_network = { version = "0.3", features = ["postgres"] }
ipnetwork
crateSimilar functionality also provide ipnetwork crate. This table show differences between these two crates:
| Feature | ip_network | ipnetwork | |----------------------|------------|-----------| | IPv4 | ✓ | ✓ | | IPv6 | ✓ | ✓ | | IPv4 and IPv6 enum | ✓ | ✓ | | IPv4 network types | ✓ | | | IPv6 network types | ✓ | | | Hosts iterator | ✓ | ✓ | | Subnetworks iterator | ✓ | | | Check host bits set | ✓ | | | Serde | ✓ | ✓ | | Serde binary | ✓ | | | Diesel CIDR | ✓ | ✓ | | Diesel operators | ✓ | | | Postgres CIDR | ✓ | | | IPv4 string parsing | 65 ns | 379 ns | | IPv6 string parsing | 126 ns | 434 ns | | IPv4 contains method | 7 ns | 15 ns | | IPv6 contains method | 28 ns | 49 ns |