From 798f81be481de2bb3bd805876e62e9a6ee8543a8 Mon Sep 17 00:00:00 2001 From: novacrazy Date: Fri, 6 Sep 2024 21:31:09 -0500 Subject: [PATCH] smol_str 0.3 support --- postgres-types/Cargo.toml | 2 ++ postgres-types/src/lib.rs | 2 ++ postgres-types/src/smol_str_03.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 postgres-types/src/smol_str_03.rs diff --git a/postgres-types/Cargo.toml b/postgres-types/Cargo.toml index 90a9c4fba..c9fd10a9b 100644 --- a/postgres-types/Cargo.toml +++ b/postgres-types/Cargo.toml @@ -25,6 +25,7 @@ with-jiff-0_1 = ["jiff-01"] with-serde_json-1 = ["serde-1", "serde_json-1"] with-smol_str-01 = ["smol_str-01"] with-smol_str-02 = ["smol_str-02"] +with-smol_str-03 = ["smol_str-03"] with-uuid-0_8 = ["uuid-08"] with-uuid-1 = ["uuid-1"] with-time-0_2 = ["time-02"] @@ -57,3 +58,4 @@ time-02 = { version = "0.2", package = "time", optional = true } time-03 = { version = "0.3", package = "time", default-features = false, optional = true } smol_str-01 = { version = "0.1.23", package = "smol_str", default-features = false, optional = true } smol_str-02 = { version = "0.2", package = "smol_str", default-features = false, optional = true } +smol_str-03 = { version = "0.3", package = "smol_str", default-features = false, optional = true } diff --git a/postgres-types/src/lib.rs b/postgres-types/src/lib.rs index 4a643b9a9..f61dec686 100644 --- a/postgres-types/src/lib.rs +++ b/postgres-types/src/lib.rs @@ -284,6 +284,8 @@ mod serde_json_1; mod smol_str_01; #[cfg(feature = "with-smol_str-02")] mod smol_str_02; +#[cfg(feature = "with-smol_str-03")] +mod smol_str_03; #[cfg(feature = "with-time-0_2")] mod time_02; #[cfg(feature = "with-time-0_3")] diff --git a/postgres-types/src/smol_str_03.rs b/postgres-types/src/smol_str_03.rs new file mode 100644 index 000000000..b746bb245 --- /dev/null +++ b/postgres-types/src/smol_str_03.rs @@ -0,0 +1,31 @@ +use bytes::BytesMut; +use smol_str_03::SmolStr; +use std::error::Error; + +use crate::{FromSql, IsNull, ToSql, Type}; + +impl<'a> FromSql<'a> for SmolStr { + fn from_sql(ty: &Type, raw: &'a [u8]) -> Result> { + <&str as FromSql>::from_sql(ty, raw).map(SmolStr::new) + } + + fn accepts(ty: &Type) -> bool { + <&str as FromSql>::accepts(ty) + } +} + +impl ToSql for SmolStr { + fn to_sql( + &self, + ty: &Type, + out: &mut BytesMut, + ) -> Result> { + <&str as ToSql>::to_sql(&self.as_str(), ty, out) + } + + fn accepts(ty: &Type) -> bool { + <&str as ToSql>::accepts(ty) + } + + to_sql_checked!(); +}