@@ -57,36 +57,37 @@ fn run_test<T>(expected_value: &T, expected_doc: &Document, description: &str)
5757where
5858 T : Serialize + DeserializeOwned + PartialEq + std:: fmt:: Debug ,
5959{
60- let expected_bytes = expected_doc. to_vec ( ) ;
60+ let expected_bytes = expected_doc. encode_to_vec ( ) . expect ( description ) ;
6161
62- let expected_bytes_serde = bson:: to_vec ( & expected_value) . expect ( description) ;
62+ let expected_bytes_serde = bson:: serialize_to_vec ( & expected_value) . expect ( description) ;
6363
6464 assert_eq ! ( expected_bytes_serde, expected_bytes, "{}" , description) ;
6565
66- let expected_bytes_from_doc_serde = bson:: to_vec ( & expected_doc) . expect ( description) ;
66+ let expected_bytes_from_doc_serde = bson:: serialize_to_vec ( & expected_doc) . expect ( description) ;
6767 assert_eq ! (
6868 expected_bytes_from_doc_serde, expected_bytes,
6969 "{}" ,
7070 description
7171 ) ;
7272
73- let serialized_doc = bson:: to_document ( & expected_value) . expect ( description) ;
73+ let serialized_doc = bson:: serialize_to_document ( & expected_value) . expect ( description) ;
7474 assert_eq ! ( & serialized_doc, expected_doc, "{}" , description) ;
7575 assert_eq ! (
7676 expected_value,
77- & bson:: from_document :: <T >( serialized_doc) . expect( description) ,
77+ & bson:: deserialize_from_document :: <T >( serialized_doc) . expect( description) ,
7878 "{}" ,
7979 description
8080 ) ;
8181
8282 assert_eq ! (
83- & bson:: from_reader :: <_, T >( expected_bytes. as_slice( ) ) . expect( description) ,
83+ & bson:: deserialize_from_reader :: <_, T >( expected_bytes. as_slice( ) ) . expect( description) ,
8484 expected_value,
8585 "{}" ,
8686 description
8787 ) ;
8888 assert_eq ! (
89- & bson:: from_reader:: <_, Document >( expected_bytes. as_slice( ) ) . expect( description) ,
89+ & bson:: deserialize_from_reader:: <_, Document >( expected_bytes. as_slice( ) )
90+ . expect( description) ,
9091 expected_doc,
9192 "{}" ,
9293 description
@@ -101,22 +102,23 @@ fn run_deserialize_test<T>(expected_value: &T, expected_doc: &Document, descript
101102where
102103 T : DeserializeOwned + PartialEq + std:: fmt:: Debug ,
103104{
104- let expected_bytes = expected_doc. to_vec ( ) ;
105+ let expected_bytes = expected_doc. encode_to_vec ( ) . expect ( description ) ;
105106
106107 assert_eq ! (
107- & bson:: from_document :: <T >( expected_doc. clone( ) ) . expect( description) ,
108+ & bson:: deserialize_from_document :: <T >( expected_doc. clone( ) ) . expect( description) ,
108109 expected_value,
109110 "{}" ,
110111 description
111112 ) ;
112113 assert_eq ! (
113- & bson:: from_reader :: <_, T >( expected_bytes. as_slice( ) ) . expect( description) ,
114+ & bson:: deserialize_from_reader :: <_, T >( expected_bytes. as_slice( ) ) . expect( description) ,
114115 expected_value,
115116 "{}" ,
116117 description
117118 ) ;
118119 assert_eq ! (
119- & bson:: from_reader:: <_, Document >( expected_bytes. as_slice( ) ) . expect( description) ,
120+ & bson:: deserialize_from_reader:: <_, Document >( expected_bytes. as_slice( ) )
121+ . expect( description) ,
120122 expected_doc,
121123 "{}" ,
122124 description
@@ -130,8 +132,8 @@ fn run_raw_round_trip_test<'de, T>(bytes: &'de [u8], description: &str)
130132where
131133 T : Deserialize < ' de > + Serialize + std:: fmt:: Debug ,
132134{
133- let t: T = bson:: from_slice ( bytes) . expect ( description) ;
134- let vec = bson:: to_vec ( & t) . expect ( description) ;
135+ let t: T = bson:: deserialize_from_slice ( bytes) . expect ( description) ;
136+ let vec = bson:: serialize_to_vec ( & t) . expect ( description) ;
135137 assert_eq ! ( vec. as_slice( ) , bytes) ;
136138}
137139
@@ -438,12 +440,12 @@ fn type_conversion() {
438440 let doc = doc ! {
439441 "bar" : 1_i64
440442 } ;
441- let deserialized: Foo = bson:: from_document ( doc. clone ( ) ) . unwrap ( ) ;
443+ let deserialized: Foo = bson:: deserialize_from_document ( doc. clone ( ) ) . unwrap ( ) ;
442444 assert_eq ! ( deserialized, v) ;
443445
444- let bytes = doc. to_vec ( ) ;
446+ let bytes = doc. encode_to_vec ( ) . unwrap ( ) ;
445447
446- let bson_deserialized: Foo = bson:: from_reader ( bytes. as_slice ( ) ) . unwrap ( ) ;
448+ let bson_deserialized: Foo = bson:: deserialize_from_reader ( bytes. as_slice ( ) ) . unwrap ( ) ;
447449 assert_eq ! ( bson_deserialized, v) ;
448450}
449451
@@ -456,11 +458,11 @@ fn missing_errors() {
456458
457459 let doc = doc ! { } ;
458460
459- bson:: from_document :: < Foo > ( doc. clone ( ) ) . unwrap_err ( ) ;
461+ bson:: deserialize_from_document :: < Foo > ( doc. clone ( ) ) . unwrap_err ( ) ;
460462
461- let bytes = doc. to_vec ( ) ;
463+ let bytes = doc. encode_to_vec ( ) . unwrap ( ) ;
462464
463- bson:: from_reader :: < _ , Foo > ( bytes. as_slice ( ) ) . unwrap_err ( ) ;
465+ bson:: deserialize_from_reader :: < _ , Foo > ( bytes. as_slice ( ) ) . unwrap_err ( ) ;
464466}
465467
466468#[ test]
@@ -674,10 +676,12 @@ fn unused_fields_deny() {
674676 "a" : 1 ,
675677 "b" : 2 ,
676678 } ;
677- bson:: from_document :: < Foo > ( doc. clone ( ) ) . expect_err ( "extra fields should cause failure" ) ;
679+ bson:: deserialize_from_document :: < Foo > ( doc. clone ( ) )
680+ . expect_err ( "extra fields should cause failure" ) ;
678681
679- let bytes = doc. to_vec ( ) ;
680- bson:: from_reader :: < _ , Foo > ( bytes. as_slice ( ) ) . expect_err ( "extra fields should cause failure" ) ;
682+ let bytes = doc. encode_to_vec ( ) . unwrap ( ) ;
683+ bson:: deserialize_from_reader :: < _ , Foo > ( bytes. as_slice ( ) )
684+ . expect_err ( "extra fields should cause failure" ) ;
681685}
682686
683687#[ test]
@@ -732,7 +736,7 @@ fn raw_doc_buf() {
732736 d : RawDocumentBuf ,
733737 }
734738
735- let bytes = bson:: to_vec ( & doc ! {
739+ let bytes = bson:: serialize_to_vec ( & doc ! {
736740 "d" : {
737741 "a" : 12 ,
738742 "b" : 5.5 ,
@@ -754,7 +758,7 @@ fn raw_doc() {
754758 d : & ' a RawDocument ,
755759 }
756760
757- let bytes = bson:: to_vec ( & doc ! {
761+ let bytes = bson:: serialize_to_vec ( & doc ! {
758762 "d" : {
759763 "a" : 12 ,
760764 "b" : 5.5 ,
@@ -776,7 +780,7 @@ fn raw_array() {
776780 d : & ' a RawArray ,
777781 }
778782
779- let bytes = bson:: to_vec ( & doc ! {
783+ let bytes = bson:: serialize_to_vec ( & doc ! {
780784 "d" : [ 1 , true , { "ok" : 1 } , [ "sub" , "array" ] , Uuid :: new( ) ]
781785 } )
782786 . expect ( "raw_array" ) ;
@@ -801,7 +805,7 @@ fn raw_binary() {
801805 other : RawBinaryRef < ' a > ,
802806 }
803807
804- let bytes = bson:: to_vec ( & doc ! {
808+ let bytes = bson:: serialize_to_vec ( & doc ! {
805809 "generic" : Binary {
806810 bytes: vec![ 1 , 2 , 3 , 4 , 5 ] ,
807811 subtype: BinarySubtype :: Generic ,
@@ -829,7 +833,7 @@ fn raw_regex() {
829833 r : RawRegexRef < ' a > ,
830834 }
831835
832- let bytes = bson:: to_vec ( & doc ! {
836+ let bytes = bson:: serialize_to_vec ( & doc ! {
833837 "r" : Regex {
834838 pattern: "a[b-c]d" . to_string( ) ,
835839 options: "ab" . to_string( ) ,
@@ -848,7 +852,7 @@ fn raw_code_w_scope() {
848852 r : RawJavaScriptCodeWithScopeRef < ' a > ,
849853 }
850854
851- let bytes = bson:: to_vec ( & doc ! {
855+ let bytes = bson:: serialize_to_vec ( & doc ! {
852856 "r" : JavaScriptCodeWithScope {
853857 code: "console.log(x)" . to_string( ) ,
854858 scope: doc! { "x" : 1 } ,
@@ -940,7 +944,7 @@ impl AllTypes {
940944
941945 let decimal = {
942946 let bytes = hex:: decode ( "18000000136400D0070000000000000000000000003A3000" ) . unwrap ( ) ;
943- let d = Document :: from_reader ( bytes. as_slice ( ) ) . unwrap ( ) ;
947+ let d = Document :: decode_from_reader ( bytes. as_slice ( ) ) . unwrap ( ) ;
944948 match d. get ( "d" ) {
945949 Some ( Bson :: Decimal128 ( d) ) => * d,
946950 c => panic ! ( "expected decimal128, got {:?}" , c) ,
@@ -1044,7 +1048,7 @@ fn all_raw_types_rmp() {
10441048 regex : RawRegexRef < ' a > ,
10451049 }
10461050
1047- let doc_bytes = bson:: to_vec ( & doc ! {
1051+ let doc_bytes = bson:: serialize_to_vec ( & doc ! {
10481052 "bson" : "some string" ,
10491053 "array" : [ 1 , 2 , 3 ] ,
10501054 "binary" : Binary { bytes: vec![ 1 , 2 , 3 ] , subtype: BinarySubtype :: Generic } ,
@@ -1059,7 +1063,7 @@ fn all_raw_types_rmp() {
10591063 }
10601064 } )
10611065 . unwrap ( ) ;
1062- let doc_buf = RawDocumentBuf :: from_bytes ( doc_bytes) . unwrap ( ) ;
1066+ let doc_buf = RawDocumentBuf :: decode_from_bytes ( doc_bytes) . unwrap ( ) ;
10631067 let document = & doc_buf;
10641068 let array = document. get_array ( "array" ) . unwrap ( ) ;
10651069
@@ -1115,7 +1119,7 @@ fn borrowed() {
11151119 "cow" : "cow" ,
11161120 "array" : [ "borrowed string" ] ,
11171121 } ;
1118- let bson = doc. to_vec ( ) ;
1122+ let bson = doc. encode_to_vec ( ) . unwrap ( ) ;
11191123
11201124 let s = "borrowed string" . to_string ( ) ;
11211125 let ss = "another borrowed string" . to_string ( ) ;
@@ -1132,7 +1136,7 @@ fn borrowed() {
11321136 } ;
11331137
11341138 let deserialized: Foo =
1135- bson:: from_slice ( bson. as_slice ( ) ) . expect ( "deserialization should succeed" ) ;
1139+ bson:: deserialize_from_slice ( bson. as_slice ( ) ) . expect ( "deserialization should succeed" ) ;
11361140 assert_eq ! ( deserialized, v) ;
11371141}
11381142
@@ -1175,8 +1179,8 @@ fn u2i() {
11751179 let v = TooBig {
11761180 u_64 : i64:: MAX as u64 + 1 ,
11771181 } ;
1178- bson:: to_document ( & v) . unwrap_err ( ) ;
1179- bson:: to_vec ( & v) . unwrap_err ( ) ;
1182+ bson:: serialize_to_document ( & v) . unwrap_err ( ) ;
1183+ bson:: serialize_to_vec ( & v) . unwrap_err ( ) ;
11801184}
11811185
11821186#[ test]
@@ -1258,14 +1262,16 @@ fn owned_raw_types() {
12581262 RawBson :: JavaScriptCodeWithScope ( raw_code_w_scope. clone ( ) ) ,
12591263 ) ,
12601264 ( "decimal128" , RawBson :: Decimal128 ( d128) ) ,
1261- ] ) ,
1265+ ] )
1266+ . unwrap ( ) ,
12621267 array : RawArrayBuf :: from_iter ( [
12631268 RawBson :: String ( "a string" . to_string ( ) ) ,
12641269 RawBson :: ObjectId ( oid) ,
12651270 RawBson :: DateTime ( dt) ,
12661271 RawBson :: JavaScriptCodeWithScope ( raw_code_w_scope) ,
12671272 RawBson :: Decimal128 ( d128) ,
1268- ] ) ,
1273+ ] )
1274+ . unwrap ( ) ,
12691275 } ;
12701276
12711277 let expected = doc ! {
@@ -1307,21 +1313,21 @@ fn hint_cleared() {
13071313 "binary" : binary_value. clone( )
13081314 } ;
13091315
1310- let bytes = bson:: to_vec ( & doc_value) . unwrap ( ) ;
1316+ let bytes = bson:: serialize_to_vec ( & doc_value) . unwrap ( ) ;
13111317
1312- let doc = RawDocument :: from_bytes ( & bytes) . unwrap ( ) ;
1318+ let doc = RawDocument :: decode_from_bytes ( & bytes) . unwrap ( ) ;
13131319 let binary = doc. get_binary ( "binary" ) . unwrap ( ) ;
13141320
13151321 let f = Foo { doc, binary } ;
13161322
1317- let serialized_bytes = bson:: to_vec ( & f) . unwrap ( ) ;
1318- let round_doc: Document = bson:: from_slice ( & serialized_bytes) . unwrap ( ) ;
1323+ let serialized_bytes = bson:: serialize_to_vec ( & f) . unwrap ( ) ;
1324+ let round_doc: Document = bson:: deserialize_from_slice ( & serialized_bytes) . unwrap ( ) ;
13191325
13201326 assert_eq ! ( round_doc, doc! { "doc" : doc_value, "binary" : binary_value } ) ;
13211327}
13221328
13231329#[ test]
13241330fn invalid_length ( ) {
13251331 // This is a regression test for fuzzer-generated input (RUST-1240).
1326- assert ! ( bson:: from_slice :: <Document >( & [ 4 , 0 , 0 , 128 , 0 , 87 ] ) . is_err( ) ) ;
1332+ assert ! ( bson:: deserialize_from_slice :: <Document >( & [ 4 , 0 , 0 , 128 , 0 , 87 ] ) . is_err( ) ) ;
13271333}
0 commit comments