Struct Hash

Source
pub struct Hash<T> { /* private fields */ }
Expand description

A SHA-1 or SHA-256 reference to a git Commit, Tree, or Blob

Implementations§

Source§

impl<T> Hash<T>

Source

pub fn from_str(s: &str) -> Result<Self, HashParseError>

Construct a Hash from a hexidecimal string. The entire hash must be specified: 40 characters (SHA-1) or 64 (SHA-256)

§Examples
use clgit::unknown::Hash; // aka clgit::generic::Hash<()>
 
for good in [
    // Legal SHA-1 hashes (20 bytes / 40 characters)
    "74da26a93c3eac22884a62bd8d70aab3434c9174",
    "89dd60cc88e4f89e0af91e2739c42a31c3a106bb",
    "eb6c43cb699caa2ccbc4e28f9ab75a2a17e4ee7c",

    // Uppercase is legal too
    "74DA26A93C3EAC22884A62BD8D70AAB3434C9174",
    "89DD60CC88E4F89E0AF91E2739C42A31C3A106BB",
    "EB6C43CB699CAA2CCBC4E28F9AB75A2A17E4EE7C",
 
    // SHA-256 hashes (40 bytes / 64 characters)
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
].iter().cloned() {
    Hash::from_str(good).unwrap_or_else(|e| panic!("Failed to parse {}: {}", good, e));
}

for bad in [
    "eb6c43cb699caa2ccbc4e28f9ab75a2a17e4ee7c0", // too long
    "eb6c43cb699caa2ccbc4e28f9ab75a2a17e4ee7",   // too short
    "eb6c43cb699caa2ccbc4e28f9ab75a2a17e4ee7!",  // invalid character
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde",   // too short
    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0", // too long
].iter().cloned() {
    assert!(Hash::from_str(bad).is_err(), "Didn't expect to parse {}", bad);
}
Source

pub fn from_bytes(src: &[u8]) -> Result<Self, HashParseError>

Construct a Hash from a slice of bytes. The entire hash must be specified: 20 bytes (SHA-1) or 32 (SHA-256)

§Examples
Hash::from_bytes(&[0u8; 20][..]).expect("20 bytes OK");
Hash::from_bytes(&[0u8; 32][..]).expect("32 bytes OK");
 
Hash::from_bytes(&[0u8; 19][..]).expect_err("19 bytes invalid");
Hash::from_bytes(&[0u8; 21][..]).expect_err("21 bytes invalid");
Hash::from_bytes(&[0u8; 31][..]).expect_err("31 bytes invalid");
Hash::from_bytes(&[0u8; 33][..]).expect_err("33 bytes invalid");
Source

pub fn read_sha1(r: &mut impl Read) -> Result<Self>

Read 20 bytes from r and treat it as a SHA-1 Hash

§Example
let mut io = std::io::Cursor::new(vec![0; 128]);
Hash::read_sha1(&mut io).unwrap();
Source

pub fn read_sha256(r: &mut impl Read) -> Result<Self>

Read 32 bytes from r and treat it as a SHA-256 Hash

§Example
let mut io = std::io::Cursor::new(vec![0; 128]);
Hash::read_sha256(&mut io).unwrap();
Source

pub fn len(&self) -> usize

Get the number of bytes in this hash (20 or 32)

§Example
assert!(hash.len() == 20 || hash.len() == 32);
Source

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

Get the bytes in this hash (length of 20 or 32)

§Example
let bytes : &[u8] = hash.bytes();
assert!(bytes.len() == 20 || bytes.len() == 32);
Source

pub fn first_byte(&self) -> u8

Get the first byte of this hash

§Example
println!("byte: {:02x}", hash.first_byte());
Source

pub fn typeless(&self) -> Hash<()>

Discard type information for this hash

Source§

impl Hash<()>

Source

pub fn cast<T>(&self) -> Hash<T>

Acquire type information for this hash

Trait Implementations§

Source§

impl<T> Clone for Hash<T>

Source§

fn clone(&self) -> Self

Returns a copy 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<T> Debug for Hash<T>

Source§

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

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

impl<T> Default for Hash<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Display for Hash<T>

Source§

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

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

impl<T> FromStr for Hash<T>

Source§

type Err = HashParseError

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

fn from_str(s: &str) -> Result<Self, HashParseError>

Parses a string s to return a value of this type. Read more
Source§

impl<T> Hash for Hash<T>

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<T> Ord for Hash<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq<Hash<()>> for Hash<Commit>

Source§

fn eq(&self, other: &Hash<()>) -> 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 PartialEq<Hash<()>> for Hash<Tree>

Source§

fn eq(&self, other: &Hash<()>) -> 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 PartialEq<Hash<Commit>> for Hash<()>

Source§

fn eq(&self, other: &Hash<Commit>) -> 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 PartialEq<Hash<Tree>> for Hash<()>

Source§

fn eq(&self, other: &Hash<Tree>) -> 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<T> PartialEq for Hash<T>

Source§

fn eq(&self, other: &Self) -> 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<T> PartialOrd for Hash<T>

Source§

fn partial_cmp(&self, other: &Self) -> 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<T> Eq for Hash<T>

Auto Trait Implementations§

§

impl<T> Freeze for Hash<T>

§

impl<T> RefUnwindSafe for Hash<T>
where T: RefUnwindSafe,

§

impl<T> Send for Hash<T>
where T: Send,

§

impl<T> Sync for Hash<T>
where T: Sync,

§

impl<T> Unpin for Hash<T>
where T: Unpin,

§

impl<T> UnwindSafe for Hash<T>
where T: UnwindSafe,

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> 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.