Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions sqlx-core/src/decode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Provides [`Decode`] for decoding values from the database.

use std::borrow::Cow;
use std::sync::Arc;

use crate::database::Database;
use crate::error::BoxDynError;

Expand Down Expand Up @@ -77,3 +80,37 @@ where
}
}
}

// implement `Decode` for Arc<T> for all SQL types
impl<'r, DB, T> Decode<'r, DB> for Arc<T>
where
DB: Database,
T: Decode<'r, DB>,
{
fn decode(value: <DB as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
Ok(Arc::new(T::decode(value)?))
}
}

// implement `Decode` for Cow<T> for all SQL types
impl<'r, DB, T> Decode<'r, DB> for Cow<'_, T>
where
DB: Database,
T: Decode<'r, DB>,
T: ToOwned<Owned = T>,
{
fn decode(value: <DB as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
Ok(Cow::Owned(T::decode(value)?))
}
}

// implement `Decode` for Box<T> for all SQL types
impl<'r, DB, T> Decode<'r, DB> for Box<T>
where
DB: Database,
T: Decode<'r, DB>,
{
fn decode(value: <DB as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
Ok(Box::new(T::decode(value)?))
}
}
116 changes: 116 additions & 0 deletions sqlx-core/src/encode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Provides [`Encode`] for encoding values for the database.

use std::borrow::Cow;
use std::mem;
use std::rc::Rc;
use std::sync::Arc;

use crate::database::Database;
use crate::error::BoxDynError;
Expand Down Expand Up @@ -129,3 +132,116 @@ macro_rules! impl_encode_for_option {
}
};
}

impl<'q, T, DB: Database> Encode<'q, DB> for Arc<T>
where
T: Encode<'q, DB>,
{
#[inline]
fn encode(self, buf: &mut <DB as Database>::ArgumentBuffer<'q>) -> Result<IsNull, BoxDynError> {
<T as Encode<DB>>::encode_by_ref(self.as_ref(), buf)
}

#[inline]
fn encode_by_ref(
&self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, BoxDynError> {
<&T as Encode<DB>>::encode(self, buf)
}

#[inline]
fn produces(&self) -> Option<DB::TypeInfo> {
(**self).produces()
}

#[inline]
fn size_hint(&self) -> usize {
(**self).size_hint()
}
}

impl<'q, T, DB: Database> Encode<'q, DB> for Cow<'_, T>
where
T: Encode<'q, DB>,
T: ToOwned<Owned = T>,
{
#[inline]
fn encode(self, buf: &mut <DB as Database>::ArgumentBuffer<'q>) -> Result<IsNull, BoxDynError> {
<T as Encode<DB>>::encode_by_ref(self.as_ref(), buf)
}

#[inline]
fn encode_by_ref(
&self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, BoxDynError> {
<&T as Encode<DB>>::encode(self, buf)
}

#[inline]
fn produces(&self) -> Option<DB::TypeInfo> {
(**self).produces()
}

#[inline]
fn size_hint(&self) -> usize {
(**self).size_hint()
}
}

impl<'q, T, DB: Database> Encode<'q, DB> for Box<T>
where
T: Encode<'q, DB>,
{
#[inline]
fn encode(self, buf: &mut <DB as Database>::ArgumentBuffer<'q>) -> Result<IsNull, BoxDynError> {
<T as Encode<DB>>::encode_by_ref(self.as_ref(), buf)
}

#[inline]
fn encode_by_ref(
&self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, BoxDynError> {
<&T as Encode<DB>>::encode(self, buf)
}

#[inline]
fn produces(&self) -> Option<DB::TypeInfo> {
(**self).produces()
}

#[inline]
fn size_hint(&self) -> usize {
(**self).size_hint()
}
}

impl<'q, T, DB: Database> Encode<'q, DB> for Rc<T>
where
T: Encode<'q, DB>,
{
#[inline]
fn encode(self, buf: &mut <DB as Database>::ArgumentBuffer<'q>) -> Result<IsNull, BoxDynError> {
<T as Encode<DB>>::encode_by_ref(self.as_ref(), buf)
}

#[inline]
fn encode_by_ref(
&self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, BoxDynError> {
<&T as Encode<DB>>::encode(self, buf)
}

#[inline]
fn produces(&self) -> Option<DB::TypeInfo> {
(**self).produces()
}

#[inline]
fn size_hint(&self) -> usize {
(**self).size_hint()
}
}
50 changes: 50 additions & 0 deletions sqlx-core/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//! To represent nullable SQL types, `Option<T>` is supported where `T` implements `Type`.
//! An `Option<T>` represents a potentially `NULL` value from SQL.

use std::{borrow::Cow, rc::Rc, sync::Arc};

use crate::database::Database;
use crate::type_info::TypeInfo;

Expand Down Expand Up @@ -238,3 +240,51 @@ impl<T: Type<DB>, DB: Database> Type<DB> for Option<T> {
ty.is_null() || <T as Type<DB>>::compatible(ty)
}
}

impl<T, DB: Database> Type<DB> for Arc<T>
where
T: Type<DB>,
T: ?Sized,
{
fn type_info() -> DB::TypeInfo {
<T as Type<DB>>::type_info()
}

fn compatible(ty: &DB::TypeInfo) -> bool {
ty.is_null() || <T as Type<DB>>::compatible(ty)
}
}

impl<T, DB: Database> Type<DB> for Cow<'_, T>
where
T: Type<DB>,
T: ToOwned<Owned = T>,
{
fn type_info() -> DB::TypeInfo {
<T as Type<DB>>::type_info()
}

fn compatible(ty: &DB::TypeInfo) -> bool {
ty.is_null() || <T as Type<DB>>::compatible(ty)
}
}

impl<T: Type<DB>, DB: Database> Type<DB> for Box<T> {
fn type_info() -> DB::TypeInfo {
<T as Type<DB>>::type_info()
}

fn compatible(ty: &DB::TypeInfo) -> bool {
ty.is_null() || <T as Type<DB>>::compatible(ty)
}
}

impl<T: Type<DB>, DB: Database> Type<DB> for Rc<T> {
fn type_info() -> DB::TypeInfo {
<T as Type<DB>>::type_info()
}

fn compatible(ty: &DB::TypeInfo) -> bool {
ty.is_null() || <T as Type<DB>>::compatible(ty)
}
}
14 changes: 14 additions & 0 deletions sqlx-mysql/src/types/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
Expand Down Expand Up @@ -83,3 +85,15 @@ impl Decode<'_, MySql> for Vec<u8> {
<&[u8] as Decode<MySql>>::decode(value).map(ToOwned::to_owned)
}
}

impl Encode<'_, MySql> for Arc<[u8]> {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> Result<IsNull, BoxDynError> {
<&[u8] as Encode<MySql>>::encode(&**self, buf)
}
}

impl Decode<'_, MySql> for Arc<[u8]> {
fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
<&[u8] as Decode<MySql>>::decode(value).map(Into::into)
}
}
13 changes: 13 additions & 0 deletions sqlx-mysql/src/types/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::protocol::text::{ColumnFlags, ColumnType};
use crate::types::Type;
use crate::{MySql, MySqlTypeInfo, MySqlValueRef};
use std::borrow::Cow;
use std::sync::Arc;

impl Type<MySql> for str {
fn type_info() -> MySqlTypeInfo {
Expand Down Expand Up @@ -114,3 +115,15 @@ impl<'r> Decode<'r, MySql> for Cow<'r, str> {
value.as_str().map(Cow::Borrowed)
}
}

impl Encode<'_, MySql> for Arc<str> {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> Result<IsNull, BoxDynError> {
<&str as Encode<MySql>>::encode(&**self, buf)
}
}

impl Decode<'_, MySql> for Arc<str> {
fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
<&str as Decode<MySql>>::decode(value).map(Into::into)
}
}
21 changes: 21 additions & 0 deletions sqlx-postgres/src/types/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use sqlx_core::bytes::Buf;
use sqlx_core::types::Text;
use std::borrow::Cow;
use std::sync::Arc;

use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
Expand Down Expand Up @@ -192,6 +193,17 @@ where
}
}

impl<'q, T> Encode<'q, Postgres> for Arc<[T]>
where
for<'a> &'a [T]: Encode<'q, Postgres>,
T: Encode<'q, Postgres>,
{
#[inline]
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
<&[T] as Encode<Postgres>>::encode_by_ref(&self.as_ref(), buf)
}
}

impl<'r, T, const N: usize> Decode<'r, Postgres> for [T; N]
where
T: for<'a> Decode<'a, Postgres> + Type<Postgres>,
Expand Down Expand Up @@ -354,3 +366,12 @@ where
}
}
}

impl<'r, T> Decode<'r, Postgres> for Arc<[T]>
where
T: for<'a> Decode<'a, Postgres> + Type<Postgres>,
{
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
<Vec<T> as Decode<Postgres>>::decode(value).map(Into::into)
}
}
23 changes: 23 additions & 0 deletions sqlx-postgres/src/types/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
Expand Down Expand Up @@ -28,6 +30,12 @@ impl PgHasArrayType for Vec<u8> {
}
}

impl PgHasArrayType for Arc<[u8]> {
fn array_type_info() -> PgTypeInfo {
<[&[u8]] as Type<Postgres>>::type_info()
}
}

impl<const N: usize> PgHasArrayType for [u8; N] {
fn array_type_info() -> PgTypeInfo {
<[&[u8]] as Type<Postgres>>::type_info()
Expand Down Expand Up @@ -60,6 +68,12 @@ impl<const N: usize> Encode<'_, Postgres> for [u8; N] {
}
}

impl Encode<'_, Postgres> for Arc<[u8]> {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
<&[u8] as Encode<Postgres>>::encode(self, buf)
}
}

impl<'r> Decode<'r, Postgres> for &'r [u8] {
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
match value.format() {
Expand Down Expand Up @@ -110,3 +124,12 @@ impl<const N: usize> Decode<'_, Postgres> for [u8; N] {
Ok(bytes)
}
}

impl Decode<'_, Postgres> for Arc<[u8]> {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(match value.format() {
PgValueFormat::Binary => value.as_bytes()?.into(),
PgValueFormat::Text => hex::decode(text_hex_decode_input(value)?)?.into(),
})
}
}
Loading
Loading