| 1 | use sqlx_corerust-analyzer cargo sqlx-core 0.8.3 crate/::bytesrust-analyzer cargo bytes 1.12.1 crate/::Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#; |
| 2 | use sqlx_corerust-analyzer cargo sqlx-core 0.8.3 crate/::typesrust-analyzer cargo sqlx-core 0.8.3 types/::Textrust-analyzer cargo sqlx-core 0.8.3 types/text/Text#; |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#; |
| 4 | |
| 5 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::decoderust-analyzer cargo sqlx-core 0.8.3 decode/::Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#; |
| 6 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::encoderust-analyzer cargo sqlx-core 0.8.3 encode/::{Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#, IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#}; |
| 7 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::errorrust-analyzer cargo sqlx-postgres 0.8.3 error/::BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#; |
| 8 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::type_inforust-analyzer cargo sqlx-postgres 0.8.3 type_info/::PgTyperust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgType#; |
| 9 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::typesrust-analyzer cargo sqlx-postgres 0.8.3 types/::Oidrust-analyzer cargo sqlx-postgres 0.8.3 types/oid/Oid#; |
| 10 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::typesrust-analyzer cargo sqlx-postgres 0.8.3 types/::Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#; |
| 11 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::{PgArgumentBufferrust-analyzer cargo sqlx-postgres 0.8.3 arguments/PgArgumentBuffer#, PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#, PgValueFormatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#, PgValueRefrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#}; |
| 12 | |
| 13 | /// Provides information necessary to encode and decode Postgres arrays as compatible Rust types. |
| 14 | /// |
| 15 | /// Implementing this trait for some type `T` enables relevant `Type`,`Encode` and `Decode` impls |
| 16 | /// for `Vec<T>`, `&[T]` (slices), `[T; N]` (arrays), etc. |
| 17 | /// |
| 18 | /// ### Note: `#[derive(sqlx::Type)]` |
| 19 | /// If you have the `postgres` feature enabled, `#[derive(sqlx::Type)]` will also generate |
| 20 | /// an impl of this trait for your type if your wrapper is marked `#[sqlx(transparent)]`: |
| 21 | /// |
| 22 | /// ```rust,ignore |
| 23 | /// #[derive(sqlx::Type)] |
| 24 | /// #[sqlx(transparent)] |
| 25 | /// struct UserId(i64); |
| 26 | /// |
| 27 | /// let user_ids: Vec<UserId> = sqlx::query_scalar("select '{ 123, 456 }'::int8[]") |
| 28 | /// .fetch(&mut pg_connection) |
| 29 | /// .await?; |
| 30 | /// ``` |
| 31 | /// |
| 32 | /// However, this may cause an error if the type being wrapped does not implement `PgHasArrayType`, |
| 33 | /// e.g. `Vec` itself, because we don't currently support multidimensional arrays: |
| 34 | /// |
| 35 | /// ```rust,ignore |
| 36 | /// #[derive(sqlx::Type)] // ERROR: `Vec<i64>` does not implement `PgHasArrayType` |
| 37 | /// #[sqlx(transparent)] |
| 38 | /// struct UserIds(Vec<i64>); |
| 39 | /// ``` |
| 40 | /// |
| 41 | /// To remedy this, add `#[sqlx(no_pg_array)]`, which disables the generation |
| 42 | /// of the `PgHasArrayType` impl: |
| 43 | /// |
| 44 | /// ```rust,ignore |
| 45 | /// #[derive(sqlx::Type)] |
| 46 | /// #[sqlx(transparent, no_pg_array)] |
| 47 | /// struct UserIds(Vec<i64>); |
| 48 | /// ``` |
| 49 | /// |
| 50 | /// See [the documentation of `Type`][Type] for more details. |
| 51 | pub trait PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType# { |
| 52 | fn array_type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#array_type_info().() -> PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#; |
| 53 | fn array_compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#array_compatible().(ty: &PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#) -> bool { |
| 54 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().ty rust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo][`PartialEq<Self>`]eq().==rust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo][`PartialEq<Self>`]eq(). rust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo][`PartialEq<Self>`]eq().Self::array_type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#array_type_info().() |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | impl<T> PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType# for &T |
| 59 | where |
| 60 | T: PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#, |
| 61 | { |
| 62 | fn array_type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`&T`][PgHasArrayType]array_type_info().() -> PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo# { |
| 63 | T::array_type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#array_type_info().() |
| 64 | } |
| 65 | |
| 66 | fn array_compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`&T`][PgHasArrayType]array_compatible().(ty: &PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#) -> bool { |
| 67 | T::array_compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#array_compatible().(ty) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl<T> PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType# for Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T> |
| 72 | where |
| 73 | T: PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#, |
| 74 | { |
| 75 | fn array_type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`Option<T>`][PgHasArrayType]array_type_info().() -> PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo# { |
| 76 | T::array_type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#array_type_info().() |
| 77 | } |
| 78 | |
| 79 | fn array_compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`Option<T>`][PgHasArrayType]array_compatible().(ty: &PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#) -> bool { |
| 80 | T::array_compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#array_compatible().(ty) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | impl<T> PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType# for Textrust-analyzer cargo sqlx-core 0.8.3 types/text/Text#<T> { |
| 85 | fn array_type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`Text<T>`][PgHasArrayType]array_type_info().() -> PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo# { |
| 86 | Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::array_type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/str/impl#[String][PgHasArrayType]array_type_info().() |
| 87 | } |
| 88 | |
| 89 | fn array_compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`Text<T>`][PgHasArrayType]array_compatible().(ty: &PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#) -> bool { |
| 90 | Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::array_compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/str/impl#[String][PgHasArrayType]array_compatible().(ty) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl<T> Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for [T] |
| 95 | where |
| 96 | T: PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#, |
| 97 | { |
| 98 | fn type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`[T]`][`Type<Postgres>`]type_info().() -> PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo# { |
| 99 | T::array_type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#array_type_info().() |
| 100 | } |
| 101 | |
| 102 | fn compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`[T]`][`Type<Postgres>`]compatible().(ty: &PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#) -> bool { |
| 103 | T::array_compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#array_compatible().(ty) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl<T> Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T> |
| 108 | where |
| 109 | T: PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#, |
| 110 | { |
| 111 | fn type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`Vec<T>`][`Type<Postgres>`]type_info().() -> PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo# { |
| 112 | T::array_type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#array_type_info().() |
| 113 | } |
| 114 | |
| 115 | fn compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`Vec<T>`][`Type<Postgres>`]compatible().(ty: &PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#) -> bool { |
| 116 | T::array_compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#array_compatible().(ty) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | impl<T, const N: usize> Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for [T; N] |
| 121 | where |
| 122 | T: PgHasArrayTyperust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#, |
| 123 | { |
| 124 | fn type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`[T; N]`][`Type<Postgres>`]type_info().() -> PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo# { |
| 125 | T::array_type_inforust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#array_type_info().() |
| 126 | } |
| 127 | |
| 128 | fn compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`[T; N]`][`Type<Postgres>`]compatible().(ty: &PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#) -> bool { |
| 129 | T::array_compatiblerust-analyzer cargo sqlx-postgres 0.8.3 types/array/PgHasArrayType#array_compatible().(ty) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | impl<'q, T> Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'q, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T> |
| 134 | where |
| 135 | for<'a> &'a [T]: Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'q, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#>, |
| 136 | T: Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'q, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#>, |
| 137 | { |
| 138 | #[inline] |
| 139 | fn encode_by_refrust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`Vec<T>`][`Encode<'q, Postgres>`]encode_by_ref().(&self, buf: &mut PgArgumentBufferrust-analyzer cargo sqlx-postgres 0.8.3 arguments/PgArgumentBuffer#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#, BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> { |
| 140 | self.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().().encode_by_refrust-analyzer cargo sqlx-core 0.8.3 encode/Encode#encode_by_ref().(buf) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | impl<'q, T, const N: usize> Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'q, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for [T; N] |
| 145 | where |
| 146 | for<'a> &'a [T]: Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'q, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#>, |
| 147 | T: Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'q, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#>, |
| 148 | { |
| 149 | fn encode_by_refrust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`[T; N]`][`Encode<'q, Postgres>`]encode_by_ref().(&self, buf: &mut PgArgumentBufferrust-analyzer cargo sqlx-postgres 0.8.3 arguments/PgArgumentBuffer#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#, BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> { |
| 150 | self.as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().().encode_by_refrust-analyzer cargo sqlx-core 0.8.3 encode/Encode#encode_by_ref().(buf) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl<'q, T> Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'q, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for &'_ [T] |
| 155 | where |
| 156 | T: Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'q, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> + Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#>, |
| 157 | { |
| 158 | fn encode_by_refrust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`&[T]`][`Encode<'q, Postgres>`]encode_by_ref().(&self, buf: &mut PgArgumentBufferrust-analyzer cargo sqlx-postgres 0.8.3 arguments/PgArgumentBuffer#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#, BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> { |
| 159 | let type_info = self |
| 160 | .firstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]first().() |
| 161 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#::producesrust-analyzer cargo sqlx-core 0.8.3 encode/Encode#produces().) |
| 162 | .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(T::type_inforust-analyzer cargo sqlx-core 0.8.3 types/Type#type_info().); |
| 163 | |
| 164 | buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&1_i32.to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]to_be_bytes().()); // number of dimensions |
| 165 | buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&0_i32.to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]to_be_bytes().()); // flags |
| 166 | |
| 167 | // element type |
| 168 | match type_info.0rust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#0. { |
| 169 | PgTyperust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgType#::DeclareWithNamerust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgType#DeclareWithName#(name) => buf.patch_type_by_namerust-analyzer cargo sqlx-postgres 0.8.3 arguments/impl#[PgArgumentBuffer]patch_type_by_name().(&name), |
| 170 | PgTyperust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgType#::DeclareArrayOfrust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgType#DeclareArrayOf#(array) => buf.patch_array_typerust-analyzer cargo sqlx-postgres 0.8.3 arguments/impl#[PgArgumentBuffer]patch_array_type().(array), |
| 171 | |
| 172 | ty => { |
| 173 | buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&ty.oidrust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgType]oid().().0rust-analyzer cargo sqlx-postgres 0.8.3 types/oid/Oid#0..to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]to_be_bytes().()); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | let array_len = i32::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/ptr_try_from_impls/impl#[i32][`TryFrom<usize>`]try_from().(self.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| { |
| 178 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 179 | "encoded array length is too large for Postgres: {}", |
| 180 | self.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() |
| 181 | ) |
| 182 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 183 | |
| 184 | buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(array_len.to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]to_be_bytes().()); // len |
| 185 | buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&1_i32.to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]to_be_bytes().()); // lower bound |
| 186 | |
| 187 | for element in self.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 188 | buf.encoderust-analyzer cargo sqlx-postgres 0.8.3 arguments/impl#[PgArgumentBuffer]encode().(element)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 189 | } |
| 190 | |
| 191 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#::Norust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#No#) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | impl<'r, T, const N: usize> Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#<'r, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for [T; N] |
| 196 | where |
| 197 | T: for<'a> Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#<'a, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> + Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#>, |
| 198 | { |
| 199 | fn decoderust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`[T; N]`][`Decode<'r, Postgres>`]decode().(value: PgValueRefrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#<'r>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`[T; N]`][`Decode<'r, Postgres>`], BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> { |
| 200 | // This could be done more efficiently by refactoring the Vec decoding below so that it can |
| 201 | // be used for arrays and Vec. |
| 202 | let vec: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T> = Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#::decoderust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`Vec<T>`][`Decode<'r, Postgres>`]decode().(value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 203 | let array: [T; N] = vec.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| "wrong number of elements")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 204 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(array) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | impl<'r, T> Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#<'r, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> for Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T> |
| 209 | where |
| 210 | T: for<'a> Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#<'a, Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#> + Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<Postgresrust-analyzer cargo sqlx-postgres 0.8.3 database/Postgres#>, |
| 211 | { |
| 212 | fn decoderust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`Vec<T>`][`Decode<'r, Postgres>`]decode().(value: PgValueRefrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#<'r>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sqlx-postgres 0.8.3 types/array/impl#[`Vec<T>`][`Decode<'r, Postgres>`], BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> { |
| 213 | let format = value.formatrust-analyzer cargo sqlx-postgres 0.8.3 value/impl#[`PgValueRef<'r>`]format().(); |
| 214 | |
| 215 | match format { |
| 216 | PgValueFormatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#::Binaryrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#Binary# => { |
| 217 | // https://github.com/postgres/postgres/blob/a995b371ae29de2d38c4b7881cf414b1560e9746/src/backend/utils/adt/arrayfuncs.c#L1548 |
| 218 | |
| 219 | let mut buf = value.as_bytesrust-analyzer cargo sqlx-postgres 0.8.3 value/impl#[`PgValueRef<'r>`]as_bytes().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 220 | |
| 221 | // number of dimensions in the array |
| 222 | let ndim = buf.get_i32rust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#get_i32().(); |
| 223 | |
| 224 | if ndim rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().0 { |
| 225 | // zero dimensions is an empty array |
| 226 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().()); |
| 227 | } |
| 228 | |
| 229 | if ndim rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().1 { |
| 230 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("encountered an array of {ndim} dimensions; only one-dimensional arrays are supported").intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 231 | } |
| 232 | |
| 233 | // appears to have been used in the past to communicate potential NULLS |
| 234 | // but reading source code back through our supported postgres versions (9.5+) |
| 235 | // this is never used for anything |
| 236 | let _flags = buf.get_i32rust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#get_i32().(); |
| 237 | |
| 238 | // the OID of the element |
| 239 | let element_type_oid = Oidrust-analyzer cargo sqlx-postgres 0.8.3 types/oid/Oid#(buf.get_u32rust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#get_u32().()); |
| 240 | let element_type_info: PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo# = PgTypeInforust-analyzer cargo sqlx-postgres 0.8.3 type_info/PgTypeInfo#::try_from_oidrust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo]try_from_oid().(element_type_oid) |
| 241 | .or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]or_else().(|| value.type_inforust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#type_info..try_array_elementrust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgType]try_array_element().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::into_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`]into_owned().)) |
| 242 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| { |
| 243 | BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/convert/impl#[`Box<dyn Error + Send + Sync + 'a>`][`From<String>`]from().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 244 | "failed to resolve array element type for oid {}", |
| 245 | element_type_oid.0rust-analyzer cargo sqlx-postgres 0.8.3 types/oid/Oid#0. |
| 246 | )) |
| 247 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 248 | |
| 249 | // length of the array axis |
| 250 | let len = buf.get_i32rust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#get_i32().(); |
| 251 | |
| 252 | let len = usize::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/ptr_try_from_impls/impl#[usize][`TryFrom<i32>`]try_from().(len) |
| 253 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("overflow converting array len ({len}) to usize"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 254 | |
| 255 | // the lower bound, we only support arrays starting from "1" |
| 256 | let lower = buf.get_i32rust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#get_i32().(); |
| 257 | |
| 258 | if lower rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]ne().1 { |
| 259 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("encountered an array with a lower bound of {lower} in the first dimension; only arrays starting at one are supported").intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 260 | } |
| 261 | |
| 262 | let mut elements = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(len); |
| 263 | |
| 264 | for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#len { |
| 265 | let value_ref = PgValueRefrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#::getrust-analyzer cargo sqlx-postgres 0.8.3 value/impl#[`PgValueRef<'r>`]get().(&mut buf, format, element_type_info.clonerust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo][Clone]clone().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 266 | |
| 267 | elements.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(T::decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#decode().(value_ref)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 268 | } |
| 269 | |
| 270 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(elements) |
| 271 | } |
| 272 | |
| 273 | PgValueFormatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#::Textrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueFormat#Text# => { |
| 274 | // no type is provided from the database for the element |
| 275 | let element_type_info = T::type_inforust-analyzer cargo sqlx-core 0.8.3 types/Type#type_info().(); |
| 276 | |
| 277 | let s = value.as_strrust-analyzer cargo sqlx-postgres 0.8.3 value/impl#[`PgValueRef<'r>`]as_str().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 278 | |
| 279 | // https://github.com/postgres/postgres/blob/a995b371ae29de2d38c4b7881cf414b1560e9746/src/backend/utils/adt/arrayfuncs.c#L718 |
| 280 | |
| 281 | // trim the wrapping braces |
| 282 | let s = &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#(s.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1)]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 283 | |
| 284 | if s.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 285 | // short-circuit empty arrays up here |
| 286 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().()); |
| 287 | } |
| 288 | |
| 289 | // NOTE: Nearly *all* types use ',' as the sequence delimiter. Yes, there is one |
| 290 | // that does not. The BOX (not PostGIS) type uses ';' as a delimiter. |
| 291 | |
| 292 | // TODO: When we add support for BOX we need to figure out some way to make the |
| 293 | // delimiter selection |
| 294 | |
| 295 | let delimiter = ','; |
| 296 | let mut done = false; |
| 297 | let mut in_quotes = false; |
| 298 | let mut in_escape = false; |
| 299 | let mut value = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(10); |
| 300 | let mut chars = s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().(); |
| 301 | let mut elements = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(4); |
| 302 | |
| 303 | while !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().done { |
| 304 | loop { |
| 305 | match chars.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().() { |
| 306 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ch) => match ch { |
| 307 | _ if in_escape => { |
| 308 | value.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(ch); |
| 309 | in_escape = false; |
| 310 | } |
| 311 | |
| 312 | '"' => { |
| 313 | in_quotes = !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().in_quotes; |
| 314 | } |
| 315 | |
| 316 | '\\' => { |
| 317 | in_escape = true; |
| 318 | } |
| 319 | |
| 320 | _ if ch rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().delimiter && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().in_quotes => { |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | _ => { |
| 325 | value.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(ch); |
| 326 | } |
| 327 | }, |
| 328 | |
| 329 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => { |
| 330 | done = true; |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | let value_opt = if value rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq()."NULL" { |
| 337 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 338 | } else { |
| 339 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(value.as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().()) |
| 340 | }; |
| 341 | |
| 342 | elements.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(T::decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#decode().(PgValueRefrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef# { |
| 343 | valuerust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#value.: value_opt, |
| 344 | rowrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#row.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 345 | type_inforust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#type_info.: element_type_info.clonerust-analyzer cargo sqlx-postgres 0.8.3 type_info/impl#[PgTypeInfo][Clone]clone().(), |
| 346 | formatrust-analyzer cargo sqlx-postgres 0.8.3 value/PgValueRef#format., |
| 347 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 348 | |
| 349 | value.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]clear().(); |
| 350 | } |
| 351 | |
| 352 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(elements) |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |