1- use chrono:: { DateTime , Duration , Utc } ;
21use rand:: RngCore ;
32use serde:: { Deserialize , Serialize } ;
43use std:: {
54 collections:: HashMap ,
5+ convert:: TryFrom ,
66 sync:: {
77 atomic:: { AtomicBool , Ordering } ,
88 Arc , RwLock ,
99 } ,
1010} ;
11+ use time:: OffsetDateTime as DateTime ;
1112
1213/// # The main session type.
1314///
@@ -56,7 +57,7 @@ use std::{
5657#[ derive( Debug , Serialize , Deserialize ) ]
5758pub struct Session {
5859 id : String ,
59- expiry : Option < DateTime < Utc > > ,
60+ expiry : Option < DateTime > ,
6061 data : Arc < RwLock < HashMap < String , String > > > ,
6162
6263 #[ serde( skip) ]
@@ -355,7 +356,7 @@ impl Session {
355356 /// assert!(session.expiry().is_some());
356357 /// # Ok(()) }) }
357358 /// ```
358- pub fn expiry ( & self ) -> Option < & DateTime < Utc > > {
359+ pub fn expiry ( & self ) -> Option < & DateTime > {
359360 self . expiry . as_ref ( )
360361 }
361362
@@ -368,11 +369,11 @@ impl Session {
368369 /// # fn main() -> async_session::Result { async_std::task::block_on(async {
369370 /// let mut session = Session::new();
370371 /// assert_eq!(None, session.expiry());
371- /// session.set_expiry(chrono::Utc::now ());
372+ /// session.set_expiry(time::OffsetDateTime::now_utc ());
372373 /// assert!(session.expiry().is_some());
373374 /// # Ok(()) }) }
374375 /// ```
375- pub fn set_expiry ( & mut self , expiry : DateTime < Utc > ) {
376+ pub fn set_expiry ( & mut self , expiry : DateTime ) {
376377 self . expiry = Some ( expiry) ;
377378 }
378379
@@ -390,7 +391,7 @@ impl Session {
390391 /// # Ok(()) }) }
391392 /// ```
392393 pub fn expire_in ( & mut self , ttl : std:: time:: Duration ) {
393- self . expiry = Some ( Utc :: now ( ) + Duration :: from_std ( ttl) . unwrap ( ) ) ;
394+ self . expiry = Some ( DateTime :: now_utc ( ) + ttl) ;
394395 }
395396
396397 /// predicate function to determine if this session is
@@ -415,7 +416,7 @@ impl Session {
415416 /// ```
416417 pub fn is_expired ( & self ) -> bool {
417418 match self . expiry {
418- Some ( expiry) => expiry < Utc :: now ( ) ,
419+ Some ( expiry) => expiry < DateTime :: now_utc ( ) ,
419420 None => false ,
420421 }
421422 }
@@ -509,7 +510,12 @@ impl Session {
509510 /// ```
510511 /// Duration from now to the expiry time of this session
511512 pub fn expires_in ( & self ) -> Option < std:: time:: Duration > {
512- self . expiry ?. signed_duration_since ( Utc :: now ( ) ) . to_std ( ) . ok ( )
513+ let dur = self . expiry ? - DateTime :: now_utc ( ) ;
514+ if dur. is_negative ( ) {
515+ None
516+ } else {
517+ std:: time:: Duration :: try_from ( dur) . ok ( )
518+ }
513519 }
514520
515521 /// takes the cookie value and consume this session.
0 commit comments