| 1 | //! Provides [`Encode`] for encoding values for the database. |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/; |
| 4 | |
| 5 | use craterust-analyzer cargo sqlx-core 0.8.3 crate/::databaserust-analyzer cargo sqlx-core 0.8.3 database/::Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#; |
| 6 | use craterust-analyzer cargo sqlx-core 0.8.3 crate/::errorrust-analyzer cargo sqlx-core 0.8.3 error/::BoxDynErrorrust-analyzer cargo sqlx-core 0.8.3 error/BoxDynError#; |
| 7 | |
| 8 | /// The return type of [Encode::encode]. |
| 9 | #[must_use] |
| 10 | pub enum IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull# { |
| 11 | /// The value is null; no data was written. |
| 12 | Yesrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#Yes#, |
| 13 | |
| 14 | /// The value is not null. |
| 15 | /// |
| 16 | /// This does not mean that data was written. |
| 17 | Norust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#No#, |
| 18 | } |
| 19 | |
| 20 | impl IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull# { |
| 21 | pub fn is_nullrust-analyzer cargo sqlx-core 0.8.3 encode/impl#[IsNull]is_null().(&self) -> bool { |
| 22 | matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self, IsNullrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#::Yesrust-analyzer cargo sqlx-core 0.8.3 encode/IsNull#Yes#) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /// Encode a single value to be sent to the database. |
| 27 | pub trait Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'q, DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#> { |
| 28 | /// Writes the value of `self` into `buf` in the expected format for the database. |
| 29 | fn encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#encode().(self, buf: &mut <DB as Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#>::ArgumentBufferrust-analyzer cargo sqlx-core 0.8.3 database/Database#ArgumentBuffer#<'q>) -> 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 | where |
| 31 | Self: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 32 | { |
| 33 | self.encode_by_refrust-analyzer cargo sqlx-core 0.8.3 encode/Encode#encode_by_ref().(buf) |
| 34 | } |
| 35 | |
| 36 | /// Writes the value of `self` into `buf` without moving `self`. |
| 37 | /// |
| 38 | /// Where possible, make use of `encode` instead as it can take advantage of re-using |
| 39 | /// memory. |
| 40 | fn encode_by_refrust-analyzer cargo sqlx-core 0.8.3 encode/Encode#encode_by_ref().( |
| 41 | &self, |
| 42 | buf: &mut <DB as Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#>::ArgumentBufferrust-analyzer cargo sqlx-core 0.8.3 database/Database#ArgumentBuffer#<'q>, |
| 43 | ) -> 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#>; |
| 44 | |
| 45 | fn producesrust-analyzer cargo sqlx-core 0.8.3 encode/Encode#produces().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<DB::TypeInforust-analyzer cargo sqlx-core 0.8.3 database/Database#TypeInfo#> { |
| 46 | // `produces` is inherently a hook to allow database drivers to produce value-dependent |
| 47 | // type information; if the driver doesn't need this, it can leave this as `None` |
| 48 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 49 | } |
| 50 | |
| 51 | #[inline] |
| 52 | fn size_hintrust-analyzer cargo sqlx-core 0.8.3 encode/Encode#size_hint().(&self) -> usize { |
| 53 | memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_of_valrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of_val().(self) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | impl<'q, T, DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#> Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'q, DB> for &'_ T |
| 58 | where |
| 59 | T: Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<'q, DB>, |
| 60 | { |
| 61 | #[inline] |
| 62 | fn encoderust-analyzer cargo sqlx-core 0.8.3 encode/impl#[`&T`][`Encode<'q, DB>`]encode().(self, buf: &mut <DB as Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#>::ArgumentBufferrust-analyzer cargo sqlx-core 0.8.3 database/Database#ArgumentBuffer#<'q>) -> 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#> { |
| 63 | <T as Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<DB>>::encode_by_refrust-analyzer cargo sqlx-core 0.8.3 encode/Encode#encode_by_ref().(self, buf) |
| 64 | } |
| 65 | |
| 66 | #[inline] |
| 67 | fn encode_by_refrust-analyzer cargo sqlx-core 0.8.3 encode/impl#[`&T`][`Encode<'q, DB>`]encode_by_ref().( |
| 68 | &self, |
| 69 | buf: &mut <DB as Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#>::ArgumentBufferrust-analyzer cargo sqlx-core 0.8.3 database/Database#ArgumentBuffer#<'q>, |
| 70 | ) -> 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#> { |
| 71 | <&T as Encoderust-analyzer cargo sqlx-core 0.8.3 encode/Encode#<DB>>::encoderust-analyzer cargo sqlx-core 0.8.3 encode/impl#[`&T`][`Encode<'q, DB>`]encode().(self, buf) |
| 72 | } |
| 73 | |
| 74 | #[inline] |
| 75 | fn producesrust-analyzer cargo sqlx-core 0.8.3 encode/impl#[`&T`][`Encode<'q, DB>`]produces().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<DB::TypeInforust-analyzer cargo sqlx-core 0.8.3 database/Database#TypeInfo#> { |
| 76 | (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self).producesrust-analyzer cargo sqlx-core 0.8.3 encode/Encode#produces().() |
| 77 | } |
| 78 | |
| 79 | #[inline] |
| 80 | fn size_hintrust-analyzer cargo sqlx-core 0.8.3 encode/impl#[`&T`][`Encode<'q, DB>`]size_hint().(&self) -> usize { |
| 81 | (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self).size_hintrust-analyzer cargo sqlx-core 0.8.3 encode/Encode#size_hint().() |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | #[macro_export] |
| 86 | macro_rules! impl_encode_for_optionrust-analyzer cargo sqlx-core 0.8.3 encode/impl_encode_for_option! { |
| 87 | ($DB:ident) => { |
| 88 | impl<'q, T> $crate::encode::Encode<'q, $DB> for Option<T> |
| 89 | where |
| 90 | T: $crate::encode::Encode<'q, $DB> + $crate::types::Type<$DB> + 'q, |
| 91 | { |
| 92 | #[inline] |
| 93 | fn produces(&self) -> Option<<$DB as $crate::database::Database>::TypeInfo> { |
| 94 | if let Some(v) = self { |
| 95 | v.produces() |
| 96 | } else { |
| 97 | T::type_info().into() |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | #[inline] |
| 102 | fn encode( |
| 103 | self, |
| 104 | buf: &mut <$DB as $crate::database::Database>::ArgumentBuffer<'q>, |
| 105 | ) -> Result<$crate::encode::IsNull, $crate::error::BoxDynError> { |
| 106 | if let Some(v) = self { |
| 107 | v.encode(buf) |
| 108 | } else { |
| 109 | Ok($crate::encode::IsNull::Yes) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | #[inline] |
| 114 | fn encode_by_ref( |
| 115 | &self, |
| 116 | buf: &mut <$DB as $crate::database::Database>::ArgumentBuffer<'q>, |
| 117 | ) -> Result<$crate::encode::IsNull, $crate::error::BoxDynError> { |
| 118 | if let Some(v) = self { |
| 119 | v.encode_by_ref(buf) |
| 120 | } else { |
| 121 | Ok($crate::encode::IsNull::Yes) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | #[inline] |
| 126 | fn size_hint(&self) -> usize { |
| 127 | self.as_ref().map_or(0, $crate::encode::Encode::size_hint) |
| 128 | } |
| 129 | } |
| 130 | }; |
| 131 | } |