@@ -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,14 @@ 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
60
self . 0 . insert ( session. id ( ) . to_string ( ) , session. clone ( ) ) ;
61
- Ok ( session. into_cookie_value ( ) )
61
+ Ok ( session. take_cookie_value ( ) )
62
62
}
63
63
64
- async fn destroy_session ( & self , session : Session ) -> Result < ( ) , Self :: Error > {
64
+ async fn destroy_session ( & self , session : & mut Session ) -> Result < ( ) , Self :: Error > {
65
65
log:: trace!( "destroying session by id `{}`" , session. id( ) ) ;
66
66
self . 0 . remove ( session. id ( ) ) ;
67
67
Ok ( ( ) )
@@ -95,7 +95,7 @@ impl MemoryStore {
95
95
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { async_std::task::block_on(async {
96
96
/// let mut store = MemoryStore::new();
97
97
/// assert_eq!(store.count(), 0);
98
- /// store.store_session(Session::new()).await?;
98
+ /// store.store_session(&mut Session::new()).await?;
99
99
/// assert_eq!(store.count(), 1);
100
100
/// # Ok(()) }) }
101
101
/// ```
@@ -115,7 +115,7 @@ mod tests {
115
115
let mut session = Session :: new ( ) ;
116
116
session. insert ( "key" , "Hello" ) ?;
117
117
let cloned = session. clone ( ) ;
118
- let cookie_value = store. store_session ( session) . await ?. unwrap ( ) ;
118
+ let cookie_value = store. store_session ( & mut session) . await ?. unwrap ( ) ;
119
119
let loaded_session = store. load_session ( cookie_value) . await ?. unwrap ( ) ;
120
120
assert_eq ! ( cloned. id( ) , loaded_session. id( ) ) ;
121
121
assert_eq ! ( "Hello" , & loaded_session. get:: <String >( "key" ) . unwrap( ) ) ;
@@ -130,14 +130,14 @@ mod tests {
130
130
let mut session = Session :: new ( ) ;
131
131
132
132
session. insert ( "key" , "value" ) ?;
133
- let cookie_value = store. store_session ( session) . await ?. unwrap ( ) ;
133
+ let cookie_value = store. store_session ( & mut session) . await ?. unwrap ( ) ;
134
134
135
135
let mut session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
136
136
session. insert ( "key" , "other value" ) ?;
137
137
138
- assert_eq ! ( store. store_session( session) . await ?, None ) ;
138
+ assert_eq ! ( store. store_session( & mut session) . await ?, None ) ;
139
139
let session = store. load_session ( cookie_value) . await ?. unwrap ( ) ;
140
- assert_eq ! ( & session. get:: <String >( "key" ) . unwrap( ) , "other value" ) ;
140
+ assert_eq ! ( & mut session. get:: <String >( "key" ) . unwrap( ) , "other value" ) ;
141
141
142
142
Ok ( ( ) )
143
143
}
@@ -148,14 +148,14 @@ mod tests {
148
148
let mut session = Session :: new ( ) ;
149
149
session. expire_in ( Duration :: from_secs ( 1 ) ) ;
150
150
let original_expires = * session. expiry ( ) . unwrap ( ) ;
151
- let cookie_value = store. store_session ( session) . await ?. unwrap ( ) ;
151
+ let cookie_value = store. store_session ( & mut session) . await ?. unwrap ( ) ;
152
152
153
153
let mut session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
154
154
155
155
assert_eq ! ( session. expiry( ) . unwrap( ) , & original_expires) ;
156
156
session. expire_in ( Duration :: from_secs ( 3 ) ) ;
157
157
let new_expires = * session. expiry ( ) . unwrap ( ) ;
158
- assert_eq ! ( None , store. store_session( session) . await ?) ;
158
+ assert_eq ! ( None , store. store_session( & mut session) . await ?) ;
159
159
160
160
let session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
161
161
assert_eq ! ( session. expiry( ) . unwrap( ) , & new_expires) ;
@@ -174,7 +174,7 @@ mod tests {
174
174
session. insert ( "key" , "value" ) ?;
175
175
let cloned = session. clone ( ) ;
176
176
177
- let cookie_value = store. store_session ( session) . await ?. unwrap ( ) ;
177
+ let cookie_value = store. store_session ( & mut session) . await ?. unwrap ( ) ;
178
178
179
179
let loaded_session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
180
180
assert_eq ! ( cloned. id( ) , loaded_session. id( ) ) ;
@@ -192,26 +192,26 @@ mod tests {
192
192
async fn destroying_a_single_session ( ) -> Result < ( ) , MemoryStoreError > {
193
193
let store = MemoryStore :: new ( ) ;
194
194
for _ in 0 ..3i8 {
195
- store. store_session ( Session :: new ( ) ) . await ?;
195
+ store. store_session ( & mut Session :: new ( ) ) . await ?;
196
196
}
197
197
198
- let cookie = store. store_session ( Session :: new ( ) ) . await ?. unwrap ( ) ;
198
+ let cookie = store. store_session ( & mut Session :: new ( ) ) . await ?. unwrap ( ) ;
199
199
assert_eq ! ( 4 , store. count( ) ) ;
200
- let session = store. load_session ( cookie. clone ( ) ) . await ?. unwrap ( ) ;
201
- store. destroy_session ( session. clone ( ) ) . await ?;
200
+ let mut session = store. load_session ( cookie. clone ( ) ) . await ?. unwrap ( ) ;
201
+ store. destroy_session ( & mut session) . await ?;
202
202
assert_eq ! ( None , store. load_session( cookie) . await ?) ;
203
203
assert_eq ! ( 3 , store. count( ) ) ;
204
204
205
205
// attempting to destroy the session again is not an error
206
- assert ! ( store. destroy_session( session) . await . is_ok( ) ) ;
206
+ assert ! ( store. destroy_session( & mut session) . await . is_ok( ) ) ;
207
207
Ok ( ( ) )
208
208
}
209
209
210
210
#[ async_std:: test]
211
211
async fn clearing_the_whole_store ( ) -> Result < ( ) , MemoryStoreError > {
212
212
let store = MemoryStore :: new ( ) ;
213
213
for _ in 0 ..3i8 {
214
- store. store_session ( Session :: new ( ) ) . await ?;
214
+ store. store_session ( & mut Session :: new ( ) ) . await ?;
215
215
}
216
216
217
217
assert_eq ! ( 3 , store. count( ) ) ;
0 commit comments