@@ -84,7 +84,13 @@ impl FromStr for ObjectId {
8484 type Err = Error ;
8585
8686 fn from_str ( s : & str ) -> std:: result:: Result < Self , Self :: Err > {
87- Self :: with_string ( s)
87+ Self :: parse_str ( s)
88+ }
89+ }
90+
91+ impl From < [ u8 ; 12 ] > for ObjectId {
92+ fn from ( bytes : [ u8 ; 12 ] ) -> Self {
93+ Self { id : bytes }
8894 }
8995}
9096
@@ -105,25 +111,25 @@ impl ObjectId {
105111 buf[ COUNTER_OFFSET ..( COUNTER_SIZE + COUNTER_OFFSET ) ]
106112 . clone_from_slice ( & counter[ ..COUNTER_SIZE ] ) ;
107113
108- ObjectId :: with_bytes ( buf)
114+ ObjectId :: from_bytes ( buf)
109115 }
110116
111117 /// Constructs a new ObjectId wrapper around the raw byte representation.
112- pub fn with_bytes ( bytes : [ u8 ; 12 ] ) -> ObjectId {
118+ pub const fn from_bytes ( bytes : [ u8 ; 12 ] ) -> ObjectId {
113119 ObjectId { id : bytes }
114120 }
115121
116122 /// Creates an ObjectID using a 12-byte (24-char) hexadecimal string.
117- pub fn with_string ( s : & str ) -> Result < ObjectId > {
118- let bytes: Vec < u8 > = hex:: decode ( s. as_bytes ( ) ) ?;
123+ pub fn parse_str ( s : impl AsRef < str > ) -> Result < ObjectId > {
124+ let bytes: Vec < u8 > = hex:: decode ( s. as_ref ( ) . as_bytes ( ) ) ?;
119125 if bytes. len ( ) != 12 {
120126 Err ( Error :: ArgumentError {
121127 message : "Provided string must be a 12-byte hexadecimal string." . to_owned ( ) ,
122128 } )
123129 } else {
124130 let mut byte_array: [ u8 ; 12 ] = [ 0 ; 12 ] ;
125131 byte_array[ ..] . copy_from_slice ( & bytes[ ..] ) ;
126- Ok ( ObjectId :: with_bytes ( byte_array) )
132+ Ok ( ObjectId :: from_bytes ( byte_array) )
127133 }
128134 }
129135
@@ -268,33 +274,33 @@ mod test {
268274
269275 #[ test]
270276 fn test_display ( ) {
271- let id = super :: ObjectId :: with_string ( "53e37d08776f724e42000000" ) . unwrap ( ) ;
277+ let id = super :: ObjectId :: parse_str ( "53e37d08776f724e42000000" ) . unwrap ( ) ;
272278
273279 assert_eq ! ( format!( "{}" , id) , "53e37d08776f724e42000000" )
274280 }
275281
276282 #[ test]
277283 fn test_debug ( ) {
278- let id = super :: ObjectId :: with_string ( "53e37d08776f724e42000000" ) . unwrap ( ) ;
284+ let id = super :: ObjectId :: parse_str ( "53e37d08776f724e42000000" ) . unwrap ( ) ;
279285
280286 assert_eq ! ( format!( "{:?}" , id) , "ObjectId(53e37d08776f724e42000000)" )
281287 }
282288
283289 #[ test]
284290 fn test_timestamp ( ) {
285- let id = super :: ObjectId :: with_string ( "000000000000000000000000" ) . unwrap ( ) ;
291+ let id = super :: ObjectId :: parse_str ( "000000000000000000000000" ) . unwrap ( ) ;
286292 // "Jan 1st, 1970 00:00:00 UTC"
287293 assert_eq ! ( Utc . ymd( 1970 , 1 , 1 ) . and_hms( 0 , 0 , 0 ) , id. timestamp( ) ) ;
288294
289- let id = super :: ObjectId :: with_string ( "7FFFFFFF0000000000000000" ) . unwrap ( ) ;
295+ let id = super :: ObjectId :: parse_str ( "7FFFFFFF0000000000000000" ) . unwrap ( ) ;
290296 // "Jan 19th, 2038 03:14:07 UTC"
291297 assert_eq ! ( Utc . ymd( 2038 , 1 , 19 ) . and_hms( 3 , 14 , 7 ) , id. timestamp( ) ) ;
292298
293- let id = super :: ObjectId :: with_string ( "800000000000000000000000" ) . unwrap ( ) ;
299+ let id = super :: ObjectId :: parse_str ( "800000000000000000000000" ) . unwrap ( ) ;
294300 // "Jan 19th, 2038 03:14:08 UTC"
295301 assert_eq ! ( Utc . ymd( 2038 , 1 , 19 ) . and_hms( 3 , 14 , 8 ) , id. timestamp( ) ) ;
296302
297- let id = super :: ObjectId :: with_string ( "FFFFFFFF0000000000000000" ) . unwrap ( ) ;
303+ let id = super :: ObjectId :: parse_str ( "FFFFFFFF0000000000000000" ) . unwrap ( ) ;
298304 // "Feb 7th, 2106 06:28:15 UTC"
299305 assert_eq ! ( Utc . ymd( 2106 , 2 , 7 ) . and_hms( 6 , 28 , 15 ) , id. timestamp( ) ) ;
300306 }
0 commit comments