@@ -11,7 +11,7 @@ use std::{
11
11
pub struct Id < ' a > ( & ' a [ u8 ; SIZE_OF_SHA1_DIGEST ] ) ;
12
12
13
13
/// A borrowed reference to a hash identifying objects.
14
- #[ derive( Hash ) ]
14
+ #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd ) ]
15
15
#[ repr( transparent) ]
16
16
#[ allow( non_camel_case_types) ]
17
17
#[ cfg_attr( feature = "serde1" , derive( serde:: Serialize ) ) ]
@@ -74,6 +74,30 @@ impl oid {
74
74
}
75
75
}
76
76
77
+ /// Sha1 specific methods
78
+ impl oid {
79
+ /// Returns an array with a hexadecimal encoded version of the Sha1 hash this `Id` represents.
80
+ ///
81
+ /// **Panics** if this is not a Sha1 hash, as identifiable by [`Id::kind()`].
82
+ pub fn to_sha1_hex ( & self ) -> [ u8 ; SIZE_OF_SHA1_DIGEST * 2 ] {
83
+ let mut buf = [ 0u8 ; SIZE_OF_SHA1_DIGEST * 2 ] ;
84
+ hex:: encode_to_slice ( & self . bytes , & mut buf) . expect ( "to count correctly" ) ;
85
+ buf
86
+ }
87
+
88
+ /// Returns the bytes making up the Sha1.
89
+ ///
90
+ /// **Panics** if this is not a Sha1 hash, as identifiable by [`Id::kind()`].
91
+ pub fn sha1 ( & self ) -> & [ u8 ; SIZE_OF_SHA1_DIGEST ] {
92
+ self . bytes . try_into ( ) . expect ( "correctly sized slice" )
93
+ }
94
+
95
+ /// Returns a Sha1 digest with all bytes being initialized to zero.
96
+ pub fn null_sha1 ( ) -> & ' static Self {
97
+ oid:: from ( [ 0u8 ; SIZE_OF_SHA1_DIGEST ] . as_ref ( ) )
98
+ }
99
+ }
100
+
77
101
impl AsRef < oid > for & oid {
78
102
fn as_ref ( & self ) -> & oid {
79
103
self
0 commit comments