#![cfg_attr(not(any(feature = "std", test)), no_std)]
#[doc(hidden)]
pub mod baseline;
#[doc(hidden)]
pub mod specialized;
#[cfg(not(feature = "std"))]
use core::hash::Hasher;
#[cfg(feature = "std")]
use std::hash::Hasher;
const DEFAULT_INIT_STATE: u32 = 1;
#[derive(Copy, Clone, Debug)]
enum State {
Baseline(baseline::State),
Specialized(specialized::State),
}
#[derive(Copy, Clone, Debug)]
pub struct Adler32 {
state: State,
}
impl Adler32 {
pub fn new() -> Self {
Self::from(DEFAULT_INIT_STATE)
}
pub fn as_u32(&self) -> u32 {
match self.state {
State::Baseline(state) => state.finalize(),
State::Specialized(state) => state.finalize(),
}
}
pub fn is_simd_enabled(&self) -> bool {
match self.state {
State::Specialized(_) => true,
_ => false,
}
}
pub fn reset(&mut self) {
match self.state {
State::Baseline(ref mut state) => state.reset(),
State::Specialized(ref mut state) => state.reset(),
}
}
pub fn update(&mut self, buf: &[u8]) {
match self.state {
State::Baseline(ref mut state) => state.update(buf),
State::Specialized(ref mut state) => state.update(buf),
}
}
fn internal_new_baseline(initial: u32) -> Self {
Self {
state: State::Baseline(baseline::State::new(initial)),
}
}
#[doc(hidden)]
fn internal_new_specialized(initial: u32) -> Option<Self> {
specialized::State::new(initial).map(|state| Self {
state: State::Specialized(state),
})
}
}
impl Default for Adler32 {
fn default() -> Self {
Self::new()
}
}
impl From<u32> for Adler32 {
fn from(initial: u32) -> Self {
Self::internal_new_specialized(initial)
.unwrap_or_else(|| Self::internal_new_baseline(initial))
}
}
impl Hasher for Adler32 {
fn finish(&self) -> u64 {
u64::from(self.as_u32())
}
fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}
impl PartialEq<u32> for Adler32 {
fn eq(&self, &other: &u32) -> bool {
self.as_u32() == other
}
}