sqlx_exasol/types/
option.rs

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