Skip to content

Commit f2930a6

Browse files
committed
chore: use macro's
1 parent 582fe5b commit f2930a6

File tree

4 files changed

+56
-122
lines changed

4 files changed

+56
-122
lines changed

sqlx-core/src/encode.rs

+34-79
Original file line numberDiff line numberDiff line change
@@ -133,34 +133,45 @@ macro_rules! impl_encode_for_option {
133133
};
134134
}
135135

136-
impl<'q, T, DB: Database> Encode<'q, DB> for Arc<T>
137-
where
138-
T: Encode<'q, DB>,
139-
{
140-
#[inline]
141-
fn encode(self, buf: &mut <DB as Database>::ArgumentBuffer<'q>) -> Result<IsNull, BoxDynError> {
142-
<T as Encode<DB>>::encode_by_ref(self.as_ref(), buf)
143-
}
136+
macro_rules! impl_encode_for_smartpointer {
137+
($smart_pointer:ty) => {
138+
impl<'q, T, DB: Database> Encode<'q, DB> for $smart_pointer
139+
where
140+
T: Encode<'q, DB>,
141+
{
142+
#[inline]
143+
fn encode(
144+
self,
145+
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
146+
) -> Result<IsNull, BoxDynError> {
147+
<T as Encode<DB>>::encode_by_ref(self.as_ref(), buf)
148+
}
144149

145-
#[inline]
146-
fn encode_by_ref(
147-
&self,
148-
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
149-
) -> Result<IsNull, BoxDynError> {
150-
<&T as Encode<DB>>::encode(self, buf)
151-
}
150+
#[inline]
151+
fn encode_by_ref(
152+
&self,
153+
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
154+
) -> Result<IsNull, BoxDynError> {
155+
<&T as Encode<DB>>::encode(self, buf)
156+
}
152157

153-
#[inline]
154-
fn produces(&self) -> Option<DB::TypeInfo> {
155-
(**self).produces()
156-
}
158+
#[inline]
159+
fn produces(&self) -> Option<DB::TypeInfo> {
160+
(**self).produces()
161+
}
157162

158-
#[inline]
159-
fn size_hint(&self) -> usize {
160-
(**self).size_hint()
161-
}
163+
#[inline]
164+
fn size_hint(&self) -> usize {
165+
(**self).size_hint()
166+
}
167+
}
168+
};
162169
}
163170

171+
impl_encode_for_smartpointer!(Arc<T>);
172+
impl_encode_for_smartpointer!(Box<T>);
173+
impl_encode_for_smartpointer!(Rc<T>);
174+
164175
impl<'q, T, DB: Database> Encode<'q, DB> for Cow<'_, T>
165176
where
166177
T: Encode<'q, DB>,
@@ -189,59 +200,3 @@ where
189200
(**self).size_hint()
190201
}
191202
}
192-
193-
impl<'q, T, DB: Database> Encode<'q, DB> for Box<T>
194-
where
195-
T: Encode<'q, DB>,
196-
{
197-
#[inline]
198-
fn encode(self, buf: &mut <DB as Database>::ArgumentBuffer<'q>) -> Result<IsNull, BoxDynError> {
199-
<T as Encode<DB>>::encode_by_ref(self.as_ref(), buf)
200-
}
201-
202-
#[inline]
203-
fn encode_by_ref(
204-
&self,
205-
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
206-
) -> Result<IsNull, BoxDynError> {
207-
<&T as Encode<DB>>::encode(self, buf)
208-
}
209-
210-
#[inline]
211-
fn produces(&self) -> Option<DB::TypeInfo> {
212-
(**self).produces()
213-
}
214-
215-
#[inline]
216-
fn size_hint(&self) -> usize {
217-
(**self).size_hint()
218-
}
219-
}
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

+20-32
Original file line numberDiff line numberDiff line change
@@ -244,20 +244,28 @@ impl<T: Type<DB>, DB: Database> Type<DB> for Option<T> {
244244
}
245245
}
246246

247-
impl<T, DB: Database> Type<DB> for Arc<T>
248-
where
249-
T: Type<DB>,
250-
T: ?Sized,
251-
{
252-
fn type_info() -> DB::TypeInfo {
253-
<T as Type<DB>>::type_info()
254-
}
247+
macro_rules! impl_type_for_smartpointer {
248+
($smart_pointer:ty) => {
249+
impl<T, DB: Database> Type<DB> for $smart_pointer
250+
where
251+
T: Type<DB>,
252+
T: ?Sized,
253+
{
254+
fn type_info() -> DB::TypeInfo {
255+
<T as Type<DB>>::type_info()
256+
}
255257

256-
fn compatible(ty: &DB::TypeInfo) -> bool {
257-
ty.is_null() || <T as Type<DB>>::compatible(ty)
258-
}
258+
fn compatible(ty: &DB::TypeInfo) -> bool {
259+
<T as Type<DB>>::compatible(ty)
260+
}
261+
}
262+
};
259263
}
260264

265+
impl_type_for_smartpointer!(Arc<T>);
266+
impl_type_for_smartpointer!(Box<T>);
267+
impl_type_for_smartpointer!(Rc<T>);
268+
261269
impl<T, DB: Database> Type<DB> for Cow<'_, T>
262270
where
263271
T: Type<DB>,
@@ -268,26 +276,6 @@ where
268276
}
269277

270278
fn compatible(ty: &DB::TypeInfo) -> bool {
271-
ty.is_null() || <T as Type<DB>>::compatible(ty)
272-
}
273-
}
274-
275-
impl<T: Type<DB>, DB: Database> Type<DB> for Box<T> {
276-
fn type_info() -> DB::TypeInfo {
277-
<T as Type<DB>>::type_info()
278-
}
279-
280-
fn compatible(ty: &DB::TypeInfo) -> bool {
281-
ty.is_null() || <T as Type<DB>>::compatible(ty)
282-
}
283-
}
284-
285-
impl<T: Type<DB>, DB: Database> Type<DB> for Rc<T> {
286-
fn type_info() -> DB::TypeInfo {
287-
<T as Type<DB>>::type_info()
288-
}
289-
290-
fn compatible(ty: &DB::TypeInfo) -> bool {
291-
ty.is_null() || <T as Type<DB>>::compatible(ty)
279+
<T as Type<DB>>::compatible(ty)
292280
}
293281
}

sqlx-postgres/src/types/str.rs

-10
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@ impl Type<Postgres> for Cow<'_, str> {
3434
}
3535
}
3636

37-
impl Type<Postgres> for Box<str> {
38-
fn type_info() -> PgTypeInfo {
39-
<&str as Type<Postgres>>::type_info()
40-
}
41-
42-
fn compatible(ty: &PgTypeInfo) -> bool {
43-
<&str as Type<Postgres>>::compatible(ty)
44-
}
45-
}
46-
4737
impl Type<Postgres> for String {
4838
fn type_info() -> PgTypeInfo {
4939
<&str as Type<Postgres>>::type_info()

tests/postgres/types.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,12 @@ async fn test_arc() -> anyhow::Result<()> {
683683
let user_age: (Arc<i32>, Cow<'static, i32>, Box<i32>, i32) =
684684
sqlx::query_as("SELECT $1, $2, $3, $4")
685685
.bind(Arc::new(1i32))
686-
.bind(Cow::<'_, i32>::Owned(2i32))
686+
.bind(Cow::<'_, i32>::Borrowed(&2i32))
687687
.bind(Box::new(3i32))
688688
.bind(Rc::new(4i32))
689689
.fetch_one(&mut conn)
690690
.await?;
691+
691692
assert!(user_age.0.as_ref() == &1);
692693
assert!(user_age.1.as_ref() == &2);
693694
assert!(user_age.2.as_ref() == &3);

0 commit comments

Comments
 (0)