Trait diesel::prelude::PgNetExpressionMethods

source ·
pub trait PgNetExpressionMethods: Expression + Sized {
    // Provided methods
    fn contains<T>(self, other: T) -> ContainsNet<Self, T>
       where T: AsExpression<Inet> { ... }
    fn contains_or_eq<T>(self, other: T) -> ContainsNetLoose<Self, T>
       where T: AsExpression<Inet> { ... }
    fn is_contained_by<T>(self, other: T) -> IsContainedByNet<Self, T>
       where T: AsExpression<Inet> { ... }
    fn is_contained_by_or_eq<T>(
        self,
        other: T,
    ) -> IsContainedByNetLoose<Self, T>
       where T: AsExpression<Inet> { ... }
    fn overlaps_with<T>(self, other: T) -> OverlapsWithNet<Self, T>
       where T: AsExpression<Inet> { ... }
    fn and<T>(self, other: T) -> AndNet<Self, T>
       where T: AsExpression<Inet> { ... }
    fn or<T>(self, other: T) -> OrNet<Self, T>
       where T: AsExpression<Inet> { ... }
    fn diff<T>(self, other: T) -> DifferenceNet<Self, T>
       where T: AsExpression<Inet> { ... }
}
Expand description

PostgreSQL specific methods present between CIDR/INET expressions

Provided Methods§

source

fn contains<T>(self, other: T) -> ContainsNet<Self, T>
where T: AsExpression<Inet>,

Creates a PostgreSQL >> expression.

This operator returns whether a subnet strictly contains another subnet or address.

§Example
diesel::insert_into(hosts)
    .values(vec![address.eq(IpNetwork::from_str("10.0.2.3/24").unwrap()),
                 address.eq(IpNetwork::from_str("10.0.3.4/23").unwrap())])
    .execute(conn)?;

let my_hosts = hosts.select(id)
    .filter(address.contains(IpNetwork::from_str("10.0.2.5").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(vec![1, 2], my_hosts);

let my_hosts = hosts.select(id)
    .filter(address.contains(IpNetwork::from_str("10.0.2.5/24").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(vec![2], my_hosts);

let my_hosts = hosts.select(id)
    .filter(address.contains(IpNetwork::from_str("10.0.3.31").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(vec![2], my_hosts);
source

fn contains_or_eq<T>(self, other: T) -> ContainsNetLoose<Self, T>
where T: AsExpression<Inet>,

Creates a PostgreSQL >>= expression.

This operator returns whether a subnet contains or is equal to another subnet.

§Example
diesel::insert_into(hosts)
    .values(vec![address.eq(IpNetwork::from_str("10.0.2.3/24").unwrap()),
                 address.eq(IpNetwork::from_str("10.0.3.4/23").unwrap())])
    .execute(conn)?;

let my_hosts = hosts.select(id)
    .filter(address.contains_or_eq(IpNetwork::from_str("10.0.2.5").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(vec![1, 2], my_hosts);

let my_hosts = hosts.select(id)
    .filter(address.contains_or_eq(IpNetwork::from_str("10.0.2.5/24").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(vec![1, 2], my_hosts);

let my_hosts = hosts.select(id)
    .filter(address.contains_or_eq(IpNetwork::from_str("10.0.3.31").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(vec![2], my_hosts);
source

fn is_contained_by<T>(self, other: T) -> IsContainedByNet<Self, T>
where T: AsExpression<Inet>,

Creates a PostgreSQL << expression.

This operator returns whether a subnet or address is strictly contained by another subnet.

§Example
diesel::insert_into(hosts)
    .values(vec![address.eq(IpNetwork::from_str("10.0.2.3/24").unwrap()),
                 address.eq(IpNetwork::from_str("10.0.3.4/23").unwrap())])
    .execute(conn)?;

let my_hosts = hosts.select(id)
    .filter(address.is_contained_by(IpNetwork::from_str("10.0.2.5/24").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(my_hosts.len(), 0);

let my_hosts = hosts.select(id)
    .filter(address.is_contained_by(IpNetwork::from_str("10.0.3.31/23").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(vec![1], my_hosts);

let my_hosts = hosts.select(id)
    .filter(address.is_contained_by(IpNetwork::from_str("10.0.3.31/22").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(vec![1, 2], my_hosts);
source

fn is_contained_by_or_eq<T>(self, other: T) -> IsContainedByNetLoose<Self, T>
where T: AsExpression<Inet>,

Creates a PostgreSQL >>= expression.

This operator returns whether a subnet is contained by or equal to another subnet.

§Example
diesel::insert_into(hosts)
    .values(vec![address.eq(IpNetwork::from_str("10.0.2.3/24").unwrap()),
                 address.eq(IpNetwork::from_str("10.0.3.4/23").unwrap())])
    .execute(conn)?;

let my_hosts = hosts.select(id)
    .filter(address.is_contained_by_or_eq(IpNetwork::from_str("10.0.2.5/24").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(vec![1], my_hosts);

let my_hosts = hosts.select(id)
    .filter(address.is_contained_by_or_eq(IpNetwork::from_str("10.0.3.31/23").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(vec![1, 2], my_hosts);
source

fn overlaps_with<T>(self, other: T) -> OverlapsWithNet<Self, T>
where T: AsExpression<Inet>,

Creates a PostgreSQL && expression.

This operator returns whether a subnet contains or is contained by another subnet.

§Example
diesel::insert_into(hosts)
    .values(vec![address.eq(IpNetwork::from_str("10.0.2.3/24").unwrap()),
                 address.eq(IpNetwork::from_str("10.0.3.4/23").unwrap())])
    .execute(conn)?;

let my_hosts = hosts.select(id)
    .filter(address.overlaps_with(IpNetwork::from_str("10.0.2.5/24").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(vec![1, 2], my_hosts);

let my_hosts = hosts.select(id)
    .filter(address.overlaps_with(IpNetwork::from_str("10.0.3.31/24").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(vec![2], my_hosts);

let my_hosts = hosts.select(id)
    .filter(address.overlaps_with(IpNetwork::from_str("10.0.3.31/23").unwrap()))
    .load::<i32>(conn)?;
assert_eq!(vec![1, 2], my_hosts);
source

fn and<T>(self, other: T) -> AndNet<Self, T>
where T: AsExpression<Inet>,

Creates a PostgreSQL & expression.

This operator computes the bitwise AND between two network addresses.

§Example
diesel::insert_into(hosts)
    .values(vec![address.eq(IpNetwork::from_str("10.0.2.3").unwrap())])
    .execute(conn)?;

let addr = hosts
    .select(address.and(IpNetwork::from_str("0.0.0.255").unwrap()))
    .first::<IpNetwork>(conn)?;
assert_eq!(addr, IpNetwork::from_str("0.0.0.3").unwrap());
source

fn or<T>(self, other: T) -> OrNet<Self, T>
where T: AsExpression<Inet>,

Creates a PostgreSQL | expression.

This operator computes the bitwise OR between two network addresses.

§Example
diesel::insert_into(hosts)
    .values(vec![address.eq(IpNetwork::from_str("10.0.2.3").unwrap())])
    .execute(conn)?;

let addr = hosts
    .select(address.or(IpNetwork::from_str("0.0.0.255").unwrap()))
    .first::<IpNetwork>(conn)?;
assert_eq!(addr, IpNetwork::from_str("10.0.2.255").unwrap());
source

fn diff<T>(self, other: T) -> DifferenceNet<Self, T>
where T: AsExpression<Inet>,

Creates a PostgreSQL - expression.

This operator substracts an address from an address to compute the distance between the two

§Example
diesel::insert_into(hosts)
    .values(vec![address.eq(IpNetwork::from_str("10.0.2.53").unwrap())])
    .execute(conn)?;

let offset = hosts
    .select(address.diff(IpNetwork::from_str("10.0.2.42").unwrap()))
    .first::<i64>(conn)?;
assert_eq!(offset, 11);

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T> PgNetExpressionMethods for T
where T: Expression, T::SqlType: InetOrCidr,