File tree 2 files changed +13
-2
lines changed 2 files changed +13
-2
lines changed Original file line number Diff line number Diff line change 22
22
//! BSON definition
23
23
24
24
use std:: {
25
+ convert:: { TryFrom , TryInto } ,
25
26
fmt:: { self , Debug , Display } ,
26
27
ops:: { Deref , DerefMut } ,
27
28
} ;
@@ -266,13 +267,22 @@ impl From<i64> for Bson {
266
267
267
268
impl From < u32 > for Bson {
268
269
fn from ( a : u32 ) -> Bson {
269
- Bson :: Int32 ( a as i32 )
270
+ if let Ok ( i) = i32:: try_from ( a) {
271
+ Bson :: Int32 ( i)
272
+ } else {
273
+ Bson :: Int64 ( a. into ( ) )
274
+ }
270
275
}
271
276
}
272
277
273
278
impl From < u64 > for Bson {
279
+ /// This conversion is lossy if the provided `u64` is greater than `i64::MAX`, which it will be
280
+ /// converted to. Using this `From` implementation is highly discouraged and it will be
281
+ /// removed in the next major version.
282
+ ///
283
+ /// Note: due to https://github.com/rust-lang/rust/issues/39935 we cannot deprecate this implementation.
274
284
fn from ( a : u64 ) -> Bson {
275
- Bson :: Int64 ( a as i64 )
285
+ Bson :: Int64 ( a. try_into ( ) . unwrap_or ( i64:: MAX ) )
276
286
}
277
287
}
278
288
Original file line number Diff line number Diff line change @@ -110,6 +110,7 @@ fn from_impls() {
110
110
assert_eq ! ( Bson :: from( -48i32 ) , Bson :: Int32 ( -48 ) ) ;
111
111
assert_eq ! ( Bson :: from( -96i64 ) , Bson :: Int64 ( -96 ) ) ;
112
112
assert_eq ! ( Bson :: from( 152u32 ) , Bson :: Int32 ( 152 ) ) ;
113
+ assert_eq ! ( Bson :: from( i32 :: MAX as u32 + 1 ) , Bson :: Int64 ( i32 :: MAX as i64 + 1 ) ) ;
113
114
assert_eq ! ( Bson :: from( 4096u64 ) , Bson :: Int64 ( 4096 ) ) ;
114
115
115
116
let oid = ObjectId :: new ( ) ;
You can’t perform that action at this time.
0 commit comments