Skip to content

Add support for bit-vec 0.7 & 0.8 #1221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions postgres-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ derive = ["postgres-derive"]
array-impls = ["array-init"]
js = ["postgres-protocol/js"]
with-bit-vec-0_6 = ["bit-vec-06"]
with-bit-vec-0_7 = ["bit-vec-07"]
with-bit-vec-0_8 = ["bit-vec-08"]
with-cidr-0_2 = ["cidr-02"]
with-cidr-0_3 = ["cidr-03"]
with-chrono-0_4 = ["chrono-04"]
Expand All @@ -39,6 +41,8 @@ postgres-derive = { version = "0.4.6", optional = true, path = "../postgres-deri

array-init = { version = "2", optional = true }
bit-vec-06 = { version = "0.6", package = "bit-vec", optional = true }
bit-vec-07 = { version = "0.7", package = "bit-vec", optional = true }
bit-vec-08 = { version = "0.8", package = "bit-vec", optional = true }
chrono-04 = { version = "0.4.16", package = "chrono", default-features = false, features = [
"clock",
], optional = true }
Expand Down
30 changes: 30 additions & 0 deletions postgres-types/src/bit_vec_07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use bit_vec_07::BitVec;
use bytes::BytesMut;
use postgres_protocol::types;
use std::error::Error;

use crate::{FromSql, IsNull, ToSql, Type};

impl<'a> FromSql<'a> for BitVec {
fn from_sql(_: &Type, raw: &[u8]) -> Result<BitVec, Box<dyn Error + Sync + Send>> {
let varbit = types::varbit_from_sql(raw)?;
let mut bitvec = BitVec::from_bytes(varbit.bytes());
while bitvec.len() > varbit.len() {
bitvec.pop();
}

Ok(bitvec)
}

accepts!(BIT, VARBIT);
}

impl ToSql for BitVec {
fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
types::varbit_to_sql(self.len(), self.to_bytes().into_iter(), out)?;
Ok(IsNull::No)
}

accepts!(BIT, VARBIT);
to_sql_checked!();
}
30 changes: 30 additions & 0 deletions postgres-types/src/bit_vec_08.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use bit_vec_08::BitVec;
use bytes::BytesMut;
use postgres_protocol::types;
use std::error::Error;

use crate::{FromSql, IsNull, ToSql, Type};

impl<'a> FromSql<'a> for BitVec {
fn from_sql(_: &Type, raw: &[u8]) -> Result<BitVec, Box<dyn Error + Sync + Send>> {
let varbit = types::varbit_from_sql(raw)?;
let mut bitvec = BitVec::from_bytes(varbit.bytes());
while bitvec.len() > varbit.len() {
bitvec.pop();
}

Ok(bitvec)
}

accepts!(BIT, VARBIT);
}

impl ToSql for BitVec {
fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
types::varbit_to_sql(self.len(), self.to_bytes().into_iter(), out)?;
Ok(IsNull::No)
}

accepts!(BIT, VARBIT);
to_sql_checked!();
}
4 changes: 4 additions & 0 deletions postgres-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ where

#[cfg(feature = "with-bit-vec-0_6")]
mod bit_vec_06;
#[cfg(feature = "with-bit-vec-0_7")]
mod bit_vec_07;
#[cfg(feature = "with-bit-vec-0_8")]
mod bit_vec_08;
#[cfg(feature = "with-chrono-0_4")]
mod chrono_04;
#[cfg(feature = "with-cidr-0_2")]
Expand Down
2 changes: 2 additions & 0 deletions postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ circle-ci = { repository = "sfackler/rust-postgres" }
[features]
array-impls = ["tokio-postgres/array-impls"]
with-bit-vec-0_6 = ["tokio-postgres/with-bit-vec-0_6"]
with-bit-vec-0_7 = ["tokio-postgres/with-bit-vec-0_7"]
with-bit-vec-0_8 = ["tokio-postgres/with-bit-vec-0_8"]
with-chrono-0_4 = ["tokio-postgres/with-chrono-0_4"]
with-cidr-0_2 = ["tokio-postgres/with-cidr-0_2"]
with-cidr-0_3 = ["tokio-postgres/with-cidr-0_3"]
Expand Down
2 changes: 2 additions & 0 deletions postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
//! | Feature | Description | Extra dependencies | Default |
//! | ------- | ----------- | ------------------ | ------- |
//! | `with-bit-vec-0_6` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.6 | no |
//! | `with-bit-vec-0_7` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.7 | no |
//! | `with-bit-vec-0_8` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.8 | no |
//! | `with-chrono-0_4` | Enable support for the `chrono` crate. | [chrono](https://crates.io/crates/chrono) 0.4 | no |
//! | `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 |
//! | `with-eui48-1` | Enable support for the 1.0 version of the `eui48` crate. | [eui48](https://crates.io/crates/eui48) 1.0 | no |
Expand Down
4 changes: 4 additions & 0 deletions tokio-postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ runtime = ["tokio/net", "tokio/time"]

array-impls = ["postgres-types/array-impls"]
with-bit-vec-0_6 = ["postgres-types/with-bit-vec-0_6"]
with-bit-vec-0_7 = ["postgres-types/with-bit-vec-0_7"]
with-bit-vec-0_8 = ["postgres-types/with-bit-vec-0_8"]
with-chrono-0_4 = ["postgres-types/with-chrono-0_4"]
with-cidr-0_2 = ["postgres-types/with-cidr-0_2"]
with-cidr-0_3 = ["postgres-types/with-cidr-0_3"]
Expand Down Expand Up @@ -81,6 +83,8 @@ tokio = { version = "1.0", features = [
] }

bit-vec-06 = { version = "0.6", package = "bit-vec" }
bit-vec-07 = { version = "0.7", package = "bit-vec" }
bit-vec-08 = { version = "0.8", package = "bit-vec" }
chrono-04 = { version = "0.4", package = "chrono", default-features = false }
eui48-1 = { version = "1.0", package = "eui48", default-features = false }
geo-types-06 = { version = "0.6", package = "geo-types" }
Expand Down
2 changes: 2 additions & 0 deletions tokio-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@
//! | `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 |
//! | `array-impls` | Enables `ToSql` and `FromSql` trait impls for arrays | - | no |
//! | `with-bit-vec-0_6` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.6 | no |
//! | `with-bit-vec-0_7` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.7 | no |
//! | `with-bit-vec-0_8` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.8 | no |
//! | `with-chrono-0_4` | Enable support for the `chrono` crate. | [chrono](https://crates.io/crates/chrono) 0.4 | no |
//! | `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 |
//! | `with-eui48-1` | Enable support for the 1.0 version of the `eui48` crate. | [eui48](https://crates.io/crates/eui48) 1.0 | no |
Expand Down
31 changes: 31 additions & 0 deletions tokio-postgres/tests/test/types/bit_vec_07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use bit_vec_07::BitVec;

use crate::types::test_type;

#[tokio::test]
async fn test_bit_params() {
let mut bv = BitVec::from_bytes(&[0b0110_1001, 0b0000_0111]);
bv.pop();
bv.pop();
test_type(
"BIT(14)",
&[(Some(bv), "B'01101001000001'"), (None, "NULL")],
)
.await
}

#[tokio::test]
async fn test_varbit_params() {
let mut bv = BitVec::from_bytes(&[0b0110_1001, 0b0000_0111]);
bv.pop();
bv.pop();
test_type(
"VARBIT",
&[
(Some(bv), "B'01101001000001'"),
(Some(BitVec::from_bytes(&[])), "B''"),
(None, "NULL"),
],
)
.await
}
31 changes: 31 additions & 0 deletions tokio-postgres/tests/test/types/bit_vec_08.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use bit_vec_08::BitVec;

use crate::types::test_type;

#[tokio::test]
async fn test_bit_params() {
let mut bv = BitVec::from_bytes(&[0b0110_1001, 0b0000_0111]);
bv.pop();
bv.pop();
test_type(
"BIT(14)",
&[(Some(bv), "B'01101001000001'"), (None, "NULL")],
)
.await
}

#[tokio::test]
async fn test_varbit_params() {
let mut bv = BitVec::from_bytes(&[0b0110_1001, 0b0000_0111]);
bv.pop();
bv.pop();
test_type(
"VARBIT",
&[
(Some(bv), "B'01101001000001'"),
(Some(BitVec::from_bytes(&[])), "B''"),
(None, "NULL"),
],
)
.await
}
4 changes: 4 additions & 0 deletions tokio-postgres/tests/test/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ use bytes::BytesMut;

#[cfg(feature = "with-bit-vec-0_6")]
mod bit_vec_06;
#[cfg(feature = "with-bit-vec-0_7")]
mod bit_vec_07;
#[cfg(feature = "with-bit-vec-0_8")]
mod bit_vec_08;
#[cfg(feature = "with-chrono-0_4")]
mod chrono_04;
#[cfg(feature = "with-eui48-1")]
Expand Down