pub struct Hash<T> { /* private fields */ }
Implementations§
Source§impl<T> Hash<T>
impl<T> Hash<T>
Sourcepub fn from_str(s: &str) -> Result<Self, HashParseError>
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);
}
Sourcepub fn from_bytes(src: &[u8]) -> Result<Self, HashParseError>
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");
Sourcepub fn read_sha256(r: &mut impl Read) -> Result<Self>
pub fn read_sha256(r: &mut impl Read) -> Result<Self>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of bytes in this hash (20 or 32)
§Example
assert!(hash.len() == 20 || hash.len() == 32);
Sourcepub fn bytes(&self) -> &[u8] ⓘ
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);
Sourcepub fn first_byte(&self) -> u8
pub fn first_byte(&self) -> u8
Trait Implementations§
Source§impl<T> Ord for Hash<T>
impl<T> Ord for Hash<T>
Source§impl<T> PartialOrd for Hash<T>
impl<T> PartialOrd for Hash<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more