@@ -39,8 +39,8 @@ pub enum MemoryStoreError {
3939impl SessionStore for MemoryStore {
4040 type Error = MemoryStoreError ;
4141
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) ?;
4444 log:: trace!( "loading session by id `{}`" , id) ;
4545 let Occupied ( entry) = self . 0 . entry ( id) else {
4646 return Ok ( None ) ;
@@ -54,14 +54,15 @@ impl SessionStore for MemoryStore {
5454 }
5555 }
5656
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 > {
5858 log:: trace!( "storing session by id `{}`" , session. id( ) ) ;
5959 session. reset_data_changed ( ) ;
60+ let cookie_value = session. take_cookie_value ( ) ;
6061 self . 0 . insert ( session. id ( ) . to_string ( ) , session. clone ( ) ) ;
61- Ok ( session . into_cookie_value ( ) )
62+ Ok ( cookie_value )
6263 }
6364
64- async fn destroy_session ( & self , session : Session ) -> Result < ( ) , Self :: Error > {
65+ async fn destroy_session ( & self , session : & mut Session ) -> Result < ( ) , Self :: Error > {
6566 log:: trace!( "destroying session by id `{}`" , session. id( ) ) ;
6667 self . 0 . remove ( session. id ( ) ) ;
6768 Ok ( ( ) )
@@ -95,7 +96,7 @@ impl MemoryStore {
9596 /// # fn main() -> Result<(), Box<dyn std::error::Error>> { async_std::task::block_on(async {
9697 /// let mut store = MemoryStore::new();
9798 /// assert_eq!(store.count(), 0);
98- /// store.store_session(Session::new()).await?;
99+ /// store.store_session(&mut Session::new()).await?;
99100 /// assert_eq!(store.count(), 1);
100101 /// # Ok(()) }) }
101102 /// ```
@@ -115,8 +116,8 @@ mod tests {
115116 let mut session = Session :: new ( ) ;
116117 session. insert ( "key" , "Hello" ) ?;
117118 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 ( ) ;
120121 assert_eq ! ( cloned. id( ) , loaded_session. id( ) ) ;
121122 assert_eq ! ( "Hello" , & loaded_session. get:: <String >( "key" ) . unwrap( ) ) ;
122123 assert ! ( !loaded_session. is_expired( ) ) ;
@@ -130,14 +131,14 @@ mod tests {
130131 let mut session = Session :: new ( ) ;
131132
132133 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 ( ) ;
134135
135- let mut session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
136+ let mut session = store. load_session ( & cookie_value) . await ?. unwrap ( ) ;
136137 session. insert ( "key" , "other value" ) ?;
137138
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" ) ;
141142
142143 Ok ( ( ) )
143144 }
@@ -148,20 +149,20 @@ mod tests {
148149 let mut session = Session :: new ( ) ;
149150 session. expire_in ( Duration :: from_secs ( 1 ) ) ;
150151 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 ( ) ;
152153
153- let mut session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
154+ let mut session = store. load_session ( & cookie_value) . await ?. unwrap ( ) ;
154155
155156 assert_eq ! ( session. expiry( ) . unwrap( ) , & original_expires) ;
156157 session. expire_in ( Duration :: from_secs ( 3 ) ) ;
157158 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 ?) ;
159160
160- let session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
161+ let session = store. load_session ( & cookie_value) . await ?. unwrap ( ) ;
161162 assert_eq ! ( session. expiry( ) . unwrap( ) , & new_expires) ;
162163
163164 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 ?) ;
165166
166167 Ok ( ( ) )
167168 }
@@ -174,16 +175,16 @@ mod tests {
174175 session. insert ( "key" , "value" ) ?;
175176 let cloned = session. clone ( ) ;
176177
177- let cookie_value = store. store_session ( session) . await ?. unwrap ( ) ;
178+ let cookie_value = store. store_session ( & mut session) . await ?. unwrap ( ) ;
178179
179- let loaded_session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
180+ let loaded_session = store. load_session ( & cookie_value) . await ?. unwrap ( ) ;
180181 assert_eq ! ( cloned. id( ) , loaded_session. id( ) ) ;
181182 assert_eq ! ( "value" , & * loaded_session. get:: <String >( "key" ) . unwrap( ) ) ;
182183
183184 assert ! ( !loaded_session. is_expired( ) ) ;
184185
185186 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 ?) ;
187188
188189 Ok ( ( ) )
189190 }
@@ -192,26 +193,26 @@ mod tests {
192193 async fn destroying_a_single_session ( ) -> Result < ( ) , MemoryStoreError > {
193194 let store = MemoryStore :: new ( ) ;
194195 for _ in 0 ..3i8 {
195- store. store_session ( Session :: new ( ) ) . await ?;
196+ store. store_session ( & mut Session :: new ( ) ) . await ?;
196197 }
197198
198- let cookie = store. store_session ( Session :: new ( ) ) . await ?. unwrap ( ) ;
199+ let cookie = store. store_session ( & mut Session :: new ( ) ) . await ?. unwrap ( ) ;
199200 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 ?) ;
203204 assert_eq ! ( 3 , store. count( ) ) ;
204205
205206 // 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( ) ) ;
207208 Ok ( ( ) )
208209 }
209210
210211 #[ async_std:: test]
211212 async fn clearing_the_whole_store ( ) -> Result < ( ) , MemoryStoreError > {
212213 let store = MemoryStore :: new ( ) ;
213214 for _ in 0 ..3i8 {
214- store. store_session ( Session :: new ( ) ) . await ?;
215+ store. store_session ( & mut Session :: new ( ) ) . await ?;
215216 }
216217
217218 assert_eq ! ( 3 , store. count( ) ) ;
0 commit comments