pub enum IpNetwork {
V4(Ipv4Network),
V6(Ipv6Network),
}with-ipnetwork only.Expand description
Represents a generic network range. This type can have two variants: the v4 and the v6 case.
Variantsยง
V4(Ipv4Network)
V6(Ipv6Network)
Implementationsยง
Sourceยงimpl IpNetwork
impl IpNetwork
Sourcepub fn new(ip: IpAddr, prefix: u8) -> Result<IpNetwork, IpNetworkError>
pub fn new(ip: IpAddr, prefix: u8) -> Result<IpNetwork, IpNetworkError>
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.
Sourcepub fn with_netmask(
netaddr: IpAddr,
netmask: IpAddr,
) -> Result<IpNetwork, IpNetworkError>
pub fn with_netmask( netaddr: IpAddr, netmask: IpAddr, ) -> Result<IpNetwork, IpNetworkError>
Constructs a new IpNetwork from a network address and a network mask.
If the netmask is not valid this will return an IpNetworkError::InvalidPrefix.
Sourcepub fn prefix(&self) -> u8
pub fn prefix(&self) -> u8
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);Sourcepub fn network(&self) -> IpAddr
pub fn network(&self) -> IpAddr
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));Sourcepub fn broadcast(&self) -> IpAddr
pub fn broadcast(&self) -> IpAddr
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));Sourcepub fn mask(&self) -> IpAddr
pub fn mask(&self) -> IpAddr
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));Sourcepub fn is_ipv4(&self) -> bool
pub fn is_ipv4(&self) -> bool
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);Sourcepub fn is_ipv6(&self) -> bool
pub fn is_ipv6(&self) -> bool
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);Sourcepub fn contains(&self, ip: IpAddr) -> bool
pub fn contains(&self, ip: IpAddr) -> bool
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));Sourcepub fn size(&self) -> NetworkSize
pub fn size(&self) -> NetworkSize
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))Sourcepub fn iter(&self) -> IpNetworkIterator
pub fn iter(&self) -> IpNetworkIterator
Returns an iterator over the addresses contained in the network.
This lists all the addresses in the network range, in ascending order.
Trait Implementationsยง
Sourceยงimpl From<Ipv4Network> for IpNetwork
impl From<Ipv4Network> for IpNetwork
Sourceยงfn from(v4: Ipv4Network) -> IpNetwork
fn from(v4: Ipv4Network) -> IpNetwork
Sourceยงimpl From<Ipv6Network> for IpNetwork
impl From<Ipv6Network> for IpNetwork
Sourceยงfn from(v6: Ipv6Network) -> IpNetwork
fn from(v6: Ipv6Network) -> IpNetwork
Sourceยงimpl FromStr for IpNetwork
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.
impl FromStr for IpNetwork
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);Sourceยงimpl IntoActiveValue<IpNetwork> for IpNetwork
impl IntoActiveValue<IpNetwork> for IpNetwork
Sourceยงfn into_active_value(self) -> ActiveValue<IpNetwork>
fn into_active_value(self) -> ActiveValue<IpNetwork>
Sourceยงimpl IntoIterator for &IpNetwork
impl IntoIterator for &IpNetwork
Sourceยงimpl Ord for IpNetwork
impl Ord for IpNetwork
1.21.0 ยท Sourceยงfn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Sourceยงimpl PartialOrd for IpNetwork
impl PartialOrd for IpNetwork
Sourceยงimpl TryFromU64 for IpNetwork
impl TryFromU64 for IpNetwork
Sourceยงimpl TryGetable for IpNetwork
impl TryGetable for IpNetwork
Sourceยงfn try_get_by<I: ColIdx>(res: &QueryResult, idx: I) -> Result<Self, TryGetError>
fn try_get_by<I: ColIdx>(res: &QueryResult, idx: I) -> Result<Self, TryGetError>
Sourceยงfn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError>
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError>
Sourceยงfn try_get_by_index(
res: &QueryResult,
index: usize,
) -> Result<Self, TryGetError>
fn try_get_by_index( res: &QueryResult, index: usize, ) -> Result<Self, TryGetError>
Sourceยงimpl ValueType for IpNetwork
impl ValueType for IpNetwork
fn try_from(v: Value) -> Result<IpNetwork, ValueTypeErr>
fn type_name() -> String
fn array_type() -> ArrayType
fn column_type() -> ColumnType
fn unwrap(v: Value) -> Self
fn expect(v: Value, msg: &str) -> Self
fn is_option() -> bool
fn enum_type_name() -> Option<&'static str>
impl Copy for IpNetwork
impl Eq for IpNetwork
impl NotU8 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 UnsafeUnpin 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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Sourceยงimpl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Sourceยงimpl<T> ExprTrait for T
impl<T> ExprTrait for T
Sourceยงfn as_enum<N>(self, type_name: N) -> Exprwhere
N: IntoIden,
fn as_enum<N>(self, type_name: N) -> Exprwhere
N: IntoIden,
AS enum expression. Read moreSourceยงfn cast_as<N>(self, type_name: N) -> Exprwhere
N: IntoIden,
fn cast_as<N>(self, type_name: N) -> Exprwhere
N: IntoIden,
CAST AS expression. Read moreSourceยงfn count_distinct(self) -> Expr
fn count_distinct(self) -> Expr
COUNT function with the DISTINCT modifier. Read moreSourceยงfn equals<C>(self, col: C) -> Exprwhere
C: IntoColumnRef,
fn equals<C>(self, col: C) -> Exprwhere
C: IntoColumnRef,
Sourceยงfn in_subquery(self, sel: SelectStatement) -> Expr
fn in_subquery(self, sel: SelectStatement) -> Expr
IN sub-query expression. Read moreSourceยงfn in_tuples<V, I>(self, v: I) -> Exprwhere
V: IntoValueTuple,
I: IntoIterator<Item = V>,
fn in_tuples<V, I>(self, v: I) -> Exprwhere
V: IntoValueTuple,
I: IntoIterator<Item = V>,
IN sub expression. Read moreSourceยงfn is_not_null(self) -> Expr
fn is_not_null(self) -> Expr
IS NOT NULL expression. Read moreSourceยงfn left_shift<R>(self, right: R) -> Expr
fn left_shift<R>(self, right: R) -> Expr
Sourceยงfn not_between<A, B>(self, a: A, b: B) -> Expr
fn not_between<A, B>(self, a: A, b: B) -> Expr
NOT BETWEEN expression. Read moreSourceยงfn not_equals<C>(self, col: C) -> Exprwhere
C: IntoColumnRef,
fn not_equals<C>(self, col: C) -> Exprwhere
C: IntoColumnRef,
Sourceยงfn not_in_subquery(self, sel: SelectStatement) -> Expr
fn not_in_subquery(self, sel: SelectStatement) -> Expr
NOT IN sub-query expression. Read moreSourceยงfn not_like<L>(self, like: L) -> Exprwhere
L: IntoLikeExpr,
fn not_like<L>(self, like: L) -> Exprwhere
L: IntoLikeExpr,
NOT LIKE expression. Read moreSourceยงfn right_shift<R>(self, right: R) -> Expr
fn right_shift<R>(self, right: R) -> Expr
Sourceยงimpl<V> FromValueTuple for V
impl<V> FromValueTuple for V
fn from_value_tuple<I>(i: I) -> Vwhere
I: IntoValueTuple,
Sourceยงimpl<T> Instrument for T
impl<T> Instrument for T
Sourceยงfn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Sourceยงfn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
Sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSourceยงimpl<T> IntoValueTuple for Twhere
T: Into<ValueTuple>,
impl<T> IntoValueTuple for Twhere
T: Into<ValueTuple>,
fn into_value_tuple(self) -> ValueTuple
Sourceยงimpl<T> PgExpr for Twhere
T: ExprTrait,
impl<T> PgExpr for Twhere
T: ExprTrait,
Sourceยงfn concatenate<T>(self, right: T) -> Expr
fn concatenate<T>(self, right: T) -> Expr
||) expression. Read moreSourceยงfn matches<T>(self, expr: T) -> Expr
fn matches<T>(self, expr: T) -> Expr
@@) expression. Read moreSourceยงfn contains<T>(self, expr: T) -> Expr
fn contains<T>(self, expr: T) -> Expr
@>) expression. Read moreSourceยงfn contained<T>(self, expr: T) -> Expr
fn contained<T>(self, expr: T) -> Expr
<@) expression. Read moreSourceยงfn ilike<L>(self, like: L) -> Exprwhere
L: IntoLikeExpr,
fn ilike<L>(self, like: L) -> Exprwhere
L: IntoLikeExpr,
ILIKE expression. Read moreSourceยงfn not_ilike<L>(self, like: L) -> Exprwhere
L: IntoLikeExpr,
fn not_ilike<L>(self, like: L) -> Exprwhere
L: IntoLikeExpr,
NOT ILIKE expressionSourceยงfn get_json_field<T>(self, right: T) -> Expr
fn get_json_field<T>(self, right: T) -> Expr
->). Read moreSourceยงfn cast_json_field<T>(self, right: T) -> Expr
fn cast_json_field<T>(self, right: T) -> Expr
->>). Read more