File tree Expand file tree Collapse file tree 2 files changed +28
-8
lines changed Expand file tree Collapse file tree 2 files changed +28
-8
lines changed Original file line number Diff line number Diff line change @@ -190,16 +190,19 @@ impl OrderedDocument {
190
190
191
191
/// Sets the value of the entry with the OccupiedEntry's key,
192
192
/// and returns the entry's old value.
193
- pub fn insert ( & mut self , key : String , val : Bson ) -> Option < Bson > {
194
- let key_slice = & key[ ..] ;
195
-
196
- if self . contains_key ( key_slice) {
197
- let position = self . position ( key_slice) . unwrap ( ) ;
198
- self . keys . remove ( position) ;
193
+ pub fn insert < KT : Into < String > > ( & mut self , key : KT , val : Bson ) -> Option < Bson > {
194
+ let key = key. into ( ) ;
195
+
196
+ {
197
+ let key_slice = & key[ ..] ;
198
+ if self . contains_key ( key_slice) {
199
+ let position = self . position ( key_slice) . unwrap ( ) ;
200
+ self . keys . remove ( position) ;
201
+ }
199
202
}
200
203
201
204
self . keys . push ( key. to_owned ( ) ) ;
202
- self . document . insert ( key. to_owned ( ) , val. to_owned ( ) )
205
+ self . document . insert ( key, val. to_owned ( ) )
203
206
}
204
207
205
208
/// Takes the value of the entry out of the document, and returns it.
Original file line number Diff line number Diff line change @@ -11,7 +11,24 @@ fn ordered_insert() {
11
11
"first" . to_owned( ) ,
12
12
"second" . to_owned( ) ,
13
13
"alphanumeric" . to_owned( ) ,
14
- ) ;
14
+ ) ;
15
+
16
+ let keys: Vec < _ > = doc. iter ( ) . map ( |( key, _) | key. to_owned ( ) ) . collect ( ) ;
17
+ assert_eq ! ( expected_keys, keys) ;
18
+ }
19
+
20
+ #[ test]
21
+ fn ordered_insert_shorthand ( ) {
22
+ let mut doc = Document :: new ( ) ;
23
+ doc. insert ( "first" , Bson :: I32 ( 1 ) ) ;
24
+ doc. insert ( "second" , Bson :: String ( "foo" . to_owned ( ) ) ) ;
25
+ doc. insert ( "alphanumeric" , Bson :: String ( "bar" . to_owned ( ) ) ) ;
26
+
27
+ let expected_keys = vec ! (
28
+ "first" . to_owned( ) ,
29
+ "second" . to_owned( ) ,
30
+ "alphanumeric" . to_owned( ) ,
31
+ ) ;
15
32
16
33
let keys: Vec < _ > = doc. iter ( ) . map ( |( key, _) | key. to_owned ( ) ) . collect ( ) ;
17
34
assert_eq ! ( expected_keys, keys) ;
You can’t perform that action at this time.
0 commit comments