Skip to content

Commit

Permalink
Add conversions from Uuid 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mati865 committed Apr 20, 2022
1 parent 024794a commit 8f6fbe3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions postgres-types/Cargo.toml
Expand Up @@ -22,6 +22,7 @@ with-geo-types-0_6 = ["geo-types-06"]
with-geo-types-0_7 = ["geo-types-0_7"]
with-serde_json-1 = ["serde-1", "serde_json-1"]
with-uuid-0_8 = ["uuid-08"]
with-uuid-1 = ["uuid-1"]
with-time-0_2 = ["time-02"]
with-time-0_3 = ["time-03"]

Expand All @@ -42,5 +43,6 @@ geo-types-0_7 = { version = "0.7", package = "geo-types", optional = true }
serde-1 = { version = "1.0", package = "serde", optional = true }
serde_json-1 = { version = "1.0", package = "serde_json", optional = true }
uuid-08 = { version = "0.8", package = "uuid", optional = true }
uuid-1 = { version = "1", package = "uuid", optional = true }
time-02 = { version = "0.2", package = "time", optional = true }
time-03 = { version = "0.3", package = "time", default-features = false, optional = true }
2 changes: 2 additions & 0 deletions postgres-types/src/lib.rs
Expand Up @@ -230,6 +230,8 @@ mod time_02;
mod time_03;
#[cfg(feature = "with-uuid-0_8")]
mod uuid_08;
#[cfg(feature = "with-uuid-1")]
mod uuid_1;

// The time::{date, time} macros produce compile errors if the crate package is renamed.
#[cfg(feature = "with-time-0_2")]
Expand Down
25 changes: 25 additions & 0 deletions postgres-types/src/uuid_1.rs
@@ -0,0 +1,25 @@
use bytes::BytesMut;
use postgres_protocol::types;
use std::error::Error;
use uuid_1::Uuid;

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

impl<'a> FromSql<'a> for Uuid {
fn from_sql(_: &Type, raw: &[u8]) -> Result<Uuid, Box<dyn Error + Sync + Send>> {
let bytes = types::uuid_from_sql(raw)?;
Ok(Uuid::from_bytes(bytes))
}

accepts!(UUID);
}

impl ToSql for Uuid {
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
types::uuid_to_sql(*self.as_bytes(), w);
Ok(IsNull::No)
}

accepts!(UUID);
to_sql_checked!();
}

0 comments on commit 8f6fbe3

Please sign in to comment.