use std::ops::Not; use crate::database::{HasOutput, HasRawValue}; use crate::{decode, encode, Database, Decode, Encode, RawValue, Type}; #[derive(Debug)] pub struct Null; impl> Type for Option where Null: Type, { fn type_id() -> ::TypeId where Self: Sized, { T::type_id() } fn compatible(ty: &::TypeInfo) -> bool where Self: Sized, { T::compatible(ty) } } impl> Encode for Option where Null: Encode, { fn encode( &self, ty: &::TypeInfo, out: &mut >::Output, ) -> encode::Result { match self { Some(value) => value.encode(ty, out), None => Null.encode(ty, out), } } } impl<'r, Db: Database, T: Decode<'r, Db>> Decode<'r, Db> for Option where Null: Decode<'r, Db>, { fn decode(value: >::RawValue) -> decode::Result { value.is_null().not().then(|| T::decode(value)).transpose() } }