|
| 1 | +use crate::{type_info::PgType, PgArgumentBuffer, PgHasArrayType, PgTypeInfo, Postgres}; |
| 2 | +use core::cell::Cell; |
1 | 3 | use sqlx_core::{
|
2 | 4 | database::Database,
|
3 | 5 | encode::{Encode, IsNull},
|
4 | 6 | error::BoxDynError,
|
5 | 7 | types::Type,
|
6 | 8 | };
|
7 | 9 |
|
8 |
| -use crate::{type_info::PgType, PgArgumentBuffer, PgHasArrayType, PgTypeInfo, Postgres}; |
9 |
| - |
10 | 10 | // 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>>); |
12 | 12 |
|
13 | 13 | /// Iterator extension trait enabling iterators to encode arrays in Postgres.
|
14 | 14 | ///
|
@@ -58,7 +58,7 @@ pub trait PgBindIterExt: Iterator + Sized {
|
58 | 58 |
|
59 | 59 | impl<I: Iterator + Sized> PgBindIterExt for I {
|
60 | 60 | fn bind_iter(self) -> PgBindIter<I> {
|
61 |
| - PgBindIter(self) |
| 61 | + PgBindIter(Cell::new(Some(self))) |
62 | 62 | }
|
63 | 63 | }
|
64 | 64 |
|
@@ -136,17 +136,19 @@ where
|
136 | 136 |
|
137 | 137 | impl<'q, I> Encode<'q, Postgres> for PgBindIter<I>
|
138 | 138 | 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, |
141 | 140 | <I as Iterator>::Item: Type<Postgres> + Encode<'q, Postgres>,
|
142 | 141 | {
|
143 | 142 | 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) |
145 | 144 | }
|
146 | 145 | fn encode(self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError>
|
147 | 146 | where
|
148 | 147 | Self: Sized,
|
149 | 148 | {
|
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 | + ) |
151 | 153 | }
|
152 | 154 | }
|
0 commit comments