Skip to content

Commit 0785381

Browse files
authored
Merge pull request #1183 from rs-sac/upstream
Add support for cidr 0.3 under separate feature
2 parents 263fb90 + eea7f13 commit 0785381

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

postgres-types/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ array-impls = ["array-init"]
1616
js = ["postgres-protocol/js"]
1717
with-bit-vec-0_6 = ["bit-vec-06"]
1818
with-cidr-0_2 = ["cidr-02"]
19+
with-cidr-0_3 = ["cidr-03"]
1920
with-chrono-0_4 = ["chrono-04"]
2021
with-eui48-0_4 = ["eui48-04"]
2122
with-eui48-1 = ["eui48-1"]
@@ -41,6 +42,7 @@ chrono-04 = { version = "0.4.16", package = "chrono", default-features = false,
4142
"clock",
4243
], optional = true }
4344
cidr-02 = { version = "0.2", package = "cidr", optional = true }
45+
cidr-03 = { version = "0.3", package = "cidr", optional = true }
4446
# eui48-04 will stop compiling and support will be removed
4547
# See https://github.com/sfackler/rust-postgres/issues/1073
4648
eui48-04 = { version = "0.4", package = "eui48", optional = true }

postgres-types/src/cidr_03.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use bytes::BytesMut;
2+
use cidr_03::{IpCidr, IpInet};
3+
use postgres_protocol::types;
4+
use std::error::Error;
5+
6+
use crate::{FromSql, IsNull, ToSql, Type};
7+
8+
impl<'a> FromSql<'a> for IpCidr {
9+
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
10+
let inet = types::inet_from_sql(raw)?;
11+
Ok(IpCidr::new(inet.addr(), inet.netmask())?)
12+
}
13+
14+
accepts!(CIDR);
15+
}
16+
17+
impl ToSql for IpCidr {
18+
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
19+
types::inet_to_sql(self.first_address(), self.network_length(), w);
20+
Ok(IsNull::No)
21+
}
22+
23+
accepts!(CIDR);
24+
to_sql_checked!();
25+
}
26+
27+
impl<'a> FromSql<'a> for IpInet {
28+
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
29+
let inet = types::inet_from_sql(raw)?;
30+
Ok(IpInet::new(inet.addr(), inet.netmask())?)
31+
}
32+
33+
accepts!(INET);
34+
}
35+
36+
impl ToSql for IpInet {
37+
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
38+
types::inet_to_sql(self.address(), self.network_length(), w);
39+
Ok(IsNull::No)
40+
}
41+
42+
accepts!(INET);
43+
to_sql_checked!();
44+
}

postgres-types/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ mod bit_vec_06;
268268
mod chrono_04;
269269
#[cfg(feature = "with-cidr-0_2")]
270270
mod cidr_02;
271+
#[cfg(feature = "with-cidr-0_3")]
272+
mod cidr_03;
271273
#[cfg(feature = "with-eui48-0_4")]
272274
mod eui48_04;
273275
#[cfg(feature = "with-eui48-1")]

0 commit comments

Comments
 (0)