pub enum IpNetwork {
V4(Ipv4Network),
V6(Ipv6Network),
}Expand description
Holds IPv4 or IPv6 network.
Variants§
V4(Ipv4Network)
V6(Ipv6Network)
Implementations§
Source§impl IpNetwork
impl IpNetwork
Sourcepub fn new<I: Into<IpAddr>>(
network_address: I,
netmask: u8,
) -> Result<Self, IpNetworkError>
pub fn new<I: Into<IpAddr>>( network_address: I, netmask: u8, ) -> Result<Self, IpNetworkError>
Constructs new IpNetwork based on IpAddr and netmask.
§Examples
use std::net::{IpAddr, Ipv4Addr};
use std::str::FromStr;
use ip_network::{IpNetwork, Ipv4Network};
let network_address = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 0));
let ip_network = IpNetwork::new(network_address, 24)?;
assert_eq!(ip_network.network_address(), IpAddr::V4(Ipv4Addr::new(192, 168, 1, 0)));
assert_eq!(ip_network.netmask(), 24);Sourcepub fn new_truncate<I: Into<IpAddr>>(
network_address: I,
netmask: u8,
) -> Result<Self, IpNetworkError>
pub fn new_truncate<I: Into<IpAddr>>( network_address: I, netmask: u8, ) -> Result<Self, IpNetworkError>
Constructs new IpNetwork based on IpAddr and netmask with truncating host bits
from given network_address.
Returns error if netmask is bigger than 32 for IPv4 and 128 for IPv6.
§Examples
use std::net::{IpAddr, Ipv4Addr};
use ip_network::IpNetwork;
let network_address = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 128));
let ip_network = IpNetwork::new_truncate(network_address, 24)?;
assert_eq!(ip_network.network_address(), IpAddr::V4(Ipv4Addr::new(192, 168, 1, 0)));
assert_eq!(ip_network.netmask(), 24);Sourcepub fn network_address(&self) -> IpAddr
pub fn network_address(&self) -> IpAddr
Returns network IP address.
§Examples
use std::net::{IpAddr, Ipv4Addr};
use ip_network::IpNetwork;
let ip_network = IpNetwork::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert_eq!(ip_network.network_address(), IpAddr::V4(Ipv4Addr::new(192, 168, 1, 0)));Sourcepub fn netmask(&self) -> u8
pub fn netmask(&self) -> u8
Returns network mask as integer.
§Examples
use std::net::{IpAddr, Ipv4Addr};
use ip_network::IpNetwork;
let ip_network = IpNetwork::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert_eq!(ip_network.netmask(), 24);Sourcepub fn contains<I: Into<IpAddr>>(&self, ip: I) -> bool
pub fn contains<I: Into<IpAddr>>(&self, ip: I) -> bool
Returns true if IpNetwork contains IpAddr. For different network type
(for example IpNetwork is IPv6 and IpAddr is IPv4) always returns false.
§Examples
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use ip_network::IpNetwork;
let ip_network = IpNetwork::new(Ipv4Addr::new(192, 168, 1, 0), 24)?;
assert!(ip_network.contains(Ipv4Addr::new(192, 168, 1, 25)));
assert!(!ip_network.contains(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 1, 0, 0)));Sourcepub fn is_default_route(&self) -> bool
pub fn is_default_route(&self) -> bool
Returns true if the network is default route, that contains all IP addresses.
Sourcepub fn is_multicast(&self) -> bool
pub fn is_multicast(&self) -> bool
Returns true if the network is part of multicast network range.
Sourcepub fn is_documentation(&self) -> bool
pub fn is_documentation(&self) -> bool
Returns true if this is a part of network reserved for documentation.
Sourcepub fn is_loopback(&self) -> bool
pub fn is_loopback(&self) -> bool
Returns true if this network is inside loopback address range.
Sourcepub fn from_str_truncate(s: &str) -> Result<Self, IpNetworkParseError>
pub fn from_str_truncate(s: &str) -> Result<Self, IpNetworkParseError>
Converts string in format IPv4 (X.X.X.X/Y) or IPv6 (X:X::X/Y) CIDR notation to IpNetwork.
§Examples
use std::net::Ipv4Addr;
use ip_network::{IpNetwork, Ipv4Network};
let ip_network = IpNetwork::from_str_truncate("192.168.1.1/24").unwrap();
assert_eq!(ip_network, IpNetwork::V4(Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24).unwrap()));Sourcepub fn collapse_addresses(addresses: &[Self]) -> Vec<Self>
pub fn collapse_addresses(addresses: &[Self]) -> Vec<Self>
Return an iterator of the collapsed IpNetworks.
Trait Implementations§
Source§impl<'expr> AsExpression<Cidr> for &'expr IpNetwork
impl<'expr> AsExpression<Cidr> for &'expr IpNetwork
Source§type Expression = Bound<Cidr, &'expr IpNetwork>
type Expression = Bound<Cidr, &'expr IpNetwork>
Source§fn as_expression(self) -> Self::Expression
fn as_expression(self) -> Self::Expression
Source§impl AsExpression<Cidr> for IpNetwork
impl AsExpression<Cidr> for IpNetwork
Source§type Expression = Bound<Cidr, IpNetwork>
type Expression = Bound<Cidr, IpNetwork>
Source§fn as_expression(self) -> Self::Expression
fn as_expression(self) -> Self::Expression
Source§impl<'expr> AsExpression<Nullable<Cidr>> for &'expr IpNetwork
impl<'expr> AsExpression<Nullable<Cidr>> for &'expr IpNetwork
Source§fn as_expression(self) -> Self::Expression
fn as_expression(self) -> Self::Expression
Source§impl AsExpression<Nullable<Cidr>> for IpNetwork
impl AsExpression<Nullable<Cidr>> for IpNetwork
Source§fn as_expression(self) -> Self::Expression
fn as_expression(self) -> Self::Expression
Source§impl<'de> Deserialize<'de> for IpNetwork
impl<'de> Deserialize<'de> for IpNetwork
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl Display for IpNetwork
impl Display for IpNetwork
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Converts IpNetwork to string in format X.X.X.X/Y for IPv4 and X:X::X/Y for IPv6 (CIDR notation).
§Examples
use std::net::Ipv4Addr;
use ip_network::{IpNetwork, Ipv4Network};
let ip_network = IpNetwork::V4(Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24)?);
assert_eq!(ip_network.to_string(), "192.168.1.0/24");Source§impl From<Ipv4Network> for IpNetwork
impl From<Ipv4Network> for IpNetwork
Source§fn from(network: Ipv4Network) -> Self
fn from(network: Ipv4Network) -> Self
Source§impl From<Ipv6Network> for IpNetwork
impl From<Ipv6Network> for IpNetwork
Source§fn from(network: Ipv6Network) -> Self
fn from(network: Ipv6Network) -> Self
Source§impl<'a> FromSql<'a> for IpNetwork
impl<'a> FromSql<'a> for IpNetwork
Source§fn from_sql(
t: &Type,
raw: &'a [u8],
) -> Result<IpNetwork, Box<dyn Error + Sync + Send>>
fn from_sql( t: &Type, raw: &'a [u8], ) -> Result<IpNetwork, Box<dyn Error + Sync + Send>>
Type in its binary format. Read moreSource§fn accepts(ty: &Type) -> bool
fn accepts(ty: &Type) -> bool
Type.Source§impl<__ST, __DB> FromSqlRow<__ST, __DB> for IpNetwork
impl<__ST, __DB> FromSqlRow<__ST, __DB> for IpNetwork
Source§const FIELDS_NEEDED: usize = 1usize
const FIELDS_NEEDED: usize = 1usize
row.take() in build_from_rowSource§impl FromStr for IpNetwork
impl FromStr for IpNetwork
Source§fn from_str(s: &str) -> Result<IpNetwork, IpNetworkParseError>
fn from_str(s: &str) -> Result<IpNetwork, IpNetworkParseError>
Converts string in format IPv4 (X.X.X.X/Y) or IPv6 (X:X::X/Y) CIDR notation to IpNetwork.
§Examples
use std::net::Ipv4Addr;
use std::str::FromStr;
use ip_network::{IpNetwork, Ipv4Network};
let ip_network = IpNetwork::from_str("192.168.1.0/24").unwrap();
assert_eq!(ip_network, IpNetwork::V4(Ipv4Network::new(Ipv4Addr::new(192, 168, 1, 0), 24).unwrap()));Source§type Err = IpNetworkParseError
type Err = IpNetworkParseError
Source§impl Ord for IpNetwork
impl Ord for IpNetwork
Source§impl PartialEq<IpNetwork> for Ipv4Network
impl PartialEq<IpNetwork> for Ipv4Network
Source§impl PartialEq<IpNetwork> for Ipv6Network
impl PartialEq<IpNetwork> for Ipv6Network
Source§impl PartialEq<Ipv4Network> for IpNetwork
impl PartialEq<Ipv4Network> for IpNetwork
Source§impl PartialEq<Ipv6Network> for IpNetwork
impl PartialEq<Ipv6Network> for IpNetwork
Source§impl PartialOrd<IpNetwork> for Ipv4Network
impl PartialOrd<IpNetwork> for Ipv4Network
Source§impl PartialOrd<IpNetwork> for Ipv6Network
impl PartialOrd<IpNetwork> for Ipv6Network
Source§impl PartialOrd<Ipv4Network> for IpNetwork
impl PartialOrd<Ipv4Network> for IpNetwork
Source§impl PartialOrd<Ipv6Network> for IpNetwork
impl PartialOrd<Ipv6Network> for IpNetwork
Source§impl PartialOrd for IpNetwork
impl PartialOrd for IpNetwork
Source§impl ToSql for IpNetwork
impl ToSql for IpNetwork
Source§fn to_sql(
&self,
ty: &Type,
w: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn to_sql( &self, ty: &Type, w: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
self into the binary format of the specified
Postgres Type, appending it to out. Read moreSource§fn accepts(ty: &Type) -> bool
fn accepts(ty: &Type) -> bool
Type.Source§fn to_sql_checked(
&self,
ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn to_sql_checked( &self, ty: &Type, out: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
Source§fn encode_format(&self, _ty: &Type) -> Format
fn encode_format(&self, _ty: &Type) -> Format
impl Copy for IpNetwork
impl Eq for IpNetwork
impl StructuralPartialEq for IpNetwork
Auto Trait Implementations§
impl Freeze for IpNetwork
impl RefUnwindSafe for IpNetwork
impl Send for IpNetwork
impl Sync for IpNetwork
impl Unpin for IpNetwork
impl UnwindSafe for IpNetwork
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> BorrowToSql for Twhere
T: ToSql,
impl<T> BorrowToSql for Twhere
T: ToSql,
Source§fn borrow_to_sql(&self) -> &dyn ToSql
fn borrow_to_sql(&self) -> &dyn ToSql
self as a ToSql trait object.Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expressionwhere
Self: Sized + AsExpression<T>,
fn into_sql<T>(self) -> Self::Expressionwhere
Self: Sized + AsExpression<T>,
self to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
&self to an expression for Diesel’s query builder. Read more