use super::*;
use num_traits::{One, Zero};
use std::fmt::Debug;
pub trait CellEncoding: Copy + Debug + Default + Zero + One + PartialEq {
fn cell_type() -> CellType;
fn into_cell_value(self) -> CellValue;
fn static_cast<T: CellEncoding + Sized>(value: T) -> Option<Self> {
if Self::cell_type() == T::cell_type() {
Some(unsafe { std::mem::transmute_copy::<T, Self>(&value) })
} else {
None
}
}
}
macro_rules! encoding {
( $( ($ct:ident, $prim:ident) ),* ) => { $(
impl CellEncoding for $prim {
fn cell_type() -> CellType {
CellType::$ct
}
fn into_cell_value(self) -> CellValue {
CellValue::$ct(self)
}
} )*
};
}
with_ct!(encoding);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn casting() {
assert!(<f64>::static_cast(34f64).is_some());
assert!(<f64>::static_cast(34f32).is_none());
}
}