Skip to content

Commit 765395f

Browse files
authored
Merge pull request #888 from mati865/uuid-1-impls
Add conversions from Uuid 1.0
2 parents 024794a + 1d9c93d commit 765395f

File tree

9 files changed

+54
-0
lines changed

9 files changed

+54
-0
lines changed

postgres-types/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ with-geo-types-0_6 = ["geo-types-06"]
2222
with-geo-types-0_7 = ["geo-types-0_7"]
2323
with-serde_json-1 = ["serde-1", "serde_json-1"]
2424
with-uuid-0_8 = ["uuid-08"]
25+
with-uuid-1 = ["uuid-1"]
2526
with-time-0_2 = ["time-02"]
2627
with-time-0_3 = ["time-03"]
2728

@@ -42,5 +43,6 @@ geo-types-0_7 = { version = "0.7", package = "geo-types", optional = true }
4243
serde-1 = { version = "1.0", package = "serde", optional = true }
4344
serde_json-1 = { version = "1.0", package = "serde_json", optional = true }
4445
uuid-08 = { version = "0.8", package = "uuid", optional = true }
46+
uuid-1 = { version = "1.0", package = "uuid", optional = true }
4547
time-02 = { version = "0.2", package = "time", optional = true }
4648
time-03 = { version = "0.3", package = "time", default-features = false, optional = true }

postgres-types/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ mod time_02;
230230
mod time_03;
231231
#[cfg(feature = "with-uuid-0_8")]
232232
mod uuid_08;
233+
#[cfg(feature = "with-uuid-1")]
234+
mod uuid_1;
233235

234236
// The time::{date, time} macros produce compile errors if the crate package is renamed.
235237
#[cfg(feature = "with-time-0_2")]

postgres-types/src/uuid_1.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use bytes::BytesMut;
2+
use postgres_protocol::types;
3+
use std::error::Error;
4+
use uuid_1::Uuid;
5+
6+
use crate::{FromSql, IsNull, ToSql, Type};
7+
8+
impl<'a> FromSql<'a> for Uuid {
9+
fn from_sql(_: &Type, raw: &[u8]) -> Result<Uuid, Box<dyn Error + Sync + Send>> {
10+
let bytes = types::uuid_from_sql(raw)?;
11+
Ok(Uuid::from_bytes(bytes))
12+
}
13+
14+
accepts!(UUID);
15+
}
16+
17+
impl ToSql for Uuid {
18+
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
19+
types::uuid_to_sql(*self.as_bytes(), w);
20+
Ok(IsNull::No)
21+
}
22+
23+
accepts!(UUID);
24+
to_sql_checked!();
25+
}

postgres/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ with-geo-types-0_6 = ["tokio-postgres/with-geo-types-0_6"]
3030
with-geo-types-0_7 = ["tokio-postgres/with-geo-types-0_7"]
3131
with-serde_json-1 = ["tokio-postgres/with-serde_json-1"]
3232
with-uuid-0_8 = ["tokio-postgres/with-uuid-0_8"]
33+
with-uuid-1 = ["tokio-postgres/with-uuid-1"]
3334
with-time-0_2 = ["tokio-postgres/with-time-0_2"]
3435
with-time-0_3 = ["tokio-postgres/with-time-0_3"]
3536

postgres/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
//! | `with-geo-types-0_7` | Enable support for the 0.7 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.7.0) 0.7 | no |
6262
//! | `with-serde_json-1` | Enable support for the `serde_json` crate. | [serde_json](https://crates.io/crates/serde_json) 1.0 | no |
6363
//! | `with-uuid-0_8` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 0.8 | no |
64+
//! | `with-uuid-1` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 1.0 | no |
6465
//! | `with-time-0_2` | Enable support for the 0.2 version of the `time` crate. | [time](https://crates.io/crates/time/0.2.0) 0.2 | no |
6566
//! | `with-time-0_3` | Enable support for the 0.3 version of the `time` crate. | [time](https://crates.io/crates/time/0.3.0) 0.3 | no |
6667
#![warn(clippy::all, rust_2018_idioms, missing_docs)]

tokio-postgres/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ with-geo-types-0_6 = ["postgres-types/with-geo-types-0_6"]
3636
with-geo-types-0_7 = ["postgres-types/with-geo-types-0_7"]
3737
with-serde_json-1 = ["postgres-types/with-serde_json-1"]
3838
with-uuid-0_8 = ["postgres-types/with-uuid-0_8"]
39+
with-uuid-1 = ["postgres-types/with-uuid-1"]
3940
with-time-0_2 = ["postgres-types/with-time-0_2"]
4041
with-time-0_3 = ["postgres-types/with-time-0_3"]
4142

@@ -70,5 +71,6 @@ geo-types-07 = { version = "0.7", package = "geo-types" }
7071
serde-1 = { version = "1.0", package = "serde" }
7172
serde_json-1 = { version = "1.0", package = "serde_json" }
7273
uuid-08 = { version = "0.8", package = "uuid" }
74+
uuid-1 = { version = "1.0", package = "uuid" }
7375
time-02 = { version = "0.2", package = "time" }
7476
time-03 = { version = "0.3", package = "time", features = ["parsing"] }

tokio-postgres/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
//! | `with-geo-types-0_7` | Enable support for the 0.7 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.7.0) 0.7 | no |
113113
//! | `with-serde_json-1` | Enable support for the `serde_json` crate. | [serde_json](https://crates.io/crates/serde_json) 1.0 | no |
114114
//! | `with-uuid-0_8` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 0.8 | no |
115+
//! | `with-uuid-1` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 1.0 | no |
115116
//! | `with-time-0_2` | Enable support for the 0.2 version of the `time` crate. | [time](https://crates.io/crates/time/0.2.0) 0.2 | no |
116117
//! | `with-time-0_3` | Enable support for the 0.3 version of the `time` crate. | [time](https://crates.io/crates/time/0.3.0) 0.3 | no |
117118
#![doc(html_root_url = "https://docs.rs/tokio-postgres/0.7")]

tokio-postgres/tests/test/types/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ mod time_02;
3333
mod time_03;
3434
#[cfg(feature = "with-uuid-0_8")]
3535
mod uuid_08;
36+
#[cfg(feature = "with-uuid-1")]
37+
mod uuid_1;
3638

3739
async fn test_type<T, S>(sql_type: &str, checks: &[(T, S)])
3840
where
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use uuid_1::Uuid;
2+
3+
use crate::types::test_type;
4+
5+
#[tokio::test]
6+
async fn test_uuid_params() {
7+
test_type(
8+
"UUID",
9+
&[
10+
(
11+
Some(Uuid::parse_str("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11").unwrap()),
12+
"'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'",
13+
),
14+
(None, "NULL"),
15+
],
16+
)
17+
.await
18+
}

0 commit comments

Comments
 (0)