@@ -39,8 +39,8 @@ pub enum MemoryStoreError {
39
39
impl SessionStore for MemoryStore {
40
40
type Error = MemoryStoreError ;
41
41
42
- async fn load_session ( & self , cookie_value : String ) -> Result < Option < Session > , Self :: Error > {
43
- let id = Session :: id_from_cookie_value ( & cookie_value) ?;
42
+ async fn load_session ( & self , cookie_value : & str ) -> Result < Option < Session > , Self :: Error > {
43
+ let id = Session :: id_from_cookie_value ( cookie_value) ?;
44
44
log:: trace!( "loading session by id `{}`" , id) ;
45
45
let Occupied ( entry) = self . 0 . entry ( id) else {
46
46
return Ok ( None ) ;
@@ -54,14 +54,15 @@ impl SessionStore for MemoryStore {
54
54
}
55
55
}
56
56
57
- async fn store_session ( & self , mut session : Session ) -> Result < Option < String > , Self :: Error > {
57
+ async fn store_session ( & self , session : & mut Session ) -> Result < Option < String > , Self :: Error > {
58
58
log:: trace!( "storing session by id `{}`" , session. id( ) ) ;
59
59
session. reset_data_changed ( ) ;
60
+ let cookie_value = session. take_cookie_value ( ) ;
60
61
self . 0 . insert ( session. id ( ) . to_string ( ) , session. clone ( ) ) ;
61
- Ok ( session . into_cookie_value ( ) )
62
+ Ok ( cookie_value )
62
63
}
63
64
64
- async fn destroy_session ( & self , session : Session ) -> Result < ( ) , Self :: Error > {
65
+ async fn destroy_session ( & self , session : & mut Session ) -> Result < ( ) , Self :: Error > {
65
66
log:: trace!( "destroying session by id `{}`" , session. id( ) ) ;
66
67
self . 0 . remove ( session. id ( ) ) ;
67
68
Ok ( ( ) )
@@ -95,7 +96,7 @@ impl MemoryStore {
95
96
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { async_std::task::block_on(async {
96
97
/// let mut store = MemoryStore::new();
97
98
/// assert_eq!(store.count(), 0);
98
- /// store.store_session(Session::new()).await?;
99
+ /// store.store_session(&mut Session::new()).await?;
99
100
/// assert_eq!(store.count(), 1);
100
101
/// # Ok(()) }) }
101
102
/// ```
@@ -115,8 +116,8 @@ mod tests {
115
116
let mut session = Session :: new ( ) ;
116
117
session. insert ( "key" , "Hello" ) ?;
117
118
let cloned = session. clone ( ) ;
118
- let cookie_value = store. store_session ( session) . await ?. unwrap ( ) ;
119
- let loaded_session = store. load_session ( cookie_value) . await ?. unwrap ( ) ;
119
+ let cookie_value = store. store_session ( & mut session) . await ?. unwrap ( ) ;
120
+ let loaded_session = store. load_session ( & cookie_value) . await ?. unwrap ( ) ;
120
121
assert_eq ! ( cloned. id( ) , loaded_session. id( ) ) ;
121
122
assert_eq ! ( "Hello" , & loaded_session. get:: <String >( "key" ) . unwrap( ) ) ;
122
123
assert ! ( !loaded_session. is_expired( ) ) ;
@@ -130,14 +131,14 @@ mod tests {
130
131
let mut session = Session :: new ( ) ;
131
132
132
133
session. insert ( "key" , "value" ) ?;
133
- let cookie_value = store. store_session ( session) . await ?. unwrap ( ) ;
134
+ let cookie_value = store. store_session ( & mut session) . await ?. unwrap ( ) ;
134
135
135
- let mut session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
136
+ let mut session = store. load_session ( & cookie_value) . await ?. unwrap ( ) ;
136
137
session. insert ( "key" , "other value" ) ?;
137
138
138
- assert_eq ! ( store. store_session( session) . await ?, None ) ;
139
- let session = store. load_session ( cookie_value) . await ?. unwrap ( ) ;
140
- assert_eq ! ( & session. get:: <String >( "key" ) . unwrap( ) , "other value" ) ;
139
+ assert_eq ! ( store. store_session( & mut session) . await ?, None ) ;
140
+ let session = store. load_session ( & cookie_value) . await ?. unwrap ( ) ;
141
+ assert_eq ! ( & mut session. get:: <String >( "key" ) . unwrap( ) , "other value" ) ;
141
142
142
143
Ok ( ( ) )
143
144
}
@@ -148,20 +149,20 @@ mod tests {
148
149
let mut session = Session :: new ( ) ;
149
150
session. expire_in ( Duration :: from_secs ( 1 ) ) ;
150
151
let original_expires = * session. expiry ( ) . unwrap ( ) ;
151
- let cookie_value = store. store_session ( session) . await ?. unwrap ( ) ;
152
+ let cookie_value = store. store_session ( & mut session) . await ?. unwrap ( ) ;
152
153
153
- let mut session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
154
+ let mut session = store. load_session ( & cookie_value) . await ?. unwrap ( ) ;
154
155
155
156
assert_eq ! ( session. expiry( ) . unwrap( ) , & original_expires) ;
156
157
session. expire_in ( Duration :: from_secs ( 3 ) ) ;
157
158
let new_expires = * session. expiry ( ) . unwrap ( ) ;
158
- assert_eq ! ( None , store. store_session( session) . await ?) ;
159
+ assert_eq ! ( None , store. store_session( & mut session) . await ?) ;
159
160
160
- let session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
161
+ let session = store. load_session ( & cookie_value) . await ?. unwrap ( ) ;
161
162
assert_eq ! ( session. expiry( ) . unwrap( ) , & new_expires) ;
162
163
163
164
task:: sleep ( Duration :: from_secs ( 3 ) ) . await ;
164
- assert_eq ! ( None , store. load_session( cookie_value) . await ?) ;
165
+ assert_eq ! ( None , store. load_session( & cookie_value) . await ?) ;
165
166
166
167
Ok ( ( ) )
167
168
}
@@ -174,16 +175,16 @@ mod tests {
174
175
session. insert ( "key" , "value" ) ?;
175
176
let cloned = session. clone ( ) ;
176
177
177
- let cookie_value = store. store_session ( session) . await ?. unwrap ( ) ;
178
+ let cookie_value = store. store_session ( & mut session) . await ?. unwrap ( ) ;
178
179
179
- let loaded_session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
180
+ let loaded_session = store. load_session ( & cookie_value) . await ?. unwrap ( ) ;
180
181
assert_eq ! ( cloned. id( ) , loaded_session. id( ) ) ;
181
182
assert_eq ! ( "value" , & * loaded_session. get:: <String >( "key" ) . unwrap( ) ) ;
182
183
183
184
assert ! ( !loaded_session. is_expired( ) ) ;
184
185
185
186
task:: sleep ( Duration :: from_secs ( 3 ) ) . await ;
186
- assert_eq ! ( None , store. load_session( cookie_value) . await ?) ;
187
+ assert_eq ! ( None , store. load_session( & cookie_value) . await ?) ;
187
188
188
189
Ok ( ( ) )
189
190
}
@@ -192,26 +193,26 @@ mod tests {
192
193
async fn destroying_a_single_session ( ) -> Result < ( ) , MemoryStoreError > {
193
194
let store = MemoryStore :: new ( ) ;
194
195
for _ in 0 ..3i8 {
195
- store. store_session ( Session :: new ( ) ) . await ?;
196
+ store. store_session ( & mut Session :: new ( ) ) . await ?;
196
197
}
197
198
198
- let cookie = store. store_session ( Session :: new ( ) ) . await ?. unwrap ( ) ;
199
+ let cookie = store. store_session ( & mut Session :: new ( ) ) . await ?. unwrap ( ) ;
199
200
assert_eq ! ( 4 , store. count( ) ) ;
200
- let session = store. load_session ( cookie. clone ( ) ) . await ?. unwrap ( ) ;
201
- store. destroy_session ( session. clone ( ) ) . await ?;
202
- assert_eq ! ( None , store. load_session( cookie) . await ?) ;
201
+ let mut session = store. load_session ( & cookie) . await ?. unwrap ( ) ;
202
+ store. destroy_session ( & mut session) . await ?;
203
+ assert_eq ! ( None , store. load_session( & cookie) . await ?) ;
203
204
assert_eq ! ( 3 , store. count( ) ) ;
204
205
205
206
// attempting to destroy the session again is not an error
206
- assert ! ( store. destroy_session( session) . await . is_ok( ) ) ;
207
+ assert ! ( store. destroy_session( & mut session) . await . is_ok( ) ) ;
207
208
Ok ( ( ) )
208
209
}
209
210
210
211
#[ async_std:: test]
211
212
async fn clearing_the_whole_store ( ) -> Result < ( ) , MemoryStoreError > {
212
213
let store = MemoryStore :: new ( ) ;
213
214
for _ in 0 ..3i8 {
214
- store. store_session ( Session :: new ( ) ) . await ?;
215
+ store. store_session ( & mut Session :: new ( ) ) . await ?;
215
216
}
216
217
217
218
assert_eq ! ( 3 , store. count( ) ) ;
0 commit comments