pub trait PgHasArrayType {
    // Required method
    fn array_type_info() -> PgTypeInfo;

    // Provided method
    fn array_compatible(ty: &PgTypeInfo) -> bool { ... }
}
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§

Provided Methods§

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl PgHasArrayType for &str

source§

impl PgHasArrayType for &[u8]

source§

impl PgHasArrayType for Cow<'_, str>

source§

impl PgHasArrayType for Value

source§

impl PgHasArrayType for bool

source§

impl PgHasArrayType for f32

source§

impl PgHasArrayType for f64

source§

impl PgHasArrayType for i8

source§

impl PgHasArrayType for i16

source§

impl PgHasArrayType for i32

source§

impl PgHasArrayType for i64

source§

impl PgHasArrayType for u8

source§

impl PgHasArrayType for Box<str>

source§

impl PgHasArrayType for Box<[u8]>

source§

impl PgHasArrayType for String

source§

impl PgHasArrayType for Vec<u8>

source§

impl PgHasArrayType for Duration

source§

impl PgHasArrayType for RawValue

source§

impl<T1> PgHasArrayType for (T1,)

source§

impl<T1, T2> PgHasArrayType for (T1, T2)

source§

impl<T1, T2, T3> PgHasArrayType for (T1, T2, T3)

source§

impl<T1, T2, T3, T4> PgHasArrayType for (T1, T2, T3, T4)

source§

impl<T1, T2, T3, T4, T5> PgHasArrayType for (T1, T2, T3, T4, T5)

source§

impl<T1, T2, T3, T4, T5, T6> PgHasArrayType for (T1, T2, T3, T4, T5, T6)

source§

impl<T1, T2, T3, T4, T5, T6, T7> PgHasArrayType for (T1, T2, T3, T4, T5, T6, T7)

source§

impl<T1, T2, T3, T4, T5, T6, T7, T8> PgHasArrayType for (T1, T2, T3, T4, T5, T6, T7, T8)

source§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9> PgHasArrayType for (T1, T2, T3, T4, T5, T6, T7, T8, T9)

source§

impl<T> PgHasArrayType for Option<T>
where T: PgHasArrayType,

source§

impl<T> PgHasArrayType for Json<T>

source§

impl<T> PgHasArrayType for Text<T>

source§

impl<const N: usize> PgHasArrayType for &[u8; N]

source§

impl<const N: usize> PgHasArrayType for [u8; N]

Implementors§