use core::hash::Hasher;
use std::fmt::Debug;
use std::hash::Hash;
use std::io;
use crate::ChecksumReader;
use crate::ChecksumWriter;
use crate::Segment;
use crate::WithChecksum;
#[cfg_attr(not(feature = "crc32fast"), doc = "```ignore")]
#[cfg_attr(feature = "crc32fast", doc = "```rust")]
pub trait CodeqConfig
where Self: Debug + Clone + Copy + Default + PartialEq + Eq + PartialOrd + Ord + Hash + Sized
{
type Hasher: Hasher + Default;
fn hash(buf: &[u8]) -> u64 {
let mut hasher = Self::Hasher::default();
hasher.write(buf);
hasher.finish()
}
fn new_writer<W: io::Write>(inner: W) -> ChecksumWriter<Self, W> {
ChecksumWriter::new(inner)
}
fn new_reader<R: io::Read>(inner: R) -> ChecksumReader<Self, R> {
ChecksumReader::new(inner)
}
fn wrap<T>(data: T) -> WithChecksum<Self, T> {
WithChecksum::<Self, _>::new(data)
}
fn segment(offset: u64, size: u64) -> Segment<Self> {
Segment::<Self>::new(offset, size)
}
}
#[cfg(feature = "crc32fast")]
pub mod crc32fast_impl {
use super::CodeqConfig;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Crc32fast;
impl CodeqConfig for Crc32fast {
type Hasher = crc32fast::Hasher;
}
}
#[cfg(feature = "crc32fast")]
pub use crc32fast_impl::Crc32fast;
#[cfg(feature = "crc64fast-nvme")]
mod crc64fast_nvme_impl {
use crate::config::CodeqConfig;
#[derive(Default, Clone)]
pub struct Crc64fastNvmeHasher(crc64fast_nvme::Digest);
impl core::hash::Hasher for Crc64fastNvmeHasher {
fn finish(&self) -> u64 {
self.0.sum64()
}
fn write(&mut self, bytes: &[u8]) {
self.0.write(bytes);
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Crc64fastNvme;
impl CodeqConfig for Crc64fastNvme {
type Hasher = Crc64fastNvmeHasher;
}
}
#[cfg(feature = "crc64fast-nvme")]
pub use crc64fast_nvme_impl::Crc64fastNvme;