| 1 | use serderust-analyzer cargo serde 1.0.229 crate/::{Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#, Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize#}; |
| 2 | |
| 3 | use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::decoderust-analyzer cargo sqlx-core 0.8.3 decode/::Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#; |
| 4 | use craterust-analyzer cargo sqlx-mysql 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#}; |
| 5 | use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::errorrust-analyzer cargo sqlx-mysql 0.8.3 error/::BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#; |
| 6 | use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::protocolrust-analyzer cargo sqlx-mysql 0.8.3 protocol/::textrust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/::ColumnTyperust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#; |
| 7 | use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::typesrust-analyzer cargo sqlx-mysql 0.8.3 types/::{Jsonrust-analyzer cargo sqlx-core 0.8.3 types/json/Json#, Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#}; |
| 8 | use craterust-analyzer cargo sqlx-mysql 0.8.3 crate/::{MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#, MySqlTypeInforust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo#, MySqlValueRefrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueRef#}; |
| 9 | |
| 10 | impl<T> Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#> for Jsonrust-analyzer cargo sqlx-core 0.8.3 types/json/Json#<T> { |
| 11 | fn type_inforust-analyzer cargo sqlx-mysql 0.8.3 types/json/impl#[`Json<T>`][`Type<MySql>`]type_info().() -> MySqlTypeInforust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo# { |
| 12 | // MySql uses the `CHAR` type to pass JSON data from and to the client |
| 13 | // NOTE: This is forwards-compatible with MySQL v8+ as CHAR is a common transmission format |
| 14 | // and has nothing to do with the native storage ability of MySQL v8+ |
| 15 | MySqlTypeInforust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo#::binaryrust-analyzer cargo sqlx-mysql 0.8.3 type_info/impl#[MySqlTypeInfo]binary().(ColumnTyperust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#::Stringrust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#String#) |
| 16 | } |
| 17 | |
| 18 | fn compatiblerust-analyzer cargo sqlx-mysql 0.8.3 types/json/impl#[`Json<T>`][`Type<MySql>`]compatible().(ty: &MySqlTypeInforust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo#) -> bool { |
| 19 | ty.r#typerust-analyzer cargo sqlx-mysql 0.8.3 type_info/MySqlTypeInfo#`r#type`. rust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/impl#[ColumnType][`PartialEq<Self>`]eq().==rust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/impl#[ColumnType][`PartialEq<Self>`]eq(). rust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/impl#[ColumnType][`PartialEq<Self>`]eq().ColumnTyperust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#::Jsonrust-analyzer cargo sqlx-mysql 0.8.3 protocol/text/column/ColumnType#Json# |
| 20 | || <&str as Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#>>::compatiblerust-analyzer cargo sqlx-core 0.8.3 types/impl#[`&T`][`Type<DB>`]compatible().(ty) |
| 21 | || <&[u8] as Typerust-analyzer cargo sqlx-core 0.8.3 types/Type#<MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#>>::compatiblerust-analyzer cargo sqlx-core 0.8.3 types/impl#[`&T`][`Type<DB>`]compatible().(ty) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl<T> Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'_, MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#> for Jsonrust-analyzer cargo sqlx-core 0.8.3 types/json/Json#<T> |
| 26 | where |
| 27 | T: Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize#, |
| 28 | { |
| 29 | fn encode_by_refrust-analyzer cargo sqlx-mysql 0.8.3 types/json/impl#[`Json<T>`][`Encode<'_, MySql>`]encode_by_ref().(&self, buf: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> 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#> { |
| 30 | // Encode JSON as a length-prefixed string. |
| 31 | // |
| 32 | // The previous implementation encoded into an intermediate buffer to get the final length. |
| 33 | // This is because the length prefix for the string is itself length-encoded, so we have |
| 34 | // to know the length first before we can start encoding in the buffer... or do we? |
| 35 | // |
| 36 | // The docs suggest that the integer length-encoding doesn't actually enforce a range on |
| 37 | // the value itself as long as it fits in the chosen encoding, so why not just choose |
| 38 | // the full length encoding to begin with? Then we can just reserve the space up-front |
| 39 | // and encode directly into the buffer. |
| 40 | // |
| 41 | // If someone is storing a JSON value it's likely large enough that the overhead of using |
| 42 | // the full-length integer encoding doesn't really matter. And if it's so large it overflows |
| 43 | // a `u64` then the process is likely to run OOM during the encoding process first anyway. |
| 44 | |
| 45 | let lenenc_start = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 46 | |
| 47 | buf.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&[0u8; 9]); |
| 48 | |
| 49 | let encode_start = buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 50 | self.encode_torust-analyzer cargo sqlx-core 0.8.3 types/json/impl#[`Json<T>`]encode_to().(buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 51 | let encoded_len = (buf.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]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().encode_start) as u64; |
| 52 | |
| 53 | // This prefix indicates that the following 8 bytes are a little-endian integer. |
| 54 | buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().lenenc_start]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = 0xFE; |
| 55 | buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().lenenc_start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/index/impl#[`[T]`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#8]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/index/impl#[`[T]`][`IndexMut<I>`]index_mut()..copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&encoded_len.to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]to_le_bytes().()); |
| 56 | |
| 57 | 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#) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl<'r, T> Decoderust-analyzer cargo sqlx-core 0.8.3 decode/Decode#<'r, MySqlrust-analyzer cargo sqlx-mysql 0.8.3 database/MySql#> for Jsonrust-analyzer cargo sqlx-core 0.8.3 types/json/Json#<T> |
| 62 | where |
| 63 | T: 'r + Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#<'r>, |
| 64 | { |
| 65 | fn decoderust-analyzer cargo sqlx-mysql 0.8.3 types/json/impl#[`Json<T>`][`Decode<'r, MySql>`]decode().(value: MySqlValueRefrust-analyzer cargo sqlx-mysql 0.8.3 value/MySqlValueRef#<'r>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sqlx-mysql 0.8.3 types/json/impl#[`Json<T>`][`Decode<'r, MySql>`], BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#> { |
| 66 | Jsonrust-analyzer cargo sqlx-core 0.8.3 types/json/Json#::decode_from_stringrust-analyzer cargo sqlx-core 0.8.3 types/json/impl#[`Json<T>`]decode_from_string().(value.as_strrust-analyzer cargo sqlx-mysql 0.8.3 value/impl#[`MySqlValueRef<'r>`]as_str().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().) |
| 67 | } |
| 68 | } |