1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
use crate::prelude::*;

// The Default derive enabled DefaultOnMissing to have None
#[derive(Copy, Clone, Default, Debug)]
#[repr(transparent)]
pub struct Nullable(bool);

unsafe impl Wrapper for Nullable {
    type Inner = bool;
}

impl BatchData for Nullable {
    // TODO: This is boilerplate, want blanket implementation to cover this and Array
    fn write_batch(items: &[Self], bytes: &mut Vec<u8>) {
        Wrapper::write_batch(items, bytes)
    }
    fn read_batch(bytes: &[u8]) -> ReadResult<Vec<Self>> {
        Wrapper::read_batch(bytes)
    }
    fn read_one(bytes: &[u8], offset: &mut usize) -> ReadResult<Self> {
        Ok(unsafe { std::mem::transmute(bool::read_one(bytes, offset)?) })
    }
    fn write_one(value: Self, bytes: &mut Vec<u8>) {
        unsafe { bool::write_one(std::mem::transmute(value), bytes) }
    }
}

impl Primitive for Nullable {
    fn id() -> PrimitiveId {
        PrimitiveId::Nullable
    }
    fn from_dyn_branch(_branch: DynBranch) -> ReadResult<OneOrMany<Self>> {
        unreachable!();
    }
}

impl_primitive_reader_writer!(Nullable);

#[derive(Default)]
pub struct NullableWriter<'a, V> {
    opt: <Nullable as Writable<'a>>::Writer,
    value: Option<V>,
}

pub struct NullableReader<V> {
    opt: <Nullable as Readable>::Reader,
    value: V,
}

impl<'a, V: Writer<'a>> Writer<'a> for NullableWriter<'a, V> {
    type Write = Option<V::Write>;
    fn write<'b : 'a>(&mut self, value: &'b Self::Write) {
        self.opt.write(&Nullable(value.is_some()));
        if let Some(value) = value {
            self.value
                .get_or_insert_with(|| V::default())
                .write(value);
        }
    }
    fn flush<B: StaticBranch>(self, branch: B, bytes: &mut Vec<u8>, lens: &mut Vec<usize>) {
        if let Some(value) = self.value {
            self.opt.flush(branch, bytes, lens);
            value.flush(branch, bytes, lens);
        } else {
            // TODO: Since Option can react to no branch, we can
            // save some bytes here by returning a value which (possibly) rolls back the outer value.
            // Eg: in struct this would simply remove the field name string that was written,
            // tuple is a bit more nuanced as it must write the void if there is a non-void that follows.
            PrimitiveId::Void.write(bytes);
        }
        
    }
}

impl<V: Reader> Reader for Option<NullableReader<V>> {
    type Read = Option<V::Read>;
    fn new<ParentBranch: StaticBranch>(sticks: DynBranch, branch: ParentBranch) -> ReadResult<Self> {
        match sticks {
            DynBranch::Nullable { opt, values } => {
                let values = *values;
                Ok(Some(NullableReader {
                    opt: PrimitiveReader::read_from(opt)?,
                    value: Reader::new(values, branch)?,
                }))
            },
            DynBranch::Void => Ok(None),
            _ => Err(ReadError::SchemaMismatch)?,
        }
    }
    fn read(&mut self) -> ReadResult<Self::Read> {
        let value = if let Some(inner) = self {
            if inner.opt.read()?.0 {
                Some(inner.value.read()?)
            } else {
                None
            }
        } else {
            None
        };
        Ok(value)
    }
}

impl<'a, T: Writable<'a>> Writable<'a> for Option<T> {
    type Writer = NullableWriter<'a, T::Writer>;
}

impl<T: Readable> Readable for Option<T> {
    type Reader = Option<NullableReader<T::Reader>>;
}