1
- use std:: { convert:: TryInto , fmt , hash} ;
1
+ use std:: { convert:: TryInto , hash} ;
2
2
3
3
use crate :: { Kind , ObjectId , SIZE_OF_SHA1_DIGEST } ;
4
4
@@ -41,28 +41,30 @@ pub struct HexDisplay<'a> {
41
41
hex_len : usize ,
42
42
}
43
43
44
- impl < ' a > fmt:: Display for HexDisplay < ' a > {
45
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
46
- let mut hex = crate :: Kind :: hex_buf ( ) ;
44
+ impl < ' a > std :: fmt:: Display for HexDisplay < ' a > {
45
+ fn fmt ( & self , f : & mut std :: fmt:: Formatter < ' _ > ) -> std :: fmt:: Result {
46
+ let mut hex = Kind :: hex_buf ( ) ;
47
47
let max_len = self . inner . hex_to_buf ( hex. as_mut ( ) ) ;
48
48
let hex = std:: str:: from_utf8 ( & hex[ ..self . hex_len . min ( max_len) ] ) . expect ( "ascii only in hex" ) ;
49
49
f. write_str ( hex)
50
50
}
51
51
}
52
52
53
- impl fmt:: Debug for oid {
54
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
53
+ impl std :: fmt:: Debug for oid {
54
+ fn fmt ( & self , f : & mut std :: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
55
55
write ! (
56
56
f,
57
57
"{}({})" ,
58
58
match self . kind( ) {
59
- crate :: Kind :: Sha1 => "Sha1" ,
59
+ Kind :: Sha1 => "Sha1" ,
60
60
} ,
61
61
self . to_hex( ) ,
62
62
)
63
63
}
64
64
}
65
65
66
+ /// The error returned when trying to convert a byte slice to an [`oid`] or [`ObjectId`]
67
+ #[ allow( missing_docs) ]
66
68
#[ derive( Debug , thiserror:: Error ) ]
67
69
pub enum Error {
68
70
#[ error( "Cannot instantiate git hash from a digest of length {0}" ) ]
@@ -104,8 +106,8 @@ impl oid {
104
106
impl oid {
105
107
/// The kind of hash used for this instance.
106
108
#[ inline]
107
- pub fn kind ( & self ) -> crate :: Kind {
108
- crate :: Kind :: from_len_in_bytes ( self . bytes . len ( ) )
109
+ pub fn kind ( & self ) -> Kind {
110
+ Kind :: from_len_in_bytes ( self . bytes . len ( ) )
109
111
}
110
112
111
113
/// The first byte of the hash, commonly used to partition a set of object ids.
@@ -164,7 +166,7 @@ impl oid {
164
166
/// Write ourselves to `out` in hexadecimal notation.
165
167
#[ inline]
166
168
pub fn write_hex_to ( & self , out : & mut dyn std:: io:: Write ) -> std:: io:: Result < ( ) > {
167
- let mut hex = crate :: Kind :: hex_buf ( ) ;
169
+ let mut hex = Kind :: hex_buf ( ) ;
168
170
let hex_len = self . hex_to_buf ( & mut hex) ;
169
171
out. write_all ( & hex[ ..hex_len] )
170
172
}
@@ -182,12 +184,20 @@ impl AsRef<oid> for &oid {
182
184
}
183
185
}
184
186
187
+ impl < ' a > TryFrom < & ' a [ u8 ] > for & ' a oid {
188
+ type Error = Error ;
189
+
190
+ fn try_from ( value : & ' a [ u8 ] ) -> Result < Self , Self :: Error > {
191
+ oid:: try_from_bytes ( value)
192
+ }
193
+ }
194
+
185
195
impl ToOwned for oid {
186
- type Owned = crate :: ObjectId ;
196
+ type Owned = ObjectId ;
187
197
188
198
fn to_owned ( & self ) -> Self :: Owned {
189
199
match self . kind ( ) {
190
- crate :: Kind :: Sha1 => crate :: ObjectId :: Sha1 ( self . bytes . try_into ( ) . expect ( "no bug in hash detection" ) ) ,
200
+ Kind :: Sha1 => ObjectId :: Sha1 ( self . bytes . try_into ( ) . expect ( "no bug in hash detection" ) ) ,
191
201
}
192
202
}
193
203
}
@@ -198,16 +208,16 @@ impl<'a> From<&'a [u8; SIZE_OF_SHA1_DIGEST]> for &'a oid {
198
208
}
199
209
}
200
210
201
- impl fmt:: Display for & oid {
202
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
211
+ impl std :: fmt:: Display for & oid {
212
+ fn fmt ( & self , f : & mut std :: fmt:: Formatter < ' _ > ) -> std :: fmt:: Result {
203
213
for b in self . as_bytes ( ) {
204
214
write ! ( f, "{b:02x}" ) ?;
205
215
}
206
216
Ok ( ( ) )
207
217
}
208
218
}
209
219
210
- impl PartialEq < crate :: ObjectId > for & oid {
220
+ impl PartialEq < ObjectId > for & oid {
211
221
fn eq ( & self , other : & ObjectId ) -> bool {
212
222
* self == other. as_ref ( )
213
223
}
0 commit comments