11use std:: { ops:: Deref , time:: Duration } ;
22
3- use async_trait:: async_trait;
4- use futures:: stream:: StreamExt ;
3+ use futures:: { future:: BoxFuture , stream:: StreamExt , FutureExt } ;
54use serde:: {
65 de:: { self , Deserializer } ,
76 Deserialize ,
@@ -16,12 +15,11 @@ use crate::{
1615 Collection ,
1716} ;
1817
19- #[ async_trait]
2018pub ( super ) trait TestOperation {
2119 /// The command names to monitor as part of this test.
2220 fn command_names ( & self ) -> & [ & str ] ;
2321
24- async fn execute ( & self , collection : Collection < Document > ) -> Result < ( ) > ;
22+ fn execute ( & self , collection : Collection < Document > ) -> BoxFuture < Result < ( ) > > ;
2523}
2624
2725pub ( super ) struct AnyTestOperation {
@@ -75,17 +73,19 @@ pub(super) struct DeleteMany {
7573 filter : Document ,
7674}
7775
78- #[ async_trait]
7976impl TestOperation for DeleteMany {
8077 fn command_names ( & self ) -> & [ & str ] {
8178 & [ "delete" ]
8279 }
8380
84- async fn execute ( & self , collection : Collection < Document > ) -> Result < ( ) > {
85- collection
86- . delete_many ( self . filter . clone ( ) , None )
87- . await
88- . map ( |_| ( ) )
81+ fn execute ( & self , collection : Collection < Document > ) -> BoxFuture < Result < ( ) > > {
82+ async move {
83+ collection
84+ . delete_many ( self . filter . clone ( ) , None )
85+ . await
86+ . map ( |_| ( ) )
87+ }
88+ . boxed ( )
8989 }
9090}
9191
@@ -94,17 +94,19 @@ pub(super) struct DeleteOne {
9494 filter : Document ,
9595}
9696
97- #[ async_trait]
9897impl TestOperation for DeleteOne {
9998 fn command_names ( & self ) -> & [ & str ] {
10099 & [ "delete" ]
101100 }
102101
103- async fn execute ( & self , collection : Collection < Document > ) -> Result < ( ) > {
104- collection
105- . delete_one ( self . filter . clone ( ) , None )
106- . await
107- . map ( |_| ( ) )
102+ fn execute ( & self , collection : Collection < Document > ) -> BoxFuture < Result < ( ) > > {
103+ async move {
104+ collection
105+ . delete_one ( self . filter . clone ( ) , None )
106+ . await
107+ . map ( |_| ( ) )
108+ }
109+ . boxed ( )
108110 }
109111}
110112
@@ -159,31 +161,33 @@ pub(super) struct Find {
159161 modifiers : Option < FindModifiers > ,
160162}
161163
162- #[ async_trait]
163164impl TestOperation for Find {
164165 fn command_names ( & self ) -> & [ & str ] {
165166 & [ "find" , "getMore" ]
166167 }
167168
168- async fn execute ( & self , collection : Collection < Document > ) -> Result < ( ) > {
169- let mut options = FindOptions {
170- sort : self . sort . clone ( ) ,
171- skip : self . skip ,
172- batch_size : self . batch_size . map ( |i| i as u32 ) ,
173- limit : self . limit ,
174- ..Default :: default ( )
175- } ;
176-
177- if let Some ( ref modifiers) = self . modifiers {
178- modifiers. update_options ( & mut options) ;
179- }
180-
181- let mut cursor = collection. find ( self . filter . clone ( ) , options) . await ?;
182-
183- while let Some ( result) = cursor. next ( ) . await {
184- result?;
169+ fn execute ( & self , collection : Collection < Document > ) -> BoxFuture < Result < ( ) > > {
170+ async move {
171+ let mut options = FindOptions {
172+ sort : self . sort . clone ( ) ,
173+ skip : self . skip ,
174+ batch_size : self . batch_size . map ( |i| i as u32 ) ,
175+ limit : self . limit ,
176+ ..Default :: default ( )
177+ } ;
178+
179+ if let Some ( ref modifiers) = self . modifiers {
180+ modifiers. update_options ( & mut options) ;
181+ }
182+
183+ let mut cursor = collection. find ( self . filter . clone ( ) , options) . await ?;
184+
185+ while let Some ( result) = cursor. next ( ) . await {
186+ result?;
187+ }
188+ Ok ( ( ) )
185189 }
186- Ok ( ( ) )
190+ . boxed ( )
187191 }
188192}
189193
@@ -194,17 +198,19 @@ pub(super) struct InsertMany {
194198 options : Option < InsertManyOptions > ,
195199}
196200
197- #[ async_trait]
198201impl TestOperation for InsertMany {
199202 fn command_names ( & self ) -> & [ & str ] {
200203 & [ "insert" ]
201204 }
202205
203- async fn execute ( & self , collection : Collection < Document > ) -> Result < ( ) > {
204- collection
205- . insert_many ( self . documents . clone ( ) , self . options . clone ( ) )
206- . await
207- . map ( |_| ( ) )
206+ fn execute ( & self , collection : Collection < Document > ) -> BoxFuture < Result < ( ) > > {
207+ async move {
208+ collection
209+ . insert_many ( self . documents . clone ( ) , self . options . clone ( ) )
210+ . await
211+ . map ( |_| ( ) )
212+ }
213+ . boxed ( )
208214 }
209215}
210216
@@ -213,17 +219,19 @@ pub(super) struct InsertOne {
213219 document : Document ,
214220}
215221
216- #[ async_trait]
217222impl TestOperation for InsertOne {
218223 fn command_names ( & self ) -> & [ & str ] {
219224 & [ "insert" ]
220225 }
221226
222- async fn execute ( & self , collection : Collection < Document > ) -> Result < ( ) > {
223- collection
224- . insert_one ( self . document . clone ( ) , None )
225- . await
226- . map ( |_| ( ) )
227+ fn execute ( & self , collection : Collection < Document > ) -> BoxFuture < Result < ( ) > > {
228+ async move {
229+ collection
230+ . insert_one ( self . document . clone ( ) , None )
231+ . await
232+ . map ( |_| ( ) )
233+ }
234+ . boxed ( )
227235 }
228236}
229237
@@ -233,17 +241,19 @@ pub(super) struct UpdateMany {
233241 update : Document ,
234242}
235243
236- #[ async_trait]
237244impl TestOperation for UpdateMany {
238245 fn command_names ( & self ) -> & [ & str ] {
239246 & [ "update" ]
240247 }
241248
242- async fn execute ( & self , collection : Collection < Document > ) -> Result < ( ) > {
243- collection
244- . update_many ( self . filter . clone ( ) , self . update . clone ( ) , None )
245- . await
246- . map ( |_| ( ) )
249+ fn execute ( & self , collection : Collection < Document > ) -> BoxFuture < Result < ( ) > > {
250+ async move {
251+ collection
252+ . update_many ( self . filter . clone ( ) , self . update . clone ( ) , None )
253+ . await
254+ . map ( |_| ( ) )
255+ }
256+ . boxed ( )
247257 }
248258}
249259
@@ -255,21 +265,23 @@ pub(super) struct UpdateOne {
255265 upsert : Option < bool > ,
256266}
257267
258- #[ async_trait]
259268impl TestOperation for UpdateOne {
260269 fn command_names ( & self ) -> & [ & str ] {
261270 & [ "update" ]
262271 }
263272
264- async fn execute ( & self , collection : Collection < Document > ) -> Result < ( ) > {
265- let options = self . upsert . map ( |b| UpdateOptions {
266- upsert : Some ( b) ,
267- ..Default :: default ( )
268- } ) ;
269- collection
270- . update_one ( self . filter . clone ( ) , self . update . clone ( ) , options)
271- . await
272- . map ( |_| ( ) )
273+ fn execute ( & self , collection : Collection < Document > ) -> BoxFuture < Result < ( ) > > {
274+ async move {
275+ let options = self . upsert . map ( |b| UpdateOptions {
276+ upsert : Some ( b) ,
277+ ..Default :: default ( )
278+ } ) ;
279+ collection
280+ . update_one ( self . filter . clone ( ) , self . update . clone ( ) , options)
281+ . await
282+ . map ( |_| ( ) )
283+ }
284+ . boxed ( )
273285 }
274286}
275287
0 commit comments