Skip to content

Commit a3c0e52

Browse files
authored
Merge pull request #919 from BratSinot/smol_str
Add FromSql / ToSql for smol_str::SmolStr.
2 parents db4c65e + 9ae23bd commit a3c0e52

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-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-01 = { version = "0.1.23", package = "smol_str", default-features = false, optional = true }

postgres-types/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ mod geo_types_06;
224224
mod geo_types_07;
225225
#[cfg(feature = "with-serde_json-1")]
226226
mod serde_json_1;
227+
#[cfg(feature = "smol_str-01")]
228+
mod smol_str_01;
227229
#[cfg(feature = "with-time-0_2")]
228230
mod time_02;
229231
#[cfg(feature = "with-time-0_3")]
@@ -443,6 +445,9 @@ impl WrongType {
443445
/// | `eui48::MacAddress` | MACADDR |
444446
/// | `cidr::InetCidr` | CIDR |
445447
/// | `cidr::InetAddr` | INET |
448+
/// | `smol_str::SmolStr` | VARCHAR, CHAR(n), TEXT, CITEXT, |
449+
/// | | NAME, UNKNOWN, LTREE, LQUERY, |
450+
/// | | LTXTQUERY |
446451
///
447452
/// # Nullability
448453
///

postgres-types/src/smol_str_01.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use bytes::BytesMut;
2+
use smol_str_01::SmolStr;
3+
use std::error::Error;
4+
5+
use crate::{FromSql, IsNull, ToSql, Type};
6+
7+
impl<'a> FromSql<'a> for SmolStr {
8+
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<SmolStr, Box<dyn Error + Sync + Send>> {
9+
<&str as FromSql>::from_sql(ty, raw).map(SmolStr::from)
10+
}
11+
12+
fn accepts(ty: &Type) -> bool {
13+
<&str as FromSql>::accepts(ty)
14+
}
15+
}
16+
17+
impl ToSql for SmolStr {
18+
fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
19+
<&str as ToSql>::to_sql(&&**self, ty, w)
20+
}
21+
22+
fn accepts(ty: &Type) -> bool {
23+
<&str as ToSql>::accepts(ty)
24+
}
25+
26+
to_sql_checked!();
27+
}

0 commit comments

Comments
 (0)