1
1
use bson:: Bson ;
2
2
use std:: collections:: BTreeMap ;
3
3
use std:: iter:: { FromIterator , Map } ;
4
+ use std:: vec:: IntoIter ;
5
+ use std:: slice;
4
6
5
7
/// A BSON document represented as an associative BTree Map with insertion ordering.
6
8
#[ derive( Debug , Clone ) ]
@@ -10,17 +12,15 @@ pub struct OrderedDocument {
10
12
}
11
13
12
14
/// An iterator over OrderedDocument entries.
13
- #[ derive( Clone ) ]
14
15
pub struct OrderedDocumentIntoIterator {
15
- ordered_document : OrderedDocument ,
16
- index : usize ,
16
+ vec_iter : IntoIter < String > ,
17
+ document : BTreeMap < String , Bson > ,
17
18
}
18
19
19
20
/// An owning iterator over OrderedDocument entries.
20
- #[ derive( Clone ) ]
21
21
pub struct OrderedDocumentIterator < ' a > {
22
- ordered_document : & ' a OrderedDocument ,
23
- index : usize ,
22
+ vec_iter : slice :: Iter < ' a , String > ,
23
+ document : & ' a BTreeMap < String , Bson > ,
24
24
}
25
25
26
26
/// An iterator over an OrderedDocument's keys.
@@ -33,19 +33,11 @@ pub struct Values<'a> {
33
33
inner : Map < OrderedDocumentIterator < ' a > , fn ( ( & ' a String , & ' a Bson ) ) -> & ' a Bson >
34
34
}
35
35
36
- impl < ' a > Clone for Keys < ' a > {
37
- fn clone ( & self ) -> Keys < ' a > { Keys { inner : self . inner . clone ( ) } }
38
- }
39
-
40
36
impl < ' a > Iterator for Keys < ' a > {
41
37
type Item = & ' a String ;
42
38
fn next ( & mut self ) -> Option < ( & ' a String ) > { self . inner . next ( ) }
43
39
}
44
40
45
- impl < ' a > Clone for Values < ' a > {
46
- fn clone ( & self ) -> Values < ' a > { Values { inner : self . inner . clone ( ) } }
47
- }
48
-
49
41
impl < ' a > Iterator for Values < ' a > {
50
42
type Item = & ' a Bson ;
51
43
@@ -57,7 +49,10 @@ impl IntoIterator for OrderedDocument {
57
49
type IntoIter = OrderedDocumentIntoIterator ;
58
50
59
51
fn into_iter ( self ) -> Self :: IntoIter {
60
- OrderedDocumentIntoIterator { ordered_document : self , index : 0 }
52
+ OrderedDocumentIntoIterator {
53
+ document : self . document ,
54
+ vec_iter : self . keys . into_iter ( )
55
+ }
61
56
}
62
57
}
63
58
@@ -66,7 +61,11 @@ impl<'a> IntoIterator for &'a OrderedDocument {
66
61
type IntoIter = OrderedDocumentIterator < ' a > ;
67
62
68
63
fn into_iter ( self ) -> Self :: IntoIter {
69
- OrderedDocumentIterator { ordered_document : self , index : 0 }
64
+ let ref keys = self . keys ;
65
+ OrderedDocumentIterator {
66
+ vec_iter : keys. into_iter ( ) ,
67
+ document : & self . document ,
68
+ }
70
69
}
71
70
}
72
71
@@ -83,28 +82,23 @@ impl FromIterator<(String, Bson)> for OrderedDocument {
83
82
impl < ' a > Iterator for OrderedDocumentIntoIterator {
84
83
type Item = ( String , Bson ) ;
85
84
fn next ( & mut self ) -> Option < ( String , Bson ) > {
86
- if self . ordered_document . keys . len ( ) <= self . index {
87
- return None ;
85
+ match self . vec_iter . next ( ) {
86
+ Some ( key) => {
87
+ let val = self . document . get ( & key[ ..] ) . unwrap ( ) ;
88
+ Some ( ( key, val. to_owned ( ) ) )
89
+ } ,
90
+ None => None ,
88
91
}
89
-
90
- let ref key = self . ordered_document . keys [ self . index ] ;
91
- let val = self . ordered_document . get ( & key[ ..] ) . unwrap ( ) ;
92
- self . index += 1 ;
93
- Some ( ( key. to_owned ( ) , val. to_owned ( ) ) )
94
92
}
95
93
}
96
94
97
95
impl < ' a > Iterator for OrderedDocumentIterator < ' a > {
98
96
type Item = ( & ' a String , & ' a Bson ) ;
99
97
fn next ( & mut self ) -> Option < ( & ' a String , & ' a Bson ) > {
100
- if self . ordered_document . keys . len ( ) <= self . index {
101
- return None ;
98
+ match self . vec_iter . next ( ) {
99
+ Some ( key) => Some ( ( & key, self . document . get ( & key[ ..] ) . unwrap ( ) ) ) ,
100
+ None => None ,
102
101
}
103
-
104
- let ref key = self . ordered_document . keys [ self . index ] ;
105
- let val = self . ordered_document . get ( & key[ ..] ) . unwrap ( ) ;
106
- self . index += 1 ;
107
- Some ( ( key, val) )
108
102
}
109
103
}
110
104
0 commit comments