3
3
use std:: io;
4
4
5
5
use actix:: prelude:: * ;
6
- use diesel:: { prelude:: * , result :: Error } ;
6
+ use diesel:: prelude:: * ;
7
7
use rand:: { rngs:: SmallRng , Rng , SeedableRng } ;
8
8
9
9
use crate :: models;
@@ -18,7 +18,7 @@ impl DbExecutor {
18
18
DbExecutor {
19
19
conn : PgConnection :: establish ( db_url)
20
20
. unwrap_or_else ( |_| panic ! ( "Error connecting to {}" , db_url) ) ,
21
- rng : SmallRng :: from_entropy ( ) ,
21
+ rng : SmallRng :: from_os_rng ( ) ,
22
22
}
23
23
}
24
24
}
@@ -39,10 +39,10 @@ impl Handler<RandomWorld> for DbExecutor {
39
39
fn handle ( & mut self , _: RandomWorld , _: & mut Self :: Context ) -> Self :: Result {
40
40
use crate :: schema:: world:: dsl:: * ;
41
41
42
- let random_id = self . rng . gen_range ( 1 ..10_001 ) ;
42
+ let random_id = self . rng . random_range ( 1 ..10_001 ) ;
43
43
match world
44
44
. filter ( id. eq ( random_id) )
45
- . load :: < models:: World > ( & self . conn )
45
+ . load :: < models:: World > ( & mut self . conn )
46
46
{
47
47
Ok ( mut items) => Ok ( items. pop ( ) . unwrap ( ) ) ,
48
48
Err ( _) => Err ( io:: Error :: new ( io:: ErrorKind :: Other , "Database error" ) ) ,
@@ -64,8 +64,8 @@ impl Handler<RandomWorlds> for DbExecutor {
64
64
65
65
let mut worlds = Vec :: with_capacity ( msg. 0 as usize ) ;
66
66
for _ in 0 ..msg. 0 {
67
- let w_id = self . rng . gen_range ( 1 ..10_001 ) ;
68
- let w = match world. filter ( id. eq ( w_id) ) . load :: < models:: World > ( & self . conn ) {
67
+ let w_id = self . rng . random_range ( 1 ..10_001 ) ;
68
+ let w = match world. filter ( id. eq ( w_id) ) . load :: < models:: World > ( & mut self . conn ) {
69
69
Ok ( mut items) => items. pop ( ) . unwrap ( ) ,
70
70
Err ( _) => {
71
71
return Err ( io:: Error :: new ( io:: ErrorKind :: Other , "Database error" ) ) ;
@@ -91,27 +91,28 @@ impl Handler<UpdateWorld> for DbExecutor {
91
91
92
92
let mut worlds = Vec :: with_capacity ( msg. 0 as usize ) ;
93
93
for _ in 0 ..msg. 0 {
94
- let w_id: i32 = self . rng . gen_range ( 1 ..10_001 ) ;
95
- let mut w = match world. filter ( id. eq ( w_id) ) . load :: < models:: World > ( & self . conn ) {
94
+ let w_id: i32 = self . rng . random_range ( 1 ..10_001 ) ;
95
+ let mut w = match world. filter ( id. eq ( w_id) ) . load :: < models:: World > ( & mut self . conn ) {
96
96
Ok ( mut items) => items. pop ( ) . unwrap ( ) ,
97
97
Err ( _) => {
98
98
return Err ( io:: Error :: new ( io:: ErrorKind :: Other , "Database error" ) ) ;
99
99
}
100
100
} ;
101
- w. randomnumber = self . rng . gen_range ( 1 ..10_001 ) ;
101
+ w. randomnumber = self . rng . random_range ( 1 ..10_001 ) ;
102
102
worlds. push ( w) ;
103
103
}
104
104
worlds. sort_by_key ( |w| w. id ) ;
105
105
106
- let _ = self . conn . transaction :: < ( ) , Error , _ > ( | | {
106
+ self . conn . transaction ( |conn | {
107
107
for w in & worlds {
108
- let _ = diesel:: update ( world)
108
+ diesel:: update ( world)
109
109
. filter ( id. eq ( w. id ) )
110
110
. set ( randomnumber. eq ( w. randomnumber ) )
111
- . execute ( & self . conn ) ;
111
+ . execute ( conn) ? ;
112
112
}
113
113
Ok ( ( ) )
114
- } ) ;
114
+ } )
115
+ . map_err ( |e : diesel:: result:: Error | io:: Error :: new ( io:: ErrorKind :: Other , e) ) ?;
115
116
116
117
Ok ( worlds)
117
118
}
@@ -129,7 +130,7 @@ impl Handler<TellFortune> for DbExecutor {
129
130
fn handle ( & mut self , _: TellFortune , _: & mut Self :: Context ) -> Self :: Result {
130
131
use crate :: schema:: fortune:: dsl:: * ;
131
132
132
- match fortune. load :: < models:: Fortune > ( & self . conn ) {
133
+ match fortune. load :: < models:: Fortune > ( & mut self . conn ) {
133
134
Ok ( mut items) => {
134
135
items. push ( models:: Fortune {
135
136
id : 0 ,
0 commit comments