Skip to content

Commit 18a4e05

Browse files
authored
Merge pull request sfackler#856 from malobre/feature/box_str_sql
Implement `ToSql` & `FromSql` for `Box<str>`
2 parents 630f179 + 35f4c0a commit 18a4e05

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

postgres-types/src/lib.rs

+24
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,18 @@ impl<'a> FromSql<'a> for String {
584584
}
585585
}
586586

587+
impl<'a> FromSql<'a> for Box<str> {
588+
fn from_sql(_: &Type, raw: &'a [u8]) -> Result<Box<str>, Box<dyn Error + Sync + Send>> {
589+
types::text_from_sql(raw)
590+
.map(ToString::to_string)
591+
.map(String::into_boxed_str)
592+
}
593+
594+
fn accepts(ty: &Type) -> bool {
595+
<&str as FromSql>::accepts(ty)
596+
}
597+
}
598+
587599
impl<'a> FromSql<'a> for &'a str {
588600
fn from_sql(_: &Type, raw: &'a [u8]) -> Result<&'a str, Box<dyn Error + Sync + Send>> {
589601
types::text_from_sql(raw)
@@ -933,6 +945,18 @@ impl ToSql for String {
933945
to_sql_checked!();
934946
}
935947

948+
impl ToSql for Box<str> {
949+
fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
950+
<&str as ToSql>::to_sql(&&**self, ty, w)
951+
}
952+
953+
fn accepts(ty: &Type) -> bool {
954+
<&str as ToSql>::accepts(ty)
955+
}
956+
957+
to_sql_checked!();
958+
}
959+
936960
macro_rules! simple_to {
937961
($t:ty, $f:ident, $($expected:ident),+) => {
938962
impl ToSql for $t {

0 commit comments

Comments
 (0)