Implement ToSql & FromSql for Box<str>

This commit is contained in:
Maël Obréjan 2021-12-26 19:13:03 +01:00
parent 630f179892
commit 35f4c0aeef
No known key found for this signature in database
GPG Key ID: D6E0F3AB9B0053FB

View File

@ -584,6 +584,18 @@ impl<'a> FromSql<'a> for String {
}
}
impl<'a> FromSql<'a> for Box<str> {
fn from_sql(_: &Type, raw: &'a [u8]) -> Result<Box<str>, Box<dyn Error + Sync + Send>> {
types::text_from_sql(raw)
.map(ToString::to_string)
.map(String::into_boxed_str)
}
fn accepts(ty: &Type) -> bool {
<&str as FromSql>::accepts(ty)
}
}
impl<'a> FromSql<'a> for &'a str {
fn from_sql(_: &Type, raw: &'a [u8]) -> Result<&'a str, Box<dyn Error + Sync + Send>> {
types::text_from_sql(raw)
@ -933,6 +945,18 @@ impl ToSql for String {
to_sql_checked!();
}
impl ToSql for Box<str> {
fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
<&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 {