Enum sqlx::types::ipnetwork::IpNetwork[][src]

pub enum IpNetwork {
    V4(Ipv4Network),
    V6(Ipv6Network),
}
Expand description

Represents a generic network range. This type can have two variants: the v4 and the v6 case.

Variants

Implementations

Constructs a new IpNetwork from a given IpAddr and a prefix denoting the network size. If the prefix is larger than 32 (for IPv4) or 128 (for IPv6), this will raise an IpNetworkError::InvalidPrefix error. Support for IPv6 is not complete yet.

Constructs a new IpNetwork from a network address and a network mask.

If the netmask is not valid this will return an IpNetworkError::InvalidPrefix.

Returns the IP part of a given IpNetwork

Returns the prefix of the given IpNetwork

Example

use ipnetwork::IpNetwork;

assert_eq!(IpNetwork::V4("10.9.0.1".parse().unwrap()).prefix(), 32u8);
assert_eq!(IpNetwork::V4("10.9.0.32/16".parse().unwrap()).prefix(), 16u8);

assert_eq!(IpNetwork::V6("ff01::0".parse().unwrap()).prefix(), 128u8);
assert_eq!(IpNetwork::V6("ff01::0/32".parse().unwrap()).prefix(), 32u8);

Returns the address of the network denoted by this IpNetwork. This means the lowest possible IP address inside of the network.

Examples

use std::net::{Ipv4Addr, Ipv6Addr};
use ipnetwork::IpNetwork;

let net: IpNetwork = "10.1.9.32/16".parse().unwrap();
assert_eq!(net.network(), Ipv4Addr::new(10, 1, 0, 0));
let net: IpNetwork = "2001:db8::/96".parse().unwrap();
assert_eq!(net.network(), Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0));

Returns the broadcasting address of this IpNetwork. This means the highest possible IP address inside of the network.

Examples

use std::net::Ipv4Addr;
use ipnetwork::{IpNetwork, Ipv4Network};

let net: Ipv4Network = "10.9.0.32/16".parse().unwrap();
assert_eq!(net.broadcast(), Ipv4Addr::new(10, 9, 255, 255));

Returns the mask for this IpNetwork. That means the prefix most significant bits will be 1 and the rest 0

Example

use ipnetwork::IpNetwork;
use std::net::{Ipv4Addr, Ipv6Addr};

let v4_net: IpNetwork = "10.9.0.1".parse().unwrap();
assert_eq!(v4_net.mask(), Ipv4Addr::new(255, 255, 255, 255));
let v4_net: IpNetwork = "10.9.0.32/16".parse().unwrap();
assert_eq!(v4_net.mask(), Ipv4Addr::new(255, 255, 0, 0));

let v6_net: IpNetwork = "ff01::0".parse().unwrap();
assert_eq!(v6_net.mask(), Ipv6Addr::new(0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff));
let v6_net: IpNetwork = "ff01::0/32".parse().unwrap();
assert_eq!(v6_net.mask(), Ipv6Addr::new(0xffff, 0xffff, 0, 0, 0, 0, 0, 0));

Returns true if the IP in this IpNetwork is a valid IPv4 address, false if it’s a valid IPv6 address.

Example

 use ipnetwork::IpNetwork;

 let v4: IpNetwork = IpNetwork::V4("10.9.0.32/16".parse().unwrap());
 assert_eq!(v4.is_ipv4(), true);
 assert_eq!(v4.is_ipv6(), false);

Returns true if the IP in this IpNetwork is a valid IPv6 address, false if it’s a valid IPv4 address.

Example

 use ipnetwork::IpNetwork;

 let v6: IpNetwork = IpNetwork::V6("ff01::0/32".parse().unwrap());
 assert_eq!(v6.is_ipv6(), true);
 assert_eq!(v6.is_ipv4(), false);

Checks if a given IpAddr is in this IpNetwork

Examples

use std::net::IpAddr;
use ipnetwork::IpNetwork;

let net: IpNetwork = "127.0.0.0/24".parse().unwrap();
let ip1: IpAddr = "127.0.0.1".parse().unwrap();
let ip2: IpAddr = "172.0.0.1".parse().unwrap();
let ip4: IpAddr = "::1".parse().unwrap();
assert!(net.contains(ip1));
assert!(!net.contains(ip2));
assert!(!net.contains(ip4));

Returns the number of possible host addresses in this IpAddr

Examples

use ipnetwork::{IpNetwork, NetworkSize};


let net: IpNetwork = "127.0.0.0/24".parse().unwrap();
assert_eq!(net.size(), NetworkSize::V4(256))

Returns an iterator over the addresses contained in the network.

This lists all the addresses in the network range, in ascending order.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Decode a new value of this type using a raw value from the database.

Formats the value using the given formatter. Read more

Writes the value of self into buf without moving self. Read more

Writes the value of self into buf in the expected format for the database.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Tries to parse the given string into a IpNetwork. Will first try to parse it as an Ipv4Network and if that fails as an Ipv6Network. If both fails it will return an InvalidAddr error.

Examples

use std::net::Ipv4Addr;
use ipnetwork::{IpNetwork, Ipv4Network};

let expected = IpNetwork::V4(Ipv4Network::new(Ipv4Addr::new(10, 1, 9, 32), 16).unwrap());
let from_cidr: IpNetwork = "10.1.9.32/16".parse().unwrap();
assert_eq!(expected, from_cidr);

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Creates an iterator from a value. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Returns the canonical SQL type for this Rust type. Read more

Determines if this Rust type is compatible with the given SQL type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Converts self into T using Into<T>. Read more

Converts self into a target type. Read more

Compare self to key and return true if they are equal.

Causes self to use its Binary implementation when Debug-formatted.

Causes self to use its Display implementation when Debug-formatted. Read more

Causes self to use its LowerExp implementation when Debug-formatted. Read more

Causes self to use its LowerHex implementation when Debug-formatted. Read more

Causes self to use its Octal implementation when Debug-formatted.

Causes self to use its Pointer implementation when Debug-formatted. Read more

Causes self to use its UpperExp implementation when Debug-formatted. Read more

Causes self to use its UpperHex implementation when Debug-formatted. Read more

Performs the conversion.

Performs the conversion.

Pipes by value. This is generally the method you want to use. Read more

Borrows self and passes that borrow into the pipe function. Read more

Mutably borrows self and passes that borrow into the pipe function. Read more

Borrows self, then passes self.borrow() into the pipe function. Read more

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more

Borrows self, then passes self.as_ref() into the pipe function.

Mutably borrows self, then passes self.as_mut() into the pipe function. Read more

Borrows self, then passes self.deref() into the pipe function.

Mutably borrows self, then passes self.deref_mut() into the pipe function. Read more

Pipes a value into a function that cannot ordinarily be called in suffix position. Read more

Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait mutable borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait borrow into a function that cannot normally be called in suffix position. Read more

Pipes a trait mutable borrow into a function that cannot normally be called in suffix position. Read more

Pipes a dereference into a function that cannot normally be called in suffix position. Read more

Pipes a mutable dereference into a function that cannot normally be called in suffix position. Read more

Pipes a reference into a function that cannot ordinarily be called in suffix position. Read more

Pipes a mutable reference into a function that cannot ordinarily be called in suffix position. Read more

Should always be Self

Immutable access to a value. Read more

Mutable access to a value. Read more

Immutable access to the Borrow<B> of a value. Read more

Mutable access to the BorrowMut<B> of a value. Read more

Immutable access to the AsRef<R> view of a value. Read more

Mutable access to the AsMut<R> view of a value. Read more

Immutable access to the Deref::Target of a value. Read more

Mutable access to the Deref::Target of a value. Read more

Calls .tap() only in debug builds, and is erased in release builds.

Calls .tap_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow() only in debug builds, and is erased in release builds. Read more

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref() only in debug builds, and is erased in release builds. Read more

Calls .tap_ref_mut() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref() only in debug builds, and is erased in release builds. Read more

Calls .tap_deref_mut() only in debug builds, and is erased in release builds. Read more

Provides immutable access for inspection. Read more

Calls tap in debug builds, and does nothing in release builds.

Provides mutable access for modification. Read more

Calls tap_mut in debug builds, and does nothing in release builds.

Provides immutable access to the reference for inspection.

Calls tap_ref in debug builds, and does nothing in release builds.

Provides mutable access to the reference for modification.

Calls tap_ref_mut in debug builds, and does nothing in release builds.

Provides immutable access to the borrow for inspection. Read more

Calls tap_borrow in debug builds, and does nothing in release builds.

Provides mutable access to the borrow for modification.

Calls tap_borrow_mut in debug builds, and does nothing in release builds. Read more

Immutably dereferences self for inspection.

Calls tap_deref in debug builds, and does nothing in release builds.

Mutably dereferences self for modification.

Calls tap_deref_mut in debug builds, and does nothing in release builds. Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

Attempts to convert self into T using TryInto<T>. Read more

Attempts to convert self into a target type. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.