feat: implement Encode, Decode, Type for Arc<str> and Arc<[u8]> (and Rc equivalents) (#3675)

* implement Encode, Decode, Type for Arc<str> and Arc<[u8]> (and Rc equivalents)

* sqlx-sqlite: Remove clone in Encode impl

* sqlx-sqlite: Remove unnecessary impls
This commit is contained in:
Joey de Waal
2025-07-05 02:58:33 +02:00
committed by GitHub
parent 0f891a3c56
commit 60f67dbc39
10 changed files with 93 additions and 63 deletions

View File

@@ -1,4 +1,6 @@
use std::borrow::Cow;
use std::rc::Rc;
use std::sync::Arc;
use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
@@ -44,12 +46,6 @@ impl Encode<'_, Postgres> for &'_ [u8] {
}
}
impl Encode<'_, Postgres> for Box<[u8]> {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
<&[u8] as Encode<Postgres>>::encode(self.as_ref(), buf)
}
}
impl Encode<'_, Postgres> for Vec<u8> {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
<&[u8] as Encode<Postgres>>::encode(self, buf)
@@ -104,8 +100,7 @@ impl<const N: usize> Decode<'_, Postgres> for [u8; N] {
}
}
impl Encode<'_, Postgres> for Cow<'_, [u8]> {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
<&[u8] as Encode<Postgres>>::encode(self.as_ref(), buf)
}
}
forward_encode_impl!(Arc<[u8]>, &[u8], Postgres);
forward_encode_impl!(Rc<[u8]>, &[u8], Postgres);
forward_encode_impl!(Box<[u8]>, &[u8], Postgres);
forward_encode_impl!(Cow<'_, [u8]>, &[u8], Postgres);

View File

@@ -5,6 +5,8 @@ use crate::types::array_compatible;
use crate::types::Type;
use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef, Postgres};
use std::borrow::Cow;
use std::rc::Rc;
use std::sync::Arc;
impl Type<Postgres> for str {
fn type_info() -> PgTypeInfo {
@@ -82,27 +84,6 @@ impl Encode<'_, Postgres> for &'_ str {
}
}
impl Encode<'_, Postgres> for Cow<'_, str> {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
match self {
Cow::Borrowed(str) => <&str as Encode<Postgres>>::encode(*str, buf),
Cow::Owned(str) => <&str as Encode<Postgres>>::encode(&**str, buf),
}
}
}
impl Encode<'_, Postgres> for Box<str> {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
<&str as Encode<Postgres>>::encode(&**self, buf)
}
}
impl Encode<'_, Postgres> for String {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
<&str as Encode<Postgres>>::encode(&**self, buf)
}
}
impl<'r> Decode<'r, Postgres> for &'r str {
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
value.as_str()
@@ -114,3 +95,9 @@ impl Decode<'_, Postgres> for String {
Ok(value.as_str()?.to_owned())
}
}
forward_encode_impl!(Arc<str>, &str, Postgres);
forward_encode_impl!(Rc<str>, &str, Postgres);
forward_encode_impl!(Cow<'_, str>, &str, Postgres);
forward_encode_impl!(Box<str>, &str, Postgres);
forward_encode_impl!(String, &str, Postgres);