pancake_db_core/primitives/
traits.rs

1use pancake_db_idl::dml::field_value::Value;
2use pancake_db_idl::dtype::DataType;
3
4use crate::compression::{Codec, ValueCodec};
5use crate::errors::CoreResult;
6use std::fmt::Debug;
7
8pub trait Atom: 'static + Copy + Debug + Default + Send + Sync {
9  const BYTE_SIZE: usize;
10
11  fn to_bytes(&self) -> Vec<u8>;
12  fn try_from_bytes(bytes: &[u8]) -> CoreResult<Self> where Self: Sized;
13}
14
15pub trait Primitive: 'static + Default + Send + Sync {
16  type A: Atom;
17
18  const DTYPE: DataType;
19  const IS_ATOMIC: bool;
20
21  fn to_value(&self) -> Value;
22  fn try_from_value(v: &Value) -> CoreResult<Self> where Self: Sized;
23
24  fn to_atoms(&self) -> Vec<Self::A>;
25  fn try_from_atoms(atoms: &[Self::A]) -> CoreResult<Self> where Self: Sized;
26
27  fn new_codec(codec: &str) -> Option<Box<dyn Codec<P=Self>>>;
28  fn new_value_codec(codec: &str) -> Option<Box<dyn ValueCodec>> where Self: Sized {
29    Self::new_codec(codec).map(|c| {
30      let c: Box<dyn ValueCodec> = Box::new(c);
31      c
32    })
33  }
34}