diff --git a/postgres-types/Cargo.toml b/postgres-types/Cargo.toml index d6527f3b9..df97575e9 100644 --- a/postgres-types/Cargo.toml +++ b/postgres-types/Cargo.toml @@ -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"] @@ -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 } diff --git a/postgres-types/src/bit_vec_07.rs b/postgres-types/src/bit_vec_07.rs new file mode 100644 index 000000000..44a3d1c4e --- /dev/null +++ b/postgres-types/src/bit_vec_07.rs @@ -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> { + 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> { + types::varbit_to_sql(self.len(), self.to_bytes().into_iter(), out)?; + Ok(IsNull::No) + } + + accepts!(BIT, VARBIT); + to_sql_checked!(); +} diff --git a/postgres-types/src/bit_vec_08.rs b/postgres-types/src/bit_vec_08.rs new file mode 100644 index 000000000..4c03bc424 --- /dev/null +++ b/postgres-types/src/bit_vec_08.rs @@ -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> { + 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> { + types::varbit_to_sql(self.len(), self.to_bytes().into_iter(), out)?; + Ok(IsNull::No) + } + + accepts!(BIT, VARBIT); + to_sql_checked!(); +} diff --git a/postgres-types/src/lib.rs b/postgres-types/src/lib.rs index 51137b6b4..34ace21da 100644 --- a/postgres-types/src/lib.rs +++ b/postgres-types/src/lib.rs @@ -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")] diff --git a/postgres/Cargo.toml b/postgres/Cargo.toml index 06f6c7ff0..41b4428a8 100644 --- a/postgres/Cargo.toml +++ b/postgres/Cargo.toml @@ -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"] diff --git a/postgres/src/lib.rs b/postgres/src/lib.rs index ddf1609ad..255a7a171 100644 --- a/postgres/src/lib.rs +++ b/postgres/src/lib.rs @@ -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 | diff --git a/tokio-postgres/Cargo.toml b/tokio-postgres/Cargo.toml index cd2b699d4..5e8b126d1 100644 --- a/tokio-postgres/Cargo.toml +++ b/tokio-postgres/Cargo.toml @@ -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"] @@ -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" } diff --git a/tokio-postgres/src/lib.rs b/tokio-postgres/src/lib.rs index ec843d511..d6da762b2 100644 --- a/tokio-postgres/src/lib.rs +++ b/tokio-postgres/src/lib.rs @@ -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 | diff --git a/tokio-postgres/tests/test/types/bit_vec_07.rs b/tokio-postgres/tests/test/types/bit_vec_07.rs new file mode 100644 index 000000000..5ea4a00bb --- /dev/null +++ b/tokio-postgres/tests/test/types/bit_vec_07.rs @@ -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 +} diff --git a/tokio-postgres/tests/test/types/bit_vec_08.rs b/tokio-postgres/tests/test/types/bit_vec_08.rs new file mode 100644 index 000000000..f01de9b75 --- /dev/null +++ b/tokio-postgres/tests/test/types/bit_vec_08.rs @@ -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 +} diff --git a/tokio-postgres/tests/test/types/mod.rs b/tokio-postgres/tests/test/types/mod.rs index 41c4a8899..25411939b 100644 --- a/tokio-postgres/tests/test/types/mod.rs +++ b/tokio-postgres/tests/test/types/mod.rs @@ -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")]