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

Implement Decode, Encode and Type for Box, Arc, Cow and Rc #3674

Open
wants to merge 2 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)
}
}
56 changes: 56 additions & 0 deletions tests/postgres/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
extern crate time_ as time;

use std::borrow::Cow;
use std::net::SocketAddr;
use std::ops::Bound;
use std::rc::Rc;
use std::sync::Arc;

use sqlx::postgres::types::{Oid, PgCiText, PgInterval, PgMoney, PgRange};
use sqlx::postgres::Postgres;
Expand Down Expand Up @@ -657,3 +660,56 @@ CREATE TEMPORARY TABLE user_login (

Ok(())
}

#[sqlx_macros::test]
async fn test_arc() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;

let user_age: Arc<i32> = sqlx::query_scalar("SELECT $1 AS age ")
.bind(Arc::new(1i32))
.fetch_one(&mut conn)
.await?;
assert!(user_age.as_ref() == &1);
Ok(())
}

#[sqlx_macros::test]
async fn test_cow() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;

let age: Cow<'_, i32> = Cow::Owned(1i32);

let user_age: Cow<'static, i32> = sqlx::query_scalar("SELECT $1 AS age ")
.bind(age)
.fetch_one(&mut conn)
.await?;

assert!(user_age.as_ref() == &1);
Ok(())
}

#[sqlx_macros::test]
async fn test_box() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;

let user_age: Box<i32> = sqlx::query_scalar("SELECT $1 AS age ")
.bind(Box::new(1))
.fetch_one(&mut conn)
.await?;

assert!(user_age.as_ref() == &1);
Ok(())
}

#[sqlx_macros::test]
async fn test_rc() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;

let user_age: i32 = sqlx::query_scalar("SELECT $1 AS age")
.bind(Rc::new(1i32))
.fetch_one(&mut conn)
.await?;

assert!(user_age == 1);
Ok(())
}
Loading