PgBindIterExt

Trait PgBindIterExt 

Source
pub trait PgBindIterExt: Iterator + Sized {
    // Required method
    fn bind_iter(self) -> PgBindIter<Self>;
}
Expand description

Iterator extension trait enabling iterators to encode arrays in Postgres.

Because of the blanket impl of PgHasArrayType for all references we can borrow instead of needing to clone or copy in the iterators and it still works

Previously, 3 separate arrays would be needed in this example which requires iterating 3 times to collect items into the array and then iterating over them again to encode.

This now requires only iterating over the array once for each field while using less memory giving both speed and memory usage improvements along with allowing much more flexibility in the underlying collection.

use sqlx::postgres::PgBindIterExt;

#[derive(sqlx::FromRow)]
struct Person {
    id: i64,
    name: String,
    birthdate: DateTime<Utc>,
}

sqlx::query("insert into person(id, name, birthdate) select * from unnest($1, $2, $3)")
    .bind(people.iter().map(|p| p.id).bind_iter())
    .bind(people.iter().map(|p| &p.name).bind_iter())
    .bind(people.iter().map(|p| &p.birthdate).bind_iter())
    .execute(&mut conn)
    .await?;

Required Methods§

Source

fn bind_iter(self) -> PgBindIter<Self>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§