Skip to content

Commit a23036e

Browse files
committed
Add support for bit-vec 0.7 & 0.8
1 parent 490c485 commit a23036e

File tree

11 files changed

+144
-0
lines changed

11 files changed

+144
-0
lines changed

postgres-types/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ derive = ["postgres-derive"]
1515
array-impls = ["array-init"]
1616
js = ["postgres-protocol/js"]
1717
with-bit-vec-0_6 = ["bit-vec-06"]
18+
with-bit-vec-0_7 = ["bit-vec-07"]
19+
with-bit-vec-0_8 = ["bit-vec-08"]
1820
with-cidr-0_2 = ["cidr-02"]
1921
with-cidr-0_3 = ["cidr-03"]
2022
with-chrono-0_4 = ["chrono-04"]
@@ -39,6 +41,8 @@ postgres-derive = { version = "0.4.6", optional = true, path = "../postgres-deri
3941

4042
array-init = { version = "2", optional = true }
4143
bit-vec-06 = { version = "0.6", package = "bit-vec", optional = true }
44+
bit-vec-07 = { version = "0.7", package = "bit-vec", optional = true }
45+
bit-vec-08 = { version = "0.8", package = "bit-vec", optional = true }
4246
chrono-04 = { version = "0.4.16", package = "chrono", default-features = false, features = [
4347
"clock",
4448
], optional = true }

postgres-types/src/bit_vec_07.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use bit_vec_07::BitVec;
2+
use bytes::BytesMut;
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 BitVec {
9+
fn from_sql(_: &Type, raw: &[u8]) -> Result<BitVec, Box<dyn Error + Sync + Send>> {
10+
let varbit = types::varbit_from_sql(raw)?;
11+
let mut bitvec = BitVec::from_bytes(varbit.bytes());
12+
while bitvec.len() > varbit.len() {
13+
bitvec.pop();
14+
}
15+
16+
Ok(bitvec)
17+
}
18+
19+
accepts!(BIT, VARBIT);
20+
}
21+
22+
impl ToSql for BitVec {
23+
fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
24+
types::varbit_to_sql(self.len(), self.to_bytes().into_iter(), out)?;
25+
Ok(IsNull::No)
26+
}
27+
28+
accepts!(BIT, VARBIT);
29+
to_sql_checked!();
30+
}

postgres-types/src/bit_vec_08.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use bit_vec_08::BitVec;
2+
use bytes::BytesMut;
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 BitVec {
9+
fn from_sql(_: &Type, raw: &[u8]) -> Result<BitVec, Box<dyn Error + Sync + Send>> {
10+
let varbit = types::varbit_from_sql(raw)?;
11+
let mut bitvec = BitVec::from_bytes(varbit.bytes());
12+
while bitvec.len() > varbit.len() {
13+
bitvec.pop();
14+
}
15+
16+
Ok(bitvec)
17+
}
18+
19+
accepts!(BIT, VARBIT);
20+
}
21+
22+
impl ToSql for BitVec {
23+
fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
24+
types::varbit_to_sql(self.len(), self.to_bytes().into_iter(), out)?;
25+
Ok(IsNull::No)
26+
}
27+
28+
accepts!(BIT, VARBIT);
29+
to_sql_checked!();
30+
}

postgres-types/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ where
264264

265265
#[cfg(feature = "with-bit-vec-0_6")]
266266
mod bit_vec_06;
267+
#[cfg(feature = "with-bit-vec-0_7")]
268+
mod bit_vec_07;
269+
#[cfg(feature = "with-bit-vec-0_8")]
270+
mod bit_vec_08;
267271
#[cfg(feature = "with-chrono-0_4")]
268272
mod chrono_04;
269273
#[cfg(feature = "with-cidr-0_2")]

postgres/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ circle-ci = { repository = "sfackler/rust-postgres" }
2323
[features]
2424
array-impls = ["tokio-postgres/array-impls"]
2525
with-bit-vec-0_6 = ["tokio-postgres/with-bit-vec-0_6"]
26+
with-bit-vec-0_7 = ["tokio-postgres/with-bit-vec-0_7"]
27+
with-bit-vec-0_8 = ["tokio-postgres/with-bit-vec-0_8"]
2628
with-chrono-0_4 = ["tokio-postgres/with-chrono-0_4"]
2729
with-cidr-0_2 = ["tokio-postgres/with-cidr-0_2"]
2830
with-cidr-0_3 = ["tokio-postgres/with-cidr-0_3"]

postgres/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
//! | Feature | Description | Extra dependencies | Default |
5555
//! | ------- | ----------- | ------------------ | ------- |
5656
//! | `with-bit-vec-0_6` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.6 | no |
57+
//! | `with-bit-vec-0_7` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.7 | no |
58+
//! | `with-bit-vec-0_8` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.8 | no |
5759
//! | `with-chrono-0_4` | Enable support for the `chrono` crate. | [chrono](https://crates.io/crates/chrono) 0.4 | no |
5860
//! | `with-eui48-0_4` | Enable support for the 0.4 version of the `eui48` crate. This is deprecated and will be removed. | [eui48](https://crates.io/crates/eui48) 0.4 | no |
5961
//! | `with-eui48-1` | Enable support for the 1.0 version of the `eui48` crate. | [eui48](https://crates.io/crates/eui48) 1.0 | no |

tokio-postgres/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ runtime = ["tokio/net", "tokio/time"]
2929

3030
array-impls = ["postgres-types/array-impls"]
3131
with-bit-vec-0_6 = ["postgres-types/with-bit-vec-0_6"]
32+
with-bit-vec-0_7 = ["postgres-types/with-bit-vec-0_7"]
33+
with-bit-vec-0_8 = ["postgres-types/with-bit-vec-0_8"]
3234
with-chrono-0_4 = ["postgres-types/with-chrono-0_4"]
3335
with-cidr-0_2 = ["postgres-types/with-cidr-0_2"]
3436
with-cidr-0_3 = ["postgres-types/with-cidr-0_3"]
@@ -81,6 +83,8 @@ tokio = { version = "1.0", features = [
8183
] }
8284

8385
bit-vec-06 = { version = "0.6", package = "bit-vec" }
86+
bit-vec-07 = { version = "0.7", package = "bit-vec" }
87+
bit-vec-08 = { version = "0.8", package = "bit-vec" }
8488
chrono-04 = { version = "0.4", package = "chrono", default-features = false }
8589
eui48-1 = { version = "1.0", package = "eui48", default-features = false }
8690
geo-types-06 = { version = "0.6", package = "geo-types" }

tokio-postgres/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106
//! | `runtime` | Enable convenience API for the connection process based on the `tokio` crate. | [tokio](https://crates.io/crates/tokio) 1.0 with the features `net` and `time` | yes |
107107
//! | `array-impls` | Enables `ToSql` and `FromSql` trait impls for arrays | - | no |
108108
//! | `with-bit-vec-0_6` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.6 | no |
109+
//! | `with-bit-vec-0_7` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.7 | no |
110+
//! | `with-bit-vec-0_8` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.8 | no |
109111
//! | `with-chrono-0_4` | Enable support for the `chrono` crate. | [chrono](https://crates.io/crates/chrono) 0.4 | no |
110112
//! | `with-eui48-0_4` | Enable support for the 0.4 version of the `eui48` crate. This is deprecated and will be removed. | [eui48](https://crates.io/crates/eui48) 0.4 | no |
111113
//! | `with-eui48-1` | Enable support for the 1.0 version of the `eui48` crate. | [eui48](https://crates.io/crates/eui48) 1.0 | no |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use bit_vec_07::BitVec;
2+
3+
use crate::types::test_type;
4+
5+
#[tokio::test]
6+
async fn test_bit_params() {
7+
let mut bv = BitVec::from_bytes(&[0b0110_1001, 0b0000_0111]);
8+
bv.pop();
9+
bv.pop();
10+
test_type(
11+
"BIT(14)",
12+
&[(Some(bv), "B'01101001000001'"), (None, "NULL")],
13+
)
14+
.await
15+
}
16+
17+
#[tokio::test]
18+
async fn test_varbit_params() {
19+
let mut bv = BitVec::from_bytes(&[0b0110_1001, 0b0000_0111]);
20+
bv.pop();
21+
bv.pop();
22+
test_type(
23+
"VARBIT",
24+
&[
25+
(Some(bv), "B'01101001000001'"),
26+
(Some(BitVec::from_bytes(&[])), "B''"),
27+
(None, "NULL"),
28+
],
29+
)
30+
.await
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use bit_vec_08::BitVec;
2+
3+
use crate::types::test_type;
4+
5+
#[tokio::test]
6+
async fn test_bit_params() {
7+
let mut bv = BitVec::from_bytes(&[0b0110_1001, 0b0000_0111]);
8+
bv.pop();
9+
bv.pop();
10+
test_type(
11+
"BIT(14)",
12+
&[(Some(bv), "B'01101001000001'"), (None, "NULL")],
13+
)
14+
.await
15+
}
16+
17+
#[tokio::test]
18+
async fn test_varbit_params() {
19+
let mut bv = BitVec::from_bytes(&[0b0110_1001, 0b0000_0111]);
20+
bv.pop();
21+
bv.pop();
22+
test_type(
23+
"VARBIT",
24+
&[
25+
(Some(bv), "B'01101001000001'"),
26+
(Some(BitVec::from_bytes(&[])), "B''"),
27+
(None, "NULL"),
28+
],
29+
)
30+
.await
31+
}

0 commit comments

Comments
 (0)