use std::cmp::{Eq, PartialEq,};
use std::convert::{AsRef, AsMut};
use std::fmt::{self, Formatter, Debug};
use std::ops::{Deref, DerefMut};
use slice_ext::{SliceExt};
#[derive(Clone)]
pub struct Hash {
hash: [u64; 8],
len: usize
}
impl Hash {
pub fn new(hash: [u64; 8], len: usize) -> Self {
assert!(len <= 64);
Hash { hash: hash, len: len }
}
pub fn len(&self) -> usize {
self.len as usize
}
pub fn into_u64_array(self) -> [u64; 8] {
assert!(self.len == 64);
self.hash
}
}
impl AsRef<[u8]> for Hash {
fn as_ref(&self) -> &[u8] {
&self.hash.as_bytes()[..self.len]
}
}
impl AsRef<[u64]> for Hash {
fn as_ref(&self) -> &[u64] {
assert!(self.len % 8 == 0);
&self.hash[..self.len / 8]
}
}
impl AsMut<[u8]> for Hash {
fn as_mut<'a>(&'a mut self) -> &'a mut [u8] {
&mut self.hash.as_mut_bytes()[..self.len]
}
}
impl AsMut<[u64]> for Hash {
fn as_mut<'a>(&'a mut self) -> &'a mut [u64] {
assert!(self.len % 8 == 0);
&mut self.hash[..self.len / 8]
}
}
impl Debug for Hash {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
fmt::Debug::fmt(&**self, fmt)
}
}
impl Deref for Hash {
type Target = [u8];
fn deref<'a>(&'a self) -> &'a Self::Target {
self.as_ref()
}
}
impl DerefMut for Hash {
fn deref_mut<'a>(&'a mut self) -> &'a mut Self::Target {
self.as_mut()
}
}
impl Eq for Hash {}
impl PartialEq for Hash {
fn eq(&self, rhs: &Self) -> bool {
&**self == &**rhs
}
}
impl PartialEq<[u8]> for Hash {
fn eq(&self, rhs: &[u8]) -> bool {
&**self == rhs
}
}