heed_types/
unit.rs

1use std::borrow::Cow;
2use std::{error, fmt};
3
4use heed_traits::{BoxedError, BytesDecode, BytesEncode};
5
6/// Describes the unit `()` type.
7pub enum Unit {}
8
9impl BytesEncode<'_> for Unit {
10    type EItem = ();
11
12    fn bytes_encode(_item: &Self::EItem) -> Result<Cow<[u8]>, BoxedError> {
13        Ok(Cow::Borrowed(&[]))
14    }
15}
16
17impl BytesDecode<'_> for Unit {
18    type DItem = ();
19
20    fn bytes_decode(bytes: &[u8]) -> Result<Self::DItem, BoxedError> {
21        if bytes.is_empty() {
22            Ok(())
23        } else {
24            Err(NonEmptyError.into())
25        }
26    }
27}
28
29/// The slice of bytes is non-empty and therefore is not a unit `()` type.
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
31pub struct NonEmptyError;
32
33impl fmt::Display for NonEmptyError {
34    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35        f.write_str("the slice of bytes is non-empty and therefore is not a unit `()` type")
36    }
37}
38
39impl error::Error for NonEmptyError {}