pub struct SHA1(pub [u8; 20]);Expand description
The SHA1 struct, encapsulating a [u8; 20] array, is specifically designed to represent Git hash IDs.
In Git’s context, these IDs are 40-character hexadecimal strings generated via the SHA-1 algorithm.
Each Git object receives a unique hash ID based on its content, serving as an identifier for its location
within the Git internal database. Utilizing a dedicated struct for these hash IDs enhances code readability and
maintainability by providing a clear, structured format for their manipulation and storage.
§Change Log
In previous versions of the ‘mega’ project, Hash was used to denote hash values. However, in newer versions,
SHA1 is employed for this purpose. Future updates plan to extend support to SHA256 and SHA512, or potentially
other hash algorithms. By abstracting the hash model to Hash, and using specific imports like use crate::hash::SHA1
or use crate::hash::SHA256, the codebase maintains a high level of clarity and maintainability. This design choice
allows for easier adaptation to different hash algorithms while keeping the underlying implementation consistent and
understandable. - Nov 26, 2023 (by @genedna)
Tuple Fields§
§0: [u8; 20]Implementations§
Source§impl SHA1
Implementation of the SHA1 struct.
impl SHA1
Implementation of the SHA1 struct.
The naming conventions for the methods in this implementation are designed to be intuitive and self-explanatory:
-
newPrefix: Methods starting withneware used for computing an SHA-1 hash from given data, signifying the creation of a newSHA1instance. For example,pub fn new(data: &Vec<u8>) -> SHA1takes a byte vector and calculates its SHA-1 hash. -
fromPrefix: Methods beginning withfromare intended for creating aSHA1instance from an existing, pre-calculated value. This implies direct derivation of theSHA1object from the provided input. For instance,pub fn from_bytes(bytes: &[u8]) -> SHA1constructs aSHA1from a 20-byte array representing an SHA-1 hash. -
toPrefix: Methods with thetoprefix are used for outputting theSHA1value in various formats. This prefix indicates a transformation or conversion of theSHA1instance into another representation. For example,pub fn to_string(self) -> Stringconverts the SHA1 value to a plain hexadecimal string, andpub fn to_data(self) -> Vec<u8>converts it into a byte vector. Thetoprefix thus serves as a clear indicator that the method is exporting or transforming the SHA1 value into a different format.
These method naming conventions (new, from, to) provide clarity and predictability in the API, making it easier for users
to understand the intended use and functionality of each method within the SHA1 struct.
pub const SIZE: usize = 20usize
Sourcepub fn new(data: &[u8]) -> SHA1
pub fn new(data: &[u8]) -> SHA1
Calculate the SHA-1 hash of the byte slice, then create a Hash value
Sourcepub fn from_type_and_data(object_type: ObjectType, data: &[u8]) -> SHA1
pub fn from_type_and_data(object_type: ObjectType, data: &[u8]) -> SHA1
Create a Hash from the object type and data This function is used to create a SHA1 hash from the object type and data. It constructs a byte vector that includes the object type, the size of the data, and the data itself, and then computes the SHA1 hash of this byte vector.
Hash compute <- {Object Type}+{ }+{Object Size(before compress)}+{\x00}+{Object Content(before compress)}
Sourcepub fn from_bytes(bytes: &[u8]) -> SHA1
pub fn from_bytes(bytes: &[u8]) -> SHA1
Create Hash from a byte array, which is a 20-byte array already calculated
Sourcepub fn from_stream(data: &mut impl Read) -> Result<SHA1>
pub fn from_stream(data: &mut impl Read) -> Result<SHA1>
Read the Hash value from the stream This function will read exactly 20 bytes from the stream
Sourcepub fn to_color_str(self) -> String
pub fn to_color_str(self) -> String
Export sha1 value to String with the color
Sourcepub fn _to_string(&self) -> String
pub fn _to_string(&self) -> String
core::fmt::Display is somewhat expensive,
use this hack to get a string more efficiently
Trait Implementations§
Source§impl<'__de, __Context> BorrowDecode<'__de, __Context> for SHA1
impl<'__de, __Context> BorrowDecode<'__de, __Context> for SHA1
Source§fn borrow_decode<__D: BorrowDecoder<'__de, Context = __Context>>(
decoder: &mut __D,
) -> Result<Self, DecodeError>
fn borrow_decode<__D: BorrowDecoder<'__de, Context = __Context>>( decoder: &mut __D, ) -> Result<Self, DecodeError>
Source§impl<'de> Deserialize<'de> for SHA1
impl<'de> Deserialize<'de> for SHA1
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl FromStr for SHA1
Implementation of the std::str::FromStr trait for the SHA1 type.
impl FromStr for SHA1
Implementation of the std::str::FromStr trait for the SHA1 type.
To effectively use the from_str method for converting a string to a SHA1 object, consider the following:
- The input string
sshould be a pre-calculated hexadecimal string, exactly 40 characters in length. This string represents a SHA1 hash and should conform to the standard SHA1 hash format. - It is necessary to explicitly import the
FromStrtrait to utilize thefrom_strmethod. Include the import statementuse std::str::FromStr;in your code before invoking thefrom_strfunction. This import ensures that thefrom_strmethod is available for converting strings toSHA1objects.
Source§impl HeapSize for SHA1
impl HeapSize for SHA1
Source§fn heap_size_sum_iter<'item, Fun, Iter>(make_iter: Fun) -> usize
fn heap_size_sum_iter<'item, Fun, Iter>(make_iter: Fun) -> usize
Source§fn heap_size_sum_exact_size_iter<'item, Fun, Iter>(make_iter: Fun) -> usizewhere
Self: 'item,
Fun: Fn() -> Iter,
Iter: ExactSizeIterator<Item = &'item Self>,
fn heap_size_sum_exact_size_iter<'item, Fun, Iter>(make_iter: Fun) -> usizewhere
Self: 'item,
Fun: Fn() -> Iter,
Iter: ExactSizeIterator<Item = &'item Self>,
Source§impl Ord for SHA1
impl Ord for SHA1
Source§impl PartialOrd for SHA1
impl PartialOrd for SHA1
impl Copy for SHA1
impl Eq for SHA1
impl StructuralPartialEq for SHA1
Auto Trait Implementations§
impl Freeze for SHA1
impl RefUnwindSafe for SHA1
impl Send for SHA1
impl Sync for SHA1
impl Unpin for SHA1
impl UnwindSafe for SHA1
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> FileLoadStore for Twhere
T: Serialize + for<'a> Deserialize<'a>,
impl<T> FileLoadStore for Twhere
T: Serialize + for<'a> Deserialize<'a>,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Lower case
letters are used (e.g. f9b4ca)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Upper case
letters are used (e.g. F9B4CA)