From 7699c78037e917df070c2115dd844f2f0da805bc Mon Sep 17 00:00:00 2001 From: BratSinot Date: Mon, 4 Jul 2022 12:25:58 +0200 Subject: [PATCH] Add FromSql / ToSql for smol_str::SmolStr. --- postgres-types/Cargo.toml | 2 ++ postgres-types/src/lib.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/postgres-types/Cargo.toml b/postgres-types/Cargo.toml index d8f14702..83ff718a 100644 --- a/postgres-types/Cargo.toml +++ b/postgres-types/Cargo.toml @@ -48,3 +48,5 @@ uuid-08 = { version = "0.8", package = "uuid", optional = true } uuid-1 = { version = "1.0", 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 } + +smol_str = { version = "0.1.23", default-features = false, optional = true } diff --git a/postgres-types/src/lib.rs b/postgres-types/src/lib.rs index 6f72a733..3933f34c 100644 --- a/postgres-types/src/lib.rs +++ b/postgres-types/src/lib.rs @@ -155,6 +155,7 @@ pub use pg_lsn::PgLsn; pub use crate::special::{Date, Timestamp}; use bytes::BytesMut; +use smol_str::SmolStr; // Number of seconds from 1970-01-01 to 2000-01-01 const TIME_SEC_CONVERSION: u64 = 946_684_800; @@ -629,6 +630,18 @@ impl<'a> FromSql<'a> for Box { } } +#[cfg(feature = "smol_str")] +impl<'a> FromSql<'a> for smol_str::SmolStr { + fn from_sql(ty: &Type, raw: &'a [u8]) -> Result> { + <&str as FromSql>::from_sql(ty, raw) + .map(SmolStr::from) + } + + fn accepts(ty: &Type) -> bool { + <&str as FromSql>::accepts(ty) + } +} + impl<'a> FromSql<'a> for &'a str { fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<&'a str, Box> { match *ty { @@ -1029,6 +1042,19 @@ impl ToSql for Box { to_sql_checked!(); } +#[cfg(feature = "smol_str")] +impl ToSql for smol_str::SmolStr { + fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result> { + <&str as ToSql>::to_sql(&&**self, ty, w) + } + + fn accepts(ty: &Type) -> bool { + <&str as ToSql>::accepts(ty) + } + + to_sql_checked!(); +} + macro_rules! simple_to { ($t:ty, $f:ident, $($expected:ident),+) => { impl ToSql for $t {