@@ -35,19 +35,19 @@ impl Client {
3535 }
3636
3737 pub async fn get_worlds ( & self , num : u16 ) -> HandleResult < Vec < World > > {
38- let len = num as usize ;
39-
4038 let mut conn = self . pool . get ( ) . await ?;
4139 let stmt = WORLD_STMT . execute ( & mut conn) . await ?;
4240
4341 let mut res = {
4442 let ( ref mut rng, ref mut buf) = * self . shared . borrow_mut ( ) ;
45- let mut pipe = Pipeline :: with_capacity_from_buf ( len, buf) ;
46- ( 0 ..num) . try_for_each ( |_| stmt. bind ( [ rng. gen_id ( ) ] ) . query ( & mut pipe) ) ?;
43+ let mut pipe = Pipeline :: with_capacity_from_buf ( num as _ , buf) ;
44+ rng. gen_multi ( )
45+ . take ( num as _ )
46+ . try_for_each ( |id| stmt. bind ( [ id] ) . query ( & mut pipe) ) ?;
4747 pipe. query ( & conn. consume ( ) ) ?
4848 } ;
4949
50- let mut worlds = Vec :: with_capacity ( len ) ;
50+ let mut worlds = Vec :: with_capacity ( num as _ ) ;
5151
5252 while let Some ( mut item) = res. try_next ( ) . await ? {
5353 let row = item. try_next ( ) . await ?. ok_or_else ( not_found) ?;
@@ -58,28 +58,26 @@ impl Client {
5858 }
5959
6060 pub async fn update ( & self , num : u16 ) -> HandleResult < Vec < World > > {
61- let len = num as usize ;
62-
6361 let mut conn = self . pool . get ( ) . await ?;
6462 let world_stmt = WORLD_STMT . execute ( & mut conn) . await ?;
6563 let update_stmt = UPDATE_BATCH_STMT . execute ( & mut conn) . await ?;
6664
6765 let ( mut res, worlds) = {
6866 let ( ref mut rng, ref mut buf) = * self . shared . borrow_mut ( ) ;
69- let mut pipe = Pipeline :: with_capacity_from_buf ( len + 1 , buf) ;
70-
71- let ( mut params , worlds ) = core :: iter :: repeat_with ( || {
72- let id = rng . gen_id ( ) ;
73- let rand = rng . gen_id ( ) ;
74- world_stmt . bind ( [ id ] ) . query ( & mut pipe ) ? ;
75- HandleResult :: Ok ( ( ( id , rand ) , World :: new ( id , rand ) ) )
76- } )
77- . take ( len )
78- . collect :: < Result < ( Vec < _ > , Vec < _ > ) , _ > > ( ) ? ;
79-
80- params . sort ( ) ;
81- let ( ids , rngs ) = params . into_iter ( ) . collect :: < ( Vec < _ > , Vec < _ > ) > ( ) ;
82-
67+ let mut pipe = Pipeline :: with_capacity_from_buf ( ( num + 1 ) as _ , buf) ;
68+
69+ let mut ids = rng . gen_multi ( ) . take ( num as _ ) . collect :: < Vec < _ > > ( ) ;
70+ ids . sort ( ) ;
71+
72+ let ( rngs , worlds ) = ids
73+ . iter ( )
74+ . cloned ( )
75+ . zip ( rng . gen_multi ( ) )
76+ . map ( | ( id , rand ) | {
77+ world_stmt . bind ( [ id ] ) . query ( & mut pipe ) ? ;
78+ HandleResult :: Ok ( ( rand , World :: new ( id , rand ) ) )
79+ } )
80+ . collect :: < HandleResult < ( Vec < _ > , Vec < _ > ) > > ( ) ? ;
8381 update_stmt. bind ( [ & ids, & rngs] ) . query ( & mut pipe) ?;
8482 ( pipe. query ( & conn. consume ( ) ) ?, worlds)
8583 } ;
0 commit comments