JakubOnderka/ip_network

Rust crate for IPv4 and IPv6 network structs.

Rust

26

175 commits

updated Jul 16, 2024

See the code

README

ip_network

IPv4 and IPv6 network structs.

Documentation Build Status Coverage Status Crates.io

Usage

Add this to your Cargo.toml:

[dependencies]
ip_network = "0.4"

this to your crate root (not necessary when your project is Rust 2018 edition):

extern crate ip_network;

and then you can use it like this:

use std::net::Ipv4Addr;
use ip_network::Ipv4Network;

let ip_network = Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert_eq!(Ipv4Addr::new(192, 168, 1, 0), ip_network.network_address());
assert_eq!(24, ip_network.netmask());
assert_eq!(254, ip_network.hosts().len());
assert_eq!("192.168.1.0/24", ip_network.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.

Optional features

When using this crate, you can choose to compile with these features:

Serde support

To enable serialization and deserialization by Serde framework, just add serde feature to package in your Cargo.toml:

[dependencies]
ip_network = { version = "0.4", features = ["serde"] }

Diesel support

To enable support for diesel CIDR type for PostgreSQL, just add diesel feature to package in your Cargo.toml:

[dependencies]
ip_network = { version = "0.4", features = ["diesel"] }

You can then use ip_network::diesel_support::PqCidrExtensionMethods trait for CIDR operators support.

Postgres support

To enable support for postgres crate CIDR type, just add postgres feature to package in your Cargo.toml:

[dependencies]
ip_network = { version = "0.4", features = ["postgres"] }

Comparison with ipnetwork crate

Similar functionality also provides ipnetwork crate. This table shows the differences between these two crates:

Featureip_networkipnetwork
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 parsing65 ns379 ns
IPv6 string parsing126 ns434 ns
IPv4 contains method7 ns15 ns
IPv6 contains method28 ns49 ns
address
ip
ipv4
ipv6
network
rust

JakubOnderka/ip_network

Rust crate for IPv4 and IPv6 network structs.

Rust

26

175 commits

updated Jul 16, 2024

See the code

README

ip_network

IPv4 and IPv6 network structs.

Documentation Build Status Coverage Status Crates.io

Usage

Add this to your Cargo.toml:

[dependencies]
ip_network = "0.4"

this to your crate root (not necessary when your project is Rust 2018 edition):

extern crate ip_network;

and then you can use it like this:

use std::net::Ipv4Addr;
use ip_network::Ipv4Network;

let ip_network = Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert_eq!(Ipv4Addr::new(192, 168, 1, 0), ip_network.network_address());
assert_eq!(24, ip_network.netmask());
assert_eq!(254, ip_network.hosts().len());
assert_eq!("192.168.1.0/24", ip_network.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.

Optional features

When using this crate, you can choose to compile with these features:

Serde support

To enable serialization and deserialization by Serde framework, just add serde feature to package in your Cargo.toml:

[dependencies]
ip_network = { version = "0.4", features = ["serde"] }

Diesel support

To enable support for diesel CIDR type for PostgreSQL, just add diesel feature to package in your Cargo.toml:

[dependencies]
ip_network = { version = "0.4", features = ["diesel"] }

You can then use ip_network::diesel_support::PqCidrExtensionMethods trait for CIDR operators support.

Postgres support

To enable support for postgres crate CIDR type, just add postgres feature to package in your Cargo.toml:

[dependencies]
ip_network = { version = "0.4", features = ["postgres"] }

Comparison with ipnetwork crate

Similar functionality also provides ipnetwork crate. This table shows the differences between these two crates:

Featureip_networkipnetwork
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 parsing65 ns379 ns
IPv6 string parsing126 ns434 ns
IPv4 contains method7 ns15 ns
IPv6 contains method28 ns49 ns
address
ip
ipv4
ipv6
network
rust

Project health

Overall

2.0/10

Maintained

0/10

Code review

0/10
OpenSSF Scorecard

crates.io

Latest release

0.4.1 · Nov 12, 2021

Releases this year

0