Skip to content

Commit 6ae3505

Browse files
committed
Use Cell<Option<I>> instead of Clone bound
1 parent 9f8e03a commit 6ae3505

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

sqlx-postgres/src/bind_iter.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
use crate::{type_info::PgType, PgArgumentBuffer, PgHasArrayType, PgTypeInfo, Postgres};
2+
use core::cell::Cell;
13
use sqlx_core::{
24
database::Database,
35
encode::{Encode, IsNull},
46
error::BoxDynError,
57
types::Type,
68
};
79

8-
use crate::{type_info::PgType, PgArgumentBuffer, PgHasArrayType, PgTypeInfo, Postgres};
9-
1010
// not exported but pub because it is used in the extension trait
11-
pub struct PgBindIter<I>(I);
11+
pub struct PgBindIter<I>(Cell<Option<I>>);
1212

1313
/// Iterator extension trait enabling iterators to encode arrays in Postgres.
1414
///
@@ -58,7 +58,7 @@ pub trait PgBindIterExt: Iterator + Sized {
5858

5959
impl<I: Iterator + Sized> PgBindIterExt for I {
6060
fn bind_iter(self) -> PgBindIter<I> {
61-
PgBindIter(self)
61+
PgBindIter(Cell::new(Some(self)))
6262
}
6363
}
6464

@@ -136,17 +136,19 @@ where
136136

137137
impl<'q, I> Encode<'q, Postgres> for PgBindIter<I>
138138
where
139-
// Clone is required for the encode_by_ref call since we can't iterate with a shared reference
140-
I: Iterator + Clone,
139+
I: Iterator,
141140
<I as Iterator>::Item: Type<Postgres> + Encode<'q, Postgres>,
142141
{
143142
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
144-
Self::encode_inner(self.0.clone(), buf)
143+
Self::encode_inner(self.0.take().expect("PgBindIter is only used once"), buf)
145144
}
146145
fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError>
147146
where
148147
Self: Sized,
149148
{
150-
Self::encode_inner(self.0, buf)
149+
Self::encode_inner(
150+
self.0.into_inner().expect("PgBindIter is only used once"),
151+
buf,
152+
)
151153
}
152154
}

0 commit comments

Comments
 (0)