File tree Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ use std::{
5
5
error,
6
6
fmt,
7
7
result,
8
+ str:: FromStr ,
8
9
sync:: atomic:: { AtomicUsize , Ordering } ,
9
10
time:: SystemTime ,
10
11
} ;
@@ -79,6 +80,14 @@ impl Default for ObjectId {
79
80
}
80
81
}
81
82
83
+ impl FromStr for ObjectId {
84
+ type Err = Error ;
85
+
86
+ fn from_str ( s : & str ) -> std:: result:: Result < Self , Self :: Err > {
87
+ Self :: with_string ( s)
88
+ }
89
+ }
90
+
82
91
impl ObjectId {
83
92
/// Generates a new ObjectID, represented in bytes.
84
93
/// See the [docs](http://docs.mongodb.org/manual/reference/object-id/)
Original file line number Diff line number Diff line change @@ -47,3 +47,13 @@ fn counter_increasing() {
47
47
let oid2_bytes = ObjectId :: new ( ) . bytes ( ) ;
48
48
assert ! ( oid1_bytes[ 11 ] < oid2_bytes[ 11 ] ) ;
49
49
}
50
+
51
+ #[ test]
52
+ fn fromstr_oid ( ) {
53
+ let _guard = LOCK . run_concurrently ( ) ;
54
+ let s = "123456789012123456789012" ;
55
+ let oid_res = s. parse :: < ObjectId > ( ) ;
56
+ assert ! ( oid_res. is_ok( ) , "oid parse failed" ) ;
57
+ let actual_s = hex:: encode ( oid_res. unwrap ( ) . bytes ( ) ) ;
58
+ assert_eq ! ( s, & actual_s, "parsed and expected oids differ" ) ;
59
+ }
You can’t perform that action at this time.
0 commit comments