sqlx_exasol_impl/types/
option.rs

1use sqlx_core::{
2    encode::{Encode, IsNull},
3    error::BoxDynError,
4    types::Type,
5};
6
7use crate::{arguments::ExaBuffer, types::ExaHasArrayType, ExaTypeInfo, Exasol};
8
9impl<T> ExaHasArrayType for Option<T> where T: ExaHasArrayType {}
10
11impl<T> Encode<'_, Exasol> for Option<T>
12where
13    for<'q> T: Encode<'q, Exasol> + Type<Exasol>,
14{
15    #[inline]
16    fn produces(&self) -> Option<ExaTypeInfo> {
17        if let Some(v) = self {
18            v.produces()
19        } else {
20            Some(T::type_info())
21        }
22    }
23
24    #[inline]
25    fn encode(self, buf: &mut ExaBuffer) -> Result<IsNull, BoxDynError> {
26        if let Some(v) = self {
27            v.encode(buf)
28        } else {
29            buf.append(())?;
30            Ok(IsNull::Yes)
31        }
32    }
33
34    #[inline]
35    fn encode_by_ref(&self, buf: &mut ExaBuffer) -> Result<IsNull, BoxDynError> {
36        if let Some(v) = self {
37            v.encode_by_ref(buf)
38        } else {
39            buf.append(())?;
40            Ok(IsNull::Yes)
41        }
42    }
43
44    #[inline]
45    fn size_hint(&self) -> usize {
46        // We encode `null` when `None`, hence size 4.
47        self.as_ref().map_or(4, Encode::size_hint)
48    }
49}