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) ]
@@ -368,7 +369,7 @@ impl Session {
368369 /// assert!(session.expiry().is_some());
369370 /// # Ok(()) }) }
370371 /// ```
371- pub fn expiry ( & self ) -> Option < & DateTime < Utc > > {
372+ pub fn expiry ( & self ) -> Option < & DateTime > {
372373 self . expiry . as_ref ( )
373374 }
374375
@@ -381,11 +382,11 @@ impl Session {
381382 /// # fn main() -> async_session::Result { async_std::task::block_on(async {
382383 /// let mut session = Session::new();
383384 /// assert_eq!(None, session.expiry());
384- /// session.set_expiry(chrono::Utc::now ());
385+ /// session.set_expiry(time::OffsetDateTime::now_utc ());
385386 /// assert!(session.expiry().is_some());
386387 /// # Ok(()) }) }
387388 /// ```
388- pub fn set_expiry ( & mut self , expiry : DateTime < Utc > ) {
389+ pub fn set_expiry ( & mut self , expiry : DateTime ) {
389390 self . expiry = Some ( expiry) ;
390391 }
391392
@@ -403,7 +404,7 @@ impl Session {
403404 /// # Ok(()) }) }
404405 /// ```
405406 pub fn expire_in ( & mut self , ttl : std:: time:: Duration ) {
406- self . expiry = Some ( Utc :: now ( ) + Duration :: from_std ( ttl) . unwrap ( ) ) ;
407+ self . expiry = Some ( DateTime :: now_utc ( ) + ttl) ;
407408 }
408409
409410 /// predicate function to determine if this session is
@@ -428,7 +429,7 @@ impl Session {
428429 /// ```
429430 pub fn is_expired ( & self ) -> bool {
430431 match self . expiry {
431- Some ( expiry) => expiry < Utc :: now ( ) ,
432+ Some ( expiry) => expiry < DateTime :: now_utc ( ) ,
432433 None => false ,
433434 }
434435 }
@@ -522,7 +523,12 @@ impl Session {
522523 /// ```
523524 /// Duration from now to the expiry time of this session
524525 pub fn expires_in ( & self ) -> Option < std:: time:: Duration > {
525- self . expiry ?. signed_duration_since ( Utc :: now ( ) ) . to_std ( ) . ok ( )
526+ let dur = self . expiry ? - DateTime :: now_utc ( ) ;
527+ if dur. is_negative ( ) {
528+ None
529+ } else {
530+ std:: time:: Duration :: try_from ( dur) . ok ( )
531+ }
526532 }
527533
528534 /// takes the cookie value and consume this session.
0 commit comments