[][src]Struct clgit::generic::Hash

pub struct Hash<T> { /* fields omitted */ }

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

Implementations

impl<T> Hash<T>[src]

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

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);
}

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

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");

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

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();

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

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();

pub fn len(&self) -> usize[src]

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

Example

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

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

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

Example

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

pub fn first_byte(&self) -> u8[src]

Get the first byte of this hash

Example

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

pub fn typeless(&self) -> Hash<()>[src]

Discard type information for this hash

impl Hash<()>[src]

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

Acquire type information for this hash

Trait Implementations

impl<T> Clone for Hash<T>[src]

impl<T> Debug for Hash<T>[src]

impl<T> Default for Hash<T>[src]

impl<T> Display for Hash<T>[src]

impl<T> Eq for Hash<T>[src]

impl<T> FromStr for Hash<T>[src]

type Err = HashParseError

The associated error which can be returned from parsing.

impl<T> Hash for Hash<T>[src]

impl<T> Ord for Hash<T>[src]

impl PartialEq<Hash<()>> for Hash<Blob>[src]

impl PartialEq<Hash<()>> for Hash<Commit>[src]

impl PartialEq<Hash<()>> for Hash<Tree>[src]

impl PartialEq<Hash<Blob>> for Hash<()>[src]

impl PartialEq<Hash<Commit>> for Hash<()>[src]

impl<T> PartialEq<Hash<T>> for Hash<T>[src]

impl PartialEq<Hash<Tree>> for Hash<()>[src]

impl<T> PartialOrd<Hash<T>> for Hash<T>[src]

Auto Trait Implementations

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.