ihash 0.1.2

World's fastest high-quality non-cryptographic hash, deterministic cross-platform XOF, and seekable stream cipher
Documentation
#[cfg(feature = "std")]
use std::io::{Read, Result as IoResult};

use fearless_simd::Level;

use super::{
  hash256_with_level,
  vperm::{CpuBackend, detect_level},
};
use crate::stream::{StreamCore, impl_stream_api};

/// Pure-CPU seekable deterministic keystream and XOF reader backed by `fearless_simd`.
#[derive(Clone, Debug)]
pub struct Stream {
  core: StreamCore,
  level: Level,
}

impl Stream {
  /// Creates a pure-CPU stream from `cpu::hash256(data)`.
  #[must_use]
  #[inline]
  pub fn new(data: &[u8]) -> Self {
    Self::with_level(data, detect_level())
  }

  /// Creates a pure-CPU stream using an explicit `fearless_simd::Level`.
  #[must_use]
  #[inline]
  pub fn with_level(data: &[u8], level: Level) -> Self {
    Self::from_root_with_level(hash256_with_level(data, level), level)
  }

  /// Creates a pure-CPU stream directly from a 256-bit root digest.
  #[must_use]
  #[inline]
  pub fn from_root(root: [u8; 32]) -> Self {
    Self::from_root_with_level(root, detect_level())
  }

  /// Creates a pure-CPU stream from a precomputed 256-bit root digest and `fearless_simd::Level`.
  #[must_use]
  #[inline]
  pub const fn from_root_with_level(root: [u8; 32], level: Level) -> Self {
    Self {
      core: StreamCore::from_root(root),
      level,
    }
  }

  #[inline(always)]
  const fn backend(&self) -> CpuBackend {
    CpuBackend(self.level)
  }
}

impl_stream_api!(Stream, core);

impl PartialEq for Stream {
  #[inline]
  fn eq(&self, other: &Self) -> bool {
    self.core == other.core
  }
}

impl Eq for Stream {}