Skip to content

Commit 7699c78

Browse files
committed
Add FromSql / ToSql for smol_str::SmolStr.
1 parent 0984ab1 commit 7699c78

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

postgres-types/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@ uuid-08 = { version = "0.8", package = "uuid", optional = true }
4848
uuid-1 = { version = "1.0", package = "uuid", optional = true }
4949
time-02 = { version = "0.2", package = "time", optional = true }
5050
time-03 = { version = "0.3", package = "time", default-features = false, optional = true }
51+
52+
smol_str = { version = "0.1.23", default-features = false, optional = true }

postgres-types/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ pub use pg_lsn::PgLsn;
155155

156156
pub use crate::special::{Date, Timestamp};
157157
use bytes::BytesMut;
158+
use smol_str::SmolStr;
158159

159160
// Number of seconds from 1970-01-01 to 2000-01-01
160161
const TIME_SEC_CONVERSION: u64 = 946_684_800;
@@ -629,6 +630,18 @@ impl<'a> FromSql<'a> for Box<str> {
629630
}
630631
}
631632

633+
#[cfg(feature = "smol_str")]
634+
impl<'a> FromSql<'a> for smol_str::SmolStr {
635+
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<SmolStr, Box<dyn Error + Sync + Send>> {
636+
<&str as FromSql>::from_sql(ty, raw)
637+
.map(SmolStr::from)
638+
}
639+
640+
fn accepts(ty: &Type) -> bool {
641+
<&str as FromSql>::accepts(ty)
642+
}
643+
}
644+
632645
impl<'a> FromSql<'a> for &'a str {
633646
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<&'a str, Box<dyn Error + Sync + Send>> {
634647
match *ty {
@@ -1029,6 +1042,19 @@ impl ToSql for Box<str> {
10291042
to_sql_checked!();
10301043
}
10311044

1045+
#[cfg(feature = "smol_str")]
1046+
impl ToSql for smol_str::SmolStr {
1047+
fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
1048+
<&str as ToSql>::to_sql(&&**self, ty, w)
1049+
}
1050+
1051+
fn accepts(ty: &Type) -> bool {
1052+
<&str as ToSql>::accepts(ty)
1053+
}
1054+
1055+
to_sql_checked!();
1056+
}
1057+
10321058
macro_rules! simple_to {
10331059
($t:ty, $f:ident, $($expected:ident),+) => {
10341060
impl ToSql for $t {

0 commit comments

Comments
 (0)