Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ before_script: |

script:
- cargo fmt -- --check
- cargo test --no-default-features
- cargo test
- cargo test --features serde
- cargo test --features postgres
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ version = "1.0"
[dev-dependencies.criterion]
version = "0.3.4"

[features]
default = [ "std" ]
alloc = []
std = ["alloc"]

[package.metadata.docs.rs]
features = ["serde", "postgres", "diesel"]

[[bench]]
name = "benchmark"
harness = false
required-features = ["std"]
25 changes: 17 additions & 8 deletions src/ip_network.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::cmp::Ordering;
use std::fmt;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::str::FromStr;
use crate::{IpNetworkError, IpNetworkParseError};
use crate::helpers;
use crate::{IpNetworkError, IpNetworkParseError};
use crate::{Ipv4Network, Ipv6Network};
#[cfg(feature = "alloc")]
use alloc::vec;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::fmt;
use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use core::str::FromStr;

/// Holds IPv4 or IPv6 network.
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, PartialOrd, Ord)]
Expand Down Expand Up @@ -206,6 +210,7 @@ impl IpNetwork {
}

/// Return an iterator of the collapsed IpNetworks.
#[cfg(feature = "std")]
pub fn collapse_addresses(addresses: &[Self]) -> Vec<Self> {
let mut ipv4_networks = vec![];
let mut ipv6_networks = vec![];
Expand Down Expand Up @@ -392,9 +397,12 @@ impl PartialOrd<IpNetwork> for Ipv6Network {

#[cfg(test)]
mod tests {
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use crate::{IpNetwork, IpNetworkParseError, IpNetworkError, Ipv4Network, Ipv6Network};
use std::str::FromStr;
extern crate std;
extern crate alloc;

use crate::{IpNetwork, IpNetworkError, IpNetworkParseError, Ipv4Network, Ipv6Network};
use alloc::string::ToString;
use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};

fn return_test_ipv4_network() -> Ipv4Network {
Ipv4Network::new(Ipv4Addr::new(192, 168, 0, 0), 16).unwrap()
Expand Down Expand Up @@ -607,6 +615,7 @@ mod tests {
assert!(ip_network_v6 > ip_network_v4);
}

#[cfg(feature = "std")]
#[test]
fn collapse_addresses() {
let addresses: Vec<_> = [
Expand Down
47 changes: 39 additions & 8 deletions src/ipv4_network.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use std::cmp;
use std::fmt;
use std::net::Ipv4Addr;
use std::str::FromStr;
use std::hash::{Hash, Hasher};
#[cfg(feature = "alloc")]
use alloc::vec;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::cmp;
use core::fmt;
use core::net::Ipv4Addr;
use core::str::FromStr;
use core::hash::{Hash, Hasher};
use crate::{IpNetworkError, IpNetworkParseError};
use crate::helpers;
use crate::iterator;
#[cfg(feature = "std")]
use std::collections::HashMap;
#[cfg(feature = "std")]
use std::collections::hash_map::Entry;

/// IPv4 Network.
Expand Down Expand Up @@ -607,6 +613,8 @@ impl Ipv4Network {
/// # Examples
///
/// ```
/// #[cfg(feature = "alloc")]
/// {
/// use std::net::Ipv4Addr;
/// use ip_network::Ipv4Network;
///
Expand All @@ -616,13 +624,15 @@ impl Ipv4Network {
/// );
///
/// assert_eq!(Ipv4Network::new(Ipv4Addr::new(10, 254, 0, 0), 15)?, ranges[0]);
/// }
/// # Ok::<(), ip_network::IpNetworkError>(())
/// ```
#[cfg(feature = "alloc")]
pub fn summarize_address_range(first: Ipv4Addr, last: Ipv4Addr) -> Vec<Self> {
let mut first_int = u32::from(first);
let last_int = u32::from(last);

let mut vector = vec![];
let mut vector = Vec::new();

while first_int <= last_int {
let bit_length_diff = if last_int - first_int == u32::MAX {
Expand Down Expand Up @@ -660,6 +670,8 @@ impl Ipv4Network {
/// # Examples
///
/// ```
/// #[cfg(feature = "std")]
/// {
/// use std::net::Ipv4Addr;
/// use ip_network::Ipv4Network;
///
Expand All @@ -669,8 +681,10 @@ impl Ipv4Network {
/// ]);
///
/// assert_eq!(Ipv4Network::new(Ipv4Addr::new(192, 0, 2, 0), 24)?, collapsed[0]);
/// }
/// # Ok::<(), ip_network::IpNetworkError>(())
/// ```
#[cfg(feature = "std")]
pub fn collapse_addresses(addresses: &[Self]) -> Vec<Self> {
let mut subnets = HashMap::new();

Expand Down Expand Up @@ -830,9 +844,13 @@ impl IntoIterator for Ipv4Network {

#[cfg(test)]
mod tests {
use std::net::Ipv4Addr;
extern crate std;
extern crate alloc;

use alloc::string::ToString;
use core::net::Ipv4Addr;
use crate::{IpNetworkError, Ipv4Network};
use std::str::FromStr;
use core::str::FromStr;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

Expand Down Expand Up @@ -1105,6 +1123,7 @@ mod tests {
assert_eq!(first_hash, second_hash);
}

#[cfg(feature = "alloc")]
#[test]
fn summarize_address_range() {
let networks = Ipv4Network::summarize_address_range(
Expand All @@ -1122,6 +1141,7 @@ mod tests {
);
}

#[cfg(feature = "alloc")]
#[test]
fn summarize_address_range_whole_range() {
let networks = Ipv4Network::summarize_address_range(
Expand All @@ -1135,6 +1155,7 @@ mod tests {
);
}

#[cfg(feature = "alloc")]
#[test]
fn summarize_address_range_first_is_bigger() {
let networks = Ipv4Network::summarize_address_range(
Expand All @@ -1144,6 +1165,7 @@ mod tests {
assert_eq!(0, networks.len());
}

#[cfg(feature = "std")]
#[test]
fn collapse_addresses() {
let addresses = [
Expand All @@ -1157,6 +1179,7 @@ mod tests {
assert_eq!(Ipv4Network::from_str("192.0.2.0/24").unwrap(), collapsed[0]);
}

#[cfg(feature = "std")]
#[test]
fn collapse_addresses_2() {
let addresses = [
Expand All @@ -1168,6 +1191,7 @@ mod tests {
assert_eq!(Ipv4Network::from_str("192.0.2.0/24").unwrap(), collapsed[0]);
}

#[cfg(feature = "std")]
#[test]
fn collapse_addresses_3() {
// test only IP addresses including some duplicates
Expand All @@ -1185,6 +1209,7 @@ mod tests {
assert_eq!(Ipv4Network::from_str("1.1.1.4/32").unwrap(), collapsed[1]);
}

#[cfg(feature = "std")]
#[test]
fn collapse_addresses_4() {
// test a mix of IP addresses and networks including some duplicates
Expand All @@ -1199,6 +1224,7 @@ mod tests {
assert_eq!(Ipv4Network::from_str("1.1.1.0/30").unwrap(), collapsed[0]);
}

#[cfg(feature = "std")]
#[test]
fn collapse_addresses_5() {
// test only IP networks
Expand All @@ -1216,6 +1242,7 @@ mod tests {
assert_eq!(Ipv4Network::from_str("1.1.4.0/24").unwrap(), collapsed[1]);
}

#[cfg(feature = "std")]
#[test]
fn collapse_addresses_5_order() {
let addresses = [
Expand All @@ -1233,6 +1260,7 @@ mod tests {
assert_eq!(Ipv4Network::from_str("1.1.4.0/24").unwrap(), collapsed[1]);
}

#[cfg(feature = "std")]
#[test]
fn collapse_addresses_6() {
// test that two addresses are supernet'ed properly
Expand All @@ -1245,6 +1273,7 @@ mod tests {
assert_eq!(Ipv4Network::from_str("1.1.0.0/23").unwrap(), collapsed[0]);
}

#[cfg(feature = "std")]
#[test]
fn collapse_addresses_7() {
// test same IP networks
Expand All @@ -1257,6 +1286,7 @@ mod tests {
assert_eq!(Ipv4Network::from_str("1.1.1.1/32").unwrap(), collapsed[0]);
}

#[cfg(feature = "std")]
#[test]
fn collapse_addresses_8() {
let addresses = [
Expand All @@ -1268,6 +1298,7 @@ mod tests {
assert_eq!(Ipv4Network::from_str("0.0.0.0/0").unwrap(), collapsed[0]);
}

#[cfg(feature = "std")]
#[test]
fn collapse_addresses_9() {
let addresses = [
Expand Down
27 changes: 19 additions & 8 deletions src/ipv6_network.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::fmt;
use std::net::Ipv6Addr;
use std::str::FromStr;
use std::hash::{Hash, Hasher};
use core::fmt;
use core::net::Ipv6Addr;
use core::str::FromStr;
use core::hash::{Hash, Hasher};
use crate::{IpNetworkError, IpNetworkParseError};
use crate::helpers;
use crate::iterator;
#[cfg(feature = "std")]
use std::collections::HashMap;
#[cfg(feature = "std")]
use std::collections::hash_map::Entry;

/// IPv6 Multicast Address Scopes.
Expand Down Expand Up @@ -216,7 +218,7 @@ impl Ipv6Network {
/// # Ok::<(), ip_network::IpNetworkError>(())
/// ```
pub fn subnets(&self) -> iterator::Ipv6NetworkIterator {
let new_netmask = ::std::cmp::min(self.netmask + 1, Self::LENGTH);
let new_netmask = ::core::cmp::min(self.netmask + 1, Self::LENGTH);
iterator::Ipv6NetworkIterator::new(*self, new_netmask)
}

Expand Down Expand Up @@ -541,6 +543,8 @@ impl Ipv6Network {
/// # Examples
///
/// ```
/// #[cfg(feature = "std")]
/// {
/// use std::net::Ipv6Addr;
/// use ip_network::Ipv6Network;
/// use std::str::FromStr;
Expand All @@ -551,8 +555,10 @@ impl Ipv6Network {
/// ]);
///
/// assert_eq!(Ipv6Network::from_str("2001::/96")?, collapsed[0]);
/// }
/// # Ok::<(), ip_network::IpNetworkParseError>(())
/// ```
#[cfg(feature = "std")]
pub fn collapse_addresses(addresses: &[Self]) -> Vec<Self> {
let mut subnets = HashMap::new();

Expand Down Expand Up @@ -664,10 +670,14 @@ impl Hash for Ipv6Network {

#[cfg(test)]
mod tests {
use std::net::Ipv6Addr;
extern crate std;
extern crate alloc;

use alloc::string::ToString;
use core::net::Ipv6Addr;
use crate::{Ipv6Network, IpNetworkError, Ipv6MulticastScope};
use std::str::FromStr;
use std::hash::{Hash, Hasher};
use core::str::FromStr;
use core::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;

fn return_test_ipv6_network() -> Ipv6Network {
Expand Down Expand Up @@ -860,6 +870,7 @@ mod tests {
);
}

#[cfg(feature = "std")]
#[test]
fn collapse_addresses() {
let addresses = [
Expand Down
7 changes: 5 additions & 2 deletions src/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::net::{Ipv4Addr, Ipv6Addr};
use core::net::{Ipv4Addr, Ipv6Addr};
use crate::{Ipv4Network, Ipv6Network};
use crate::helpers;

Expand Down Expand Up @@ -272,7 +272,10 @@ impl ExactSizeIterator for Ipv6NetworkIterator {}

#[cfg(test)]
mod tests {
use std::net::{Ipv4Addr, Ipv6Addr};
extern crate std;

use std::vec::Vec;
use core::net::{Ipv4Addr, Ipv6Addr};
use crate::{Ipv4Network, Ipv6Network};
use super::{Ipv4NetworkIterator, Ipv4RangeIterator, Ipv6NetworkIterator};

Expand Down
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
//!
//! [PostgreSQL CIDR type]: https://www.postgresql.org/docs/current/datatype-net-types.html#DATATYPE-CIDR

// Set `no_std` where `std` feature is disabled
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "diesel")]
#[macro_use]
extern crate diesel;
Expand All @@ -44,8 +50,8 @@ mod postgres_support;
#[cfg(feature = "serde")]
mod serde_support;

use std::error::Error;
use std::fmt;
use core::error::Error;
use core::fmt;

pub use self::ip_network::IpNetwork;
pub use self::ipv4_network::Ipv4Network;
Expand Down