GeneralHash

Enum GeneralHash 

Source
pub enum GeneralHash {
    SHA512([u8; 64]),
    SHA256([u8; 32]),
    SHA1([u8; 20]),
    XXH64([u8; 8]),
    XXH32([u8; 4]),
    NULL,
}
Expand description

GeneralHash is an enum that represents a hash value.

The hash value is stored as a byte array of a fixed size. The size of the byte array depends on the hash function used.

The following hash functions are supported: SHA512, SHA256, SHA1, XXH64, XXH32, and NULL.

The hash_type method returns the type of the hash function used. The hasher method returns a new instance of a GeneralHasher trait object that corresponds to the hash type. The hasher can then be used to compute a hash of that kind.

§Traits

  • Display - to allow formatting a GeneralHash into a string.
  • FromStr - to allow parsing a string into a GeneralHash.
  • Serialize - to allow serializing a GeneralHash into a string.
  • Deserialize - to allow deserializing a GeneralHash from a string.

§Examples

use std::str::FromStr;
use backup_deduplicator::hash::{GeneralHash, GeneralHashType};

#[cfg(feature = "hash-sha2")]
{
let hash = GeneralHash::from_str("SHA256:315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3").unwrap();

let mut hasher = GeneralHashType::SHA256.hasher();
hasher.update(b"Hello, world!".as_slice());
let new_hash = hasher.finalize();

assert_eq!(hash, new_hash);
assert_eq!(hash.to_string(), new_hash.to_string());
}

§See also

Variants§

§

SHA512([u8; 64])

§

SHA256([u8; 32])

§

SHA1([u8; 20])

§

XXH64([u8; 8])

§

XXH32([u8; 4])

§

NULL

Implementations§

Source§

impl GeneralHash

Source

pub fn as_bytes(&self) -> &[u8]

Returns the hash value as a byte array.

§Returns

A reference to the byte array that represents the hash value.

Source

pub fn new_sha512() -> Self

Returns a new instance of a SHA512 hash value.

Source

pub fn new_sha256() -> Self

Returns a new instance of a SHA256 hash value.

Source

pub fn new_sha1() -> Self

Returns a new instance of a SHA1 hash value.

Source

pub fn new_xxh64() -> Self

Returns a new instance of a XXH64 hash value.

Source

pub fn new_xxh32() -> Self

Returns a new instance of a XXH32 hash value.

Source

pub fn hash_type(&self) -> GeneralHashType

Returns the type of the hash function used.

§Returns

The type of the hash function used.

§Examples
use backup_deduplicator::hash::{GeneralHash, GeneralHashType};

#[cfg(feature = "hash-sha2")]
{
   let hash = GeneralHash::new_sha256();
}
Source

pub fn from_type(hash_type: GeneralHashType) -> Self

Returns a new instance of a GeneralHash with the specified hash type.

§Arguments
  • hash_type - The type of the hash function to use.
§Returns

A new instance of a GeneralHash with the specified hash type.

Source

pub fn hasher(&self) -> Box<dyn GeneralHasher>

Returns a new instance of a GeneralHash with the specified hash type.

§Arguments
  • hash_type - The type of the hash function to use.
§Returns

A new instance of a GeneralHash with the specified hash type.

§See also
  • GeneralHashType - representation of the different types of hash functions.
Source

pub fn hash_file<T>(&mut self, reader: T) -> Result<u64>
where T: Read,

Computes the hash value of the specified data.

§Arguments
  • reader - The data to hash (supplied as std::io::Read).
§Returns

The size of the data that was hashed.

§Errors

Returns an error if the data could not be read.

Source

pub fn hash_directory<'a>( &mut self, children: impl Iterator<Item = &'a BuildFile>, ) -> Result<u64>

Computes the hash value of file iterator/directory.

§Arguments
  • children - The iterator of files to hash.
§Returns

The count of files that were hashed.

§Errors

Does not return an error. Might return an error in the future.

Source

pub fn hash_path(&mut self, path: &Path) -> Result<()>

Computes the hash value of the specified path.

§Arguments
  • path - The path to hash.
§Returns

Does not return a value.

§Errors

Does not return an error. Might return an error in the future.

Source

pub fn hash_filepath(&mut self, path: &FilePath) -> Result<()>

Computes the hash value of the specified file path.

§Arguments
  • path - The file path to hash.
§Returns

Does not return a value.

§Errors

Does not return an error. Might return an error in the future.

Trait Implementations§

Source§

impl Clone for GeneralHash

Source§

fn clone(&self) -> GeneralHash

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GeneralHash

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for GeneralHash

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserializes a GeneralHash from a string.

§Arguments
  • deserializer - The deserializer to use.
§Returns

The deserialized GeneralHash.

§Errors

If the string could not be deserialized.

Source§

impl Display for GeneralHash

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for GeneralHash

Source§

fn from_str(hex: &str) -> Result<Self, Self::Err>

Parses a string into a GeneralHash.

§Arguments
  • hex - The string to parse, in the format hash_type:hash_data (hex).
§Returns

The GeneralHash that corresponds to the string or an error.

§Errors

Returns an error if the string does not correspond to a GeneralHash.

  • If the hash type is not recognized.
  • If the hash data is not valid (wrong length or non-hex string).
Source§

type Err = &'static str

The associated error which can be returned from parsing.
Source§

impl Hash for GeneralHash

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for GeneralHash

Source§

fn eq(&self, other: &GeneralHash) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for GeneralHash

Source§

fn partial_cmp(&self, other: &GeneralHash) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for GeneralHash

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for GeneralHash

Source§

impl StructuralPartialEq for GeneralHash

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,