pub trait PgHasArrayType {
// Required method
fn array_type_info() -> PgTypeInfo;
// Provided method
fn array_compatible(ty: &PgTypeInfo) -> bool { ... }
}postgres only.Expand description
Provides information necessary to encode and decode Postgres arrays as compatible Rust types.
Implementing this trait for some type T enables relevant Type,Encode and Decode impls
for Vec<T>, &[T] (slices), [T; N] (arrays), etc.
ยงNote: #[derive(sqlx::Type)]
If you have the postgres feature enabled, #[derive(sqlx::Type)] will also generate
an impl of this trait for your type if your wrapper is marked #[sqlx(transparent)]:
#[derive(sqlx::Type)]
#[sqlx(transparent)]
struct UserId(i64);
let user_ids: Vec<UserId> = sqlx::query_scalar("select '{ 123, 456 }'::int8[]")
.fetch(&mut pg_connection)
.await?;However, this may cause an error if the type being wrapped does not implement PgHasArrayType,
e.g. Vec itself, because we donโt currently support multidimensional arrays:
#[derive(sqlx::Type)] // ERROR: `Vec<i64>` does not implement `PgHasArrayType`
#[sqlx(transparent)]
struct UserIds(Vec<i64>);To remedy this, add #[sqlx(no_pg_array)], which disables the generation
of the PgHasArrayType impl:
#[derive(sqlx::Type)]
#[sqlx(transparent, no_pg_array)]
struct UserIds(Vec<i64>);See the documentation of Type for more details.
Required Methodsยง
fn array_type_info() -> PgTypeInfo
Provided Methodsยง
fn array_compatible(ty: &PgTypeInfo) -> bool
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.