@@ -87,71 +87,71 @@ impl Default for Bson {
87
87
88
88
impl Debug for Bson {
89
89
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
90
- match self {
91
- & Bson :: FloatingPoint ( p) => write ! ( f, "FloatingPoint({:?})" , p) ,
92
- & Bson :: String ( ref s) => write ! ( f, "String({:?})" , s) ,
93
- & Bson :: Array ( ref vec) => write ! ( f, "Array({:?})" , vec) ,
94
- & Bson :: Document ( ref doc) => write ! ( f, "Document({:?})" , doc) ,
95
- & Bson :: Boolean ( b) => write ! ( f, "Boolean({:?})" , b) ,
96
- & Bson :: Null => write ! ( f, "Null" ) ,
97
- & Bson :: RegExp ( ref pat, ref opt) => write ! ( f, "RegExp(/{:?}/{:?})" , pat, opt) ,
98
- & Bson :: JavaScriptCode ( ref s) => write ! ( f, "JavaScriptCode({:?})" , s) ,
99
- & Bson :: JavaScriptCodeWithScope ( ref s, ref scope) => {
90
+ match * self {
91
+ Bson :: FloatingPoint ( p) => write ! ( f, "FloatingPoint({:?})" , p) ,
92
+ Bson :: String ( ref s) => write ! ( f, "String({:?})" , s) ,
93
+ Bson :: Array ( ref vec) => write ! ( f, "Array({:?})" , vec) ,
94
+ Bson :: Document ( ref doc) => write ! ( f, "Document({:?})" , doc) ,
95
+ Bson :: Boolean ( b) => write ! ( f, "Boolean({:?})" , b) ,
96
+ Bson :: Null => write ! ( f, "Null" ) ,
97
+ Bson :: RegExp ( ref pat, ref opt) => write ! ( f, "RegExp(/{:?}/{:?})" , pat, opt) ,
98
+ Bson :: JavaScriptCode ( ref s) => write ! ( f, "JavaScriptCode({:?})" , s) ,
99
+ Bson :: JavaScriptCodeWithScope ( ref s, ref scope) => {
100
100
write ! ( f, "JavaScriptCodeWithScope({:?}, {:?})" , s, scope)
101
101
}
102
- & Bson :: I32 ( v) => write ! ( f, "I32({:?})" , v) ,
103
- & Bson :: I64 ( v) => write ! ( f, "I64({:?})" , v) ,
104
- & Bson :: TimeStamp ( i) => {
102
+ Bson :: I32 ( v) => write ! ( f, "I32({:?})" , v) ,
103
+ Bson :: I64 ( v) => write ! ( f, "I64({:?})" , v) ,
104
+ Bson :: TimeStamp ( i) => {
105
105
let time = ( i >> 32 ) as i32 ;
106
106
let inc = ( i & 0xFFFFFFFF ) as i32 ;
107
107
108
108
write ! ( f, "TimeStamp({}, {})" , time, inc)
109
109
}
110
- & Bson :: Binary ( t, ref vec) => write ! ( f, "BinData({}, 0x{})" , u8 :: from( t) , hex:: encode( vec) ) ,
111
- & Bson :: ObjectId ( ref id) => write ! ( f, "ObjectId({:?})" , id) ,
112
- & Bson :: UtcDatetime ( date_time) => write ! ( f, "UtcDatetime({:?})" , date_time) ,
113
- & Bson :: Symbol ( ref sym) => write ! ( f, "Symbol({:?})" , sym) ,
110
+ Bson :: Binary ( t, ref vec) => write ! ( f, "BinData({}, 0x{})" , u8 :: from( t) , hex:: encode( vec) ) ,
111
+ Bson :: ObjectId ( ref id) => write ! ( f, "ObjectId({:?})" , id) ,
112
+ Bson :: UtcDatetime ( date_time) => write ! ( f, "UtcDatetime({:?})" , date_time) ,
113
+ Bson :: Symbol ( ref sym) => write ! ( f, "Symbol({:?})" , sym) ,
114
114
}
115
115
}
116
116
}
117
117
118
118
impl Display for Bson {
119
119
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
120
- match self {
121
- & Bson :: FloatingPoint ( f) => write ! ( fmt, "{}" , f) ,
122
- & Bson :: String ( ref s) => write ! ( fmt, "\" {}\" " , s) ,
123
- & Bson :: Array ( ref vec) => {
124
- write ! ( fmt, "[" ) ?;
120
+ match * self {
121
+ Bson :: FloatingPoint ( f) => write ! ( fmt, "{}" , f) ,
122
+ Bson :: String ( ref s) => write ! ( fmt, "\" {}\" " , s) ,
123
+ Bson :: Array ( ref vec) => {
124
+ fmt. write_str ( "[" ) ?;
125
125
126
126
let mut first = true ;
127
- for bson in vec. iter ( ) {
127
+ for bson in vec {
128
128
if !first {
129
- write ! ( fmt, ", " ) ?;
129
+ fmt. write_str ( ", " ) ?;
130
130
}
131
131
132
132
write ! ( fmt, "{}" , bson) ?;
133
133
first = false ;
134
134
}
135
135
136
- write ! ( fmt, "]" )
136
+ fmt. write_str ( "]" )
137
137
}
138
- & Bson :: Document ( ref doc) => write ! ( fmt, "{}" , doc) ,
139
- & Bson :: Boolean ( b) => write ! ( fmt, "{}" , b) ,
140
- & Bson :: Null => write ! ( fmt, "null" ) ,
141
- & Bson :: RegExp ( ref pat, ref opt) => write ! ( fmt, "/{}/{}" , pat, opt) ,
142
- & Bson :: JavaScriptCode ( ref s) | & Bson :: JavaScriptCodeWithScope ( ref s, _) => fmt. write_str ( & s) ,
143
- & Bson :: I32 ( i) => write ! ( fmt, "{}" , i) ,
144
- & Bson :: I64 ( i) => write ! ( fmt, "{}" , i) ,
145
- & Bson :: TimeStamp ( i) => {
138
+ Bson :: Document ( ref doc) => write ! ( fmt, "{}" , doc) ,
139
+ Bson :: Boolean ( b) => write ! ( fmt, "{}" , b) ,
140
+ Bson :: Null => write ! ( fmt, "null" ) ,
141
+ Bson :: RegExp ( ref pat, ref opt) => write ! ( fmt, "/{}/{}" , pat, opt) ,
142
+ Bson :: JavaScriptCode ( ref s) | Bson :: JavaScriptCodeWithScope ( ref s, _) => fmt. write_str ( & s) ,
143
+ Bson :: I32 ( i) => write ! ( fmt, "{}" , i) ,
144
+ Bson :: I64 ( i) => write ! ( fmt, "{}" , i) ,
145
+ Bson :: TimeStamp ( i) => {
146
146
let time = ( i >> 32 ) as i32 ;
147
147
let inc = ( i & 0xFFFFFFFF ) as i32 ;
148
148
149
149
write ! ( fmt, "Timestamp({}, {})" , time, inc)
150
150
}
151
- & Bson :: Binary ( t, ref vec) => write ! ( fmt, "BinData({}, 0x{})" , u8 :: from( t) , hex:: encode( vec) ) ,
152
- & Bson :: ObjectId ( ref id) => write ! ( fmt, "ObjectId(\" {}\" )" , id) ,
153
- & Bson :: UtcDatetime ( date_time) => write ! ( fmt, "Date(\" {}\" )" , date_time) ,
154
- & Bson :: Symbol ( ref sym) => write ! ( fmt, "Symbol(\" {}\" )" , sym) ,
151
+ Bson :: Binary ( t, ref vec) => write ! ( fmt, "BinData({}, 0x{})" , u8 :: from( t) , hex:: encode( vec) ) ,
152
+ Bson :: ObjectId ( ref id) => write ! ( fmt, "ObjectId(\" {}\" )" , id) ,
153
+ Bson :: UtcDatetime ( date_time) => write ! ( fmt, "Date(\" {}\" )" , date_time) ,
154
+ Bson :: Symbol ( ref sym) => write ! ( fmt, "Symbol(\" {}\" )" , sym) ,
155
155
}
156
156
}
157
157
}
@@ -205,23 +205,20 @@ impl From<bool> for Bson {
205
205
}
206
206
207
207
impl From < ( String , String ) > for Bson {
208
- fn from ( a : ( String , String ) ) -> Bson {
209
- let ( a1, a2) = a;
210
- Bson :: RegExp ( a1. to_owned ( ) , a2. to_owned ( ) )
208
+ fn from ( ( pat, opt) : ( String , String ) ) -> Bson {
209
+ Bson :: RegExp ( pat, opt)
211
210
}
212
211
}
213
212
214
213
impl From < ( String , Document ) > for Bson {
215
- fn from ( a : ( String , Document ) ) -> Bson {
216
- let ( a1, a2) = a;
217
- Bson :: JavaScriptCodeWithScope ( a1, a2)
214
+ fn from ( ( code, scope) : ( String , Document ) ) -> Bson {
215
+ Bson :: JavaScriptCodeWithScope ( code, scope)
218
216
}
219
217
}
220
218
221
219
impl From < ( BinarySubtype , Vec < u8 > ) > for Bson {
222
- fn from ( a : ( BinarySubtype , Vec < u8 > ) ) -> Bson {
223
- let ( a1, a2) = a;
224
- Bson :: Binary ( a1, a2)
220
+ fn from ( ( ty, data) : ( BinarySubtype , Vec < u8 > ) ) -> Bson {
221
+ Bson :: Binary ( ty, data)
225
222
}
226
223
}
227
224
@@ -257,7 +254,7 @@ impl From<[u8; 12]> for Bson {
257
254
258
255
impl From < oid:: ObjectId > for Bson {
259
256
fn from ( a : oid:: ObjectId ) -> Bson {
260
- Bson :: ObjectId ( a. to_owned ( ) )
257
+ Bson :: ObjectId ( a)
261
258
}
262
259
}
263
260
@@ -279,16 +276,16 @@ impl From<Value> for Bson {
279
276
Value :: Bool ( x) => x. into ( ) ,
280
277
Value :: Array ( x) => Bson :: Array ( x. into_iter ( ) . map ( Bson :: from) . collect ( ) ) ,
281
278
Value :: Object ( x) => {
282
- Bson :: from_extended_document ( x. into_iter ( ) . map ( |( k, v) | ( k. clone ( ) , v. into ( ) ) ) . collect ( ) )
279
+ Bson :: from_extended_document ( x. into_iter ( ) . map ( |( k, v) | ( k, v. into ( ) ) ) . collect ( ) )
283
280
}
284
281
Value :: Null => Bson :: Null ,
285
282
}
286
283
}
287
284
}
288
285
289
- impl Into < Value > for Bson {
290
- fn into ( self ) -> Value {
291
- match self {
286
+ impl From < Bson > for Value {
287
+ fn from ( bson : Bson ) -> Self {
288
+ match bson {
292
289
Bson :: FloatingPoint ( v) => json ! ( v) ,
293
290
Bson :: String ( v) => json ! ( v) ,
294
291
Bson :: Array ( v) => json ! ( v) ,
@@ -336,23 +333,23 @@ impl Into<Value> for Bson {
336
333
impl Bson {
337
334
/// Get the `ElementType` of this value.
338
335
pub fn element_type ( & self ) -> ElementType {
339
- match self {
340
- & Bson :: FloatingPoint ( ..) => ElementType :: FloatingPoint ,
341
- & Bson :: String ( ..) => ElementType :: Utf8String ,
342
- & Bson :: Array ( ..) => ElementType :: Array ,
343
- & Bson :: Document ( ..) => ElementType :: EmbeddedDocument ,
344
- & Bson :: Boolean ( ..) => ElementType :: Boolean ,
345
- & Bson :: Null => ElementType :: NullValue ,
346
- & Bson :: RegExp ( ..) => ElementType :: RegularExpression ,
347
- & Bson :: JavaScriptCode ( ..) => ElementType :: JavaScriptCode ,
348
- & Bson :: JavaScriptCodeWithScope ( ..) => ElementType :: JavaScriptCodeWithScope ,
349
- & Bson :: I32 ( ..) => ElementType :: Integer32Bit ,
350
- & Bson :: I64 ( ..) => ElementType :: Integer64Bit ,
351
- & Bson :: TimeStamp ( ..) => ElementType :: TimeStamp ,
352
- & Bson :: Binary ( ..) => ElementType :: Binary ,
353
- & Bson :: ObjectId ( ..) => ElementType :: ObjectId ,
354
- & Bson :: UtcDatetime ( ..) => ElementType :: UtcDatetime ,
355
- & Bson :: Symbol ( ..) => ElementType :: Symbol ,
336
+ match * self {
337
+ Bson :: FloatingPoint ( ..) => ElementType :: FloatingPoint ,
338
+ Bson :: String ( ..) => ElementType :: Utf8String ,
339
+ Bson :: Array ( ..) => ElementType :: Array ,
340
+ Bson :: Document ( ..) => ElementType :: EmbeddedDocument ,
341
+ Bson :: Boolean ( ..) => ElementType :: Boolean ,
342
+ Bson :: Null => ElementType :: NullValue ,
343
+ Bson :: RegExp ( ..) => ElementType :: RegularExpression ,
344
+ Bson :: JavaScriptCode ( ..) => ElementType :: JavaScriptCode ,
345
+ Bson :: JavaScriptCodeWithScope ( ..) => ElementType :: JavaScriptCodeWithScope ,
346
+ Bson :: I32 ( ..) => ElementType :: Integer32Bit ,
347
+ Bson :: I64 ( ..) => ElementType :: Integer64Bit ,
348
+ Bson :: TimeStamp ( ..) => ElementType :: TimeStamp ,
349
+ Bson :: Binary ( ..) => ElementType :: Binary ,
350
+ Bson :: ObjectId ( ..) => ElementType :: ObjectId ,
351
+ Bson :: UtcDatetime ( ..) => ElementType :: UtcDatetime ,
352
+ Bson :: Symbol ( ..) => ElementType :: Symbol ,
356
353
}
357
354
}
358
355
@@ -437,7 +434,7 @@ impl Bson {
437
434
}
438
435
439
436
/// Converts from extended format.
440
- /// This function mainly used for [extended JSON format](https://docs.mongodb.com/manual/reference/mongodb-extended-json/).
437
+ /// This function is mainly used for [extended JSON format](https://docs.mongodb.com/manual/reference/mongodb-extended-json/).
441
438
#[ doc( hidden) ]
442
439
pub fn from_extended_document ( values : Document ) -> Bson {
443
440
if values. len ( ) == 2 {
@@ -478,7 +475,7 @@ impl Bson {
478
475
/// If `Bson` is `FloatingPoint`, return its value. Returns `None` otherwise
479
476
pub fn as_f64 ( & self ) -> Option < f64 > {
480
477
match * self {
481
- Bson :: FloatingPoint ( ref v) => Some ( * v) ,
478
+ Bson :: FloatingPoint ( v) => Some ( v) ,
482
479
_ => None ,
483
480
}
484
481
}
@@ -510,23 +507,23 @@ impl Bson {
510
507
/// If `Bson` is `Boolean`, return its value. Returns `None` otherwise
511
508
pub fn as_bool ( & self ) -> Option < bool > {
512
509
match * self {
513
- Bson :: Boolean ( ref v) => Some ( * v) ,
510
+ Bson :: Boolean ( v) => Some ( v) ,
514
511
_ => None ,
515
512
}
516
513
}
517
514
518
515
/// If `Bson` is `I32`, return its value. Returns `None` otherwise
519
516
pub fn as_i32 ( & self ) -> Option < i32 > {
520
517
match * self {
521
- Bson :: I32 ( ref v) => Some ( * v) ,
518
+ Bson :: I32 ( v) => Some ( v) ,
522
519
_ => None ,
523
520
}
524
521
}
525
522
526
523
/// If `Bson` is `I64`, return its value. Returns `None` otherwise
527
524
pub fn as_i64 ( & self ) -> Option < i64 > {
528
525
match * self {
529
- Bson :: I64 ( ref v) => Some ( * v) ,
526
+ Bson :: I64 ( v) => Some ( v) ,
530
527
_ => None ,
531
528
}
532
529
}
@@ -558,7 +555,7 @@ impl Bson {
558
555
/// If `Bson` is `TimeStamp`, return its value. Returns `None` otherwise
559
556
pub fn as_timestamp ( & self ) -> Option < i64 > {
560
557
match * self {
561
- Bson :: TimeStamp ( ref v) => Some ( * v) ,
558
+ Bson :: TimeStamp ( v) => Some ( v) ,
562
559
_ => None ,
563
560
}
564
561
}
@@ -587,7 +584,7 @@ impl Bson {
587
584
/// timestamp: TimeStamp,
588
585
/// }
589
586
/// ```
590
- #[ derive( Debug , Eq , PartialEq , Clone ) ]
587
+ #[ derive( Debug , Eq , PartialEq , Clone , Copy , Hash ) ]
591
588
pub struct TimeStamp {
592
589
pub t : u32 ,
593
590
pub i : u32 ,
0 commit comments