@@ -25,7 +25,7 @@ pub(crate) struct Insert<'a, T> {
2525 ns : Namespace ,
2626 documents : Vec < & ' a T > ,
2727 inserted_ids : Vec < Bson > ,
28- options : InsertManyOptions ,
28+ options : Option < InsertManyOptions > ,
2929}
3030
3131impl < ' a , T > Insert < ' a , T > {
@@ -34,9 +34,6 @@ impl<'a, T> Insert<'a, T> {
3434 documents : Vec < & ' a T > ,
3535 options : Option < InsertManyOptions > ,
3636 ) -> Self {
37- let mut options =
38- options. unwrap_or_else ( || InsertManyOptions :: builder ( ) . ordered ( true ) . build ( ) ) ;
39- options. ordered = Some ( options. ordered . unwrap_or ( true ) ) ;
4037 Self {
4138 ns,
4239 options,
@@ -46,7 +43,10 @@ impl<'a, T> Insert<'a, T> {
4643 }
4744
4845 fn is_ordered ( & self ) -> bool {
49- self . options . ordered . unwrap_or ( true )
46+ self . options
47+ . as_ref ( )
48+ . and_then ( |o| o. ordered )
49+ . unwrap_or ( true )
5050 }
5151}
5252
@@ -58,13 +58,6 @@ impl<'a, T: Serialize> Operation for Insert<'a, T> {
5858 const NAME : & ' static str = "insert" ;
5959
6060 fn build ( & mut self , description : & StreamDescription ) -> Result < Command < InsertCommand > > {
61- if self . documents . is_empty ( ) {
62- return Err ( ErrorKind :: InvalidArgument {
63- message : "must specify at least one document to insert" . to_string ( ) ,
64- }
65- . into ( ) ) ;
66- }
67-
6861 let mut docs: Vec < Vec < u8 > > = Vec :: new ( ) ;
6962 let mut size = 0 ;
7063
@@ -79,14 +72,19 @@ impl<'a, T: Serialize> Operation for Insert<'a, T> {
7972 Some ( b) => b,
8073 None => {
8174 let oid = ObjectId :: new ( ) ;
82- let new_len = doc. len ( ) as i32 + 1 + 4 + 12 ;
83- doc. splice ( 0 ..4 , new_len. to_le_bytes ( ) . iter ( ) . cloned ( ) ) ;
8475
85- let mut new_doc = Vec :: new ( ) ;
86- new_doc. write_u8 ( ElementType :: ObjectId as u8 ) ?;
87- new_doc. write_all ( b"_id\0 " ) ?;
88- new_doc. extend ( oid. bytes ( ) . iter ( ) ) ;
89- doc. splice ( 4 ..4 , new_doc. into_iter ( ) ) ;
76+ // write element to temporary buffer
77+ let mut new_id = Vec :: new ( ) ;
78+ new_id. write_u8 ( ElementType :: ObjectId as u8 ) ?;
79+ new_id. write_all ( b"_id\0 " ) ?;
80+ new_id. extend ( oid. bytes ( ) . iter ( ) ) ;
81+
82+ // insert element to beginning of existing doc after length
83+ doc. splice ( 4 ..4 , new_id. into_iter ( ) ) ;
84+
85+ // update length of doc
86+ let new_len = doc. len ( ) as i32 ;
87+ doc. splice ( 0 ..4 , new_len. to_le_bytes ( ) . iter ( ) . cloned ( ) ) ;
9088
9189 Bson :: ObjectId ( oid)
9290 }
@@ -112,13 +110,16 @@ impl<'a, T: Serialize> Operation for Insert<'a, T> {
112110 . into ( ) ) ;
113111 }
114112
113+ let mut options = self . options . clone ( ) . unwrap_or_default ( ) ;
114+ options. ordered = Some ( self . is_ordered ( ) ) ;
115+
115116 let body = InsertCommand {
116117 insert : self . ns . coll . clone ( ) ,
117118 documents : DocumentArraySpec {
118119 documents : docs,
119120 length : size as i32 ,
120121 } ,
121- options : self . options . clone ( ) ,
122+ options,
122123 } ;
123124
124125 Ok ( Command :: new ( "insert" . to_string ( ) , self . ns . db . clone ( ) , body) )
@@ -161,7 +162,7 @@ impl<'a, T: Serialize> Operation for Insert<'a, T> {
161162
162163 // update length of original doc
163164 let final_length = serialized. len ( ) as i32 ;
164- ( & mut serialized[ 0 ..4 ] ) . write_all ( & final_length . to_le_bytes ( ) ) ? ;
165+ serialized. splice ( 0 ..4 , final_length . to_le_bytes ( ) . iter ( ) . cloned ( ) ) ;
165166
166167 Ok ( serialized)
167168 }
@@ -211,7 +212,7 @@ impl<'a, T: Serialize> Operation for Insert<'a, T> {
211212 }
212213
213214 fn write_concern ( & self ) -> Option < & WriteConcern > {
214- self . options . write_concern . as_ref ( )
215+ self . options . as_ref ( ) . and_then ( |o| o . write_concern . as_ref ( ) )
215216 }
216217
217218 fn retryability ( & self ) -> Retryability {
0 commit comments