Skip to content

Commit 676e11e

Browse files
committed
feat implement Encode,Type for Rc
1 parent 3a73ece commit 676e11e

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

sqlx-core/src/encode.rs

+29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::borrow::Cow;
44
use std::mem;
5+
use std::rc::Rc;
56
use std::sync::Arc;
67

78
use crate::database::Database;
@@ -216,3 +217,31 @@ where
216217
(**self).size_hint()
217218
}
218219
}
220+
221+
impl<'q, T, DB: Database> Encode<'q, DB> for Rc<T>
222+
where
223+
T: Encode<'q, DB>,
224+
{
225+
#[inline]
226+
fn encode(self, buf: &mut <DB as Database>::ArgumentBuffer<'q>) -> Result<IsNull, BoxDynError> {
227+
<T as Encode<DB>>::encode_by_ref(self.as_ref(), buf)
228+
}
229+
230+
#[inline]
231+
fn encode_by_ref(
232+
&self,
233+
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
234+
) -> Result<IsNull, BoxDynError> {
235+
<&T as Encode<DB>>::encode(self, buf)
236+
}
237+
238+
#[inline]
239+
fn produces(&self) -> Option<DB::TypeInfo> {
240+
(**self).produces()
241+
}
242+
243+
#[inline]
244+
fn size_hint(&self) -> usize {
245+
(**self).size_hint()
246+
}
247+
}

sqlx-core/src/types/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! To represent nullable SQL types, `Option<T>` is supported where `T` implements `Type`.
1818
//! An `Option<T>` represents a potentially `NULL` value from SQL.
1919
20-
use std::{borrow::Cow, sync::Arc};
20+
use std::{borrow::Cow, rc::Rc, sync::Arc};
2121

2222
use crate::database::Database;
2323
use crate::type_info::TypeInfo;
@@ -278,3 +278,13 @@ impl<T: Type<DB>, DB: Database> Type<DB> for Box<T> {
278278
ty.is_null() || <T as Type<DB>>::compatible(ty)
279279
}
280280
}
281+
282+
impl<T: Type<DB>, DB: Database> Type<DB> for Rc<T> {
283+
fn type_info() -> DB::TypeInfo {
284+
<T as Type<DB>>::type_info()
285+
}
286+
287+
fn compatible(ty: &DB::TypeInfo) -> bool {
288+
ty.is_null() || <T as Type<DB>>::compatible(ty)
289+
}
290+
}

tests/postgres/types.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extern crate time_ as time;
33
use std::borrow::Cow;
44
use std::net::SocketAddr;
55
use std::ops::Bound;
6+
use std::rc::Rc;
67
use std::sync::Arc;
78

89
use sqlx::postgres::types::{Oid, PgCiText, PgInterval, PgMoney, PgRange};
@@ -664,7 +665,7 @@ CREATE TEMPORARY TABLE user_login (
664665
async fn test_arc() -> anyhow::Result<()> {
665666
let mut conn = new::<Postgres>().await?;
666667

667-
let user_age: Arc<i32> = sqlx::query_scalar("select $1 as age ")
668+
let user_age: Arc<i32> = sqlx::query_scalar("SELECT $1 AS age ")
668669
.bind(Arc::new(1i32))
669670
.fetch_one(&mut conn)
670671
.await?;
@@ -678,7 +679,7 @@ async fn test_cow() -> anyhow::Result<()> {
678679

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

681-
let user_age: Cow<'static, i32> = sqlx::query_scalar("select $1 as age ")
682+
let user_age: Cow<'static, i32> = sqlx::query_scalar("SELECT $1 AS age ")
682683
.bind(age)
683684
.fetch_one(&mut conn)
684685
.await?;
@@ -691,11 +692,24 @@ async fn test_cow() -> anyhow::Result<()> {
691692
async fn test_box() -> anyhow::Result<()> {
692693
let mut conn = new::<Postgres>().await?;
693694

694-
let user_age: Box<i32> = sqlx::query_scalar("select $1 as age ")
695+
let user_age: Box<i32> = sqlx::query_scalar("SELECT $1 AS age ")
695696
.bind(Box::new(1))
696697
.fetch_one(&mut conn)
697698
.await?;
698699

699700
assert!(user_age.as_ref() == &1);
700701
Ok(())
701702
}
703+
704+
#[sqlx_macros::test]
705+
async fn test_rc() -> anyhow::Result<()> {
706+
let mut conn = new::<Postgres>().await?;
707+
708+
let user_age: i32 = sqlx::query_scalar("SELECT $1 AS age")
709+
.bind(Rc::new(1i32))
710+
.fetch_one(&mut conn)
711+
.await?;
712+
713+
assert!(user_age == 1);
714+
Ok(())
715+
}

0 commit comments

Comments
 (0)