Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita638ada

Browse files
authored
Merge pull request#877 from timando/cidr
Add ToSql / FromSql for IpInet and IpCidr from cidr crate
2 parents25cabd5 +27039f6 commita638ada

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

‎postgres-types/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ categories = ["database"]
1414
derive = ["postgres-derive"]
1515
array-impls = ["array-init"]
1616
with-bit-vec-0_6 = ["bit-vec-06"]
17+
with-cidr-0_2 = ["cidr-02"]
1718
with-chrono-0_4 = ["chrono-04"]
1819
with-eui48-0_4 = ["eui48-04"]
1920
with-eui48-1 = ["eui48-1"]
@@ -33,6 +34,7 @@ postgres-derive = { version = "0.4.0", optional = true, path = "../postgres-deri
3334
array-init = {version ="2",optional =true }
3435
bit-vec-06 = {version ="0.6",package ="bit-vec",optional =true }
3536
chrono-04 = {version ="0.4.16",package ="chrono",default-features =false,features = ["clock"],optional =true }
37+
cidr-02 = {version ="0.2",package ="cidr",optional =true }
3638
eui48-04 = {version ="0.4",package ="eui48",optional =true }
3739
eui48-1 = {version ="1.0",package ="eui48",optional =true }
3840
geo-types-06 = {version ="0.6",package ="geo-types",optional =true }

‎postgres-types/src/cidr_02.rs‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use bytes::BytesMut;
2+
use cidr_02::{IpCidr,IpInet};
3+
use postgres_protocol::types;
4+
use std::error::Error;
5+
6+
usecrate::{FromSql,IsNull,ToSql,Type};
7+
8+
impl<'a>FromSql<'a>forIpCidr{
9+
fnfrom_sql(_:&Type,raw:&[u8]) ->Result<Self,Box<dynError +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+
implToSqlforIpCidr{
18+
fnto_sql(&self, _:&Type,w:&mutBytesMut) ->Result<IsNull,Box<dynError +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>forIpInet{
28+
fnfrom_sql(_:&Type,raw:&[u8]) ->Result<Self,Box<dynError +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+
implToSqlforIpInet{
37+
fnto_sql(&self, _:&Type,w:&mutBytesMut) ->Result<IsNull,Box<dynError +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‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ where
212212
mod bit_vec_06;
213213
#[cfg(feature ="with-chrono-0_4")]
214214
mod chrono_04;
215+
#[cfg(feature ="with-cidr-0_2")]
216+
mod cidr_02;
215217
#[cfg(feature ="with-eui48-0_4")]
216218
mod eui48_04;
217219
#[cfg(feature ="with-eui48-1")]
@@ -436,6 +438,8 @@ impl WrongType {
436438
/// | `uuid::Uuid` | UUID |
437439
/// | `bit_vec::BitVec` | BIT, VARBIT |
438440
/// | `eui48::MacAddress` | MACADDR |
441+
/// | `cidr::InetCidr` | CIDR |
442+
/// | `cidr::InetAddr` | INET |
439443
///
440444
/// # Nullability
441445
///

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp