use std::{
hash::Hasher,
pin::Pin,
task::{Context, Poll},
};
#[derive(Default, Debug, Clone)]
pub struct Crc32(crc32fast::Hasher);
impl Crc32 {
pub fn finalize(self) -> u32 {
self.0.finalize()
}
}
impl std::io::Write for Crc32 {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0.write(buf);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
impl tokio::io::AsyncWrite for Crc32 {
fn poll_write(
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, std::io::Error>> {
Poll::Ready(std::io::Write::write(&mut *self, buf))
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), std::io::Error>> {
Poll::Ready(Ok(()))
}
fn poll_shutdown(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<(), std::io::Error>> {
Poll::Ready(Ok(()))
}
}