use std::fmt;
use std::hash::Hash;
use super::DType;
pub trait DataType:
Copy + Clone + fmt::Debug + PartialEq + Eq + Hash + Send + Sync + 'static
{
fn size_in_bytes(self) -> usize;
fn short_name(self) -> &'static str;
fn is_float(self) -> bool;
fn is_int(self) -> bool;
fn is_quantized(self) -> bool {
false
}
fn block_size(self) -> usize {
1
}
fn block_bytes(self) -> usize {
self.size_in_bytes()
}
fn storage_bytes(self, numel: usize) -> usize {
if self.is_quantized() {
let bs = self.block_size();
let bb = self.block_bytes();
((numel + bs - 1) / bs) * bb
} else {
numel * self.size_in_bytes()
}
}
fn as_standard(&self) -> Option<DType>;
fn fill_bytes(self, value: f64, count: usize) -> Option<Vec<u8>> {
self.as_standard()
.map(|std_dtype| std_dtype.fill_bytes_impl(value, count))
}
}
impl DataType for DType {
#[inline]
fn size_in_bytes(self) -> usize {
DType::size_in_bytes(self)
}
#[inline]
fn short_name(self) -> &'static str {
DType::short_name(self)
}
#[inline]
fn is_float(self) -> bool {
DType::is_float(self)
}
#[inline]
fn is_int(self) -> bool {
DType::is_int(self)
}
#[inline]
fn as_standard(&self) -> Option<DType> {
Some(*self)
}
}