Skip to content

Commit

Permalink
smol_str 0.3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
novacrazy committed Sep 7, 2024
1 parent f52f821 commit 798f81b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions postgres-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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 }
2 changes: 2 additions & 0 deletions postgres-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
31 changes: 31 additions & 0 deletions postgres-types/src/smol_str_03.rs
Original file line number Diff line number Diff line change
@@ -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<Self, Box<dyn Error + Sync + Send>> {
<&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<IsNull, Box<dyn Error + Sync + Send>> {
<&str as ToSql>::to_sql(&self.as_str(), ty, out)
}

fn accepts(ty: &Type) -> bool {
<&str as ToSql>::accepts(ty)
}

to_sql_checked!();
}

0 comments on commit 798f81b

Please sign in to comment.