1
- use chrono:: { DateTime , Duration , Utc } ;
2
1
use rand:: RngCore ;
3
2
use serde:: { Deserialize , Serialize } ;
4
3
use std:: {
5
4
collections:: HashMap ,
5
+ convert:: TryFrom ,
6
6
sync:: {
7
7
atomic:: { AtomicBool , Ordering } ,
8
8
Arc , RwLock ,
9
9
} ,
10
10
} ;
11
+ use time:: OffsetDateTime as DateTime ;
11
12
12
13
/// # The main session type.
13
14
///
@@ -56,7 +57,7 @@ use std::{
56
57
#[ derive( Debug , Serialize , Deserialize ) ]
57
58
pub struct Session {
58
59
id : String ,
59
- expiry : Option < DateTime < Utc > > ,
60
+ expiry : Option < DateTime > ,
60
61
data : Arc < RwLock < HashMap < String , String > > > ,
61
62
62
63
#[ serde( skip) ]
@@ -355,7 +356,7 @@ impl Session {
355
356
/// assert!(session.expiry().is_some());
356
357
/// # Ok(()) }) }
357
358
/// ```
358
- pub fn expiry ( & self ) -> Option < & DateTime < Utc > > {
359
+ pub fn expiry ( & self ) -> Option < & DateTime > {
359
360
self . expiry . as_ref ( )
360
361
}
361
362
@@ -368,11 +369,11 @@ impl Session {
368
369
/// # fn main() -> async_session::Result { async_std::task::block_on(async {
369
370
/// let mut session = Session::new();
370
371
/// assert_eq!(None, session.expiry());
371
- /// session.set_expiry(chrono::Utc::now ());
372
+ /// session.set_expiry(time::OffsetDateTime::now_utc ());
372
373
/// assert!(session.expiry().is_some());
373
374
/// # Ok(()) }) }
374
375
/// ```
375
- pub fn set_expiry ( & mut self , expiry : DateTime < Utc > ) {
376
+ pub fn set_expiry ( & mut self , expiry : DateTime ) {
376
377
self . expiry = Some ( expiry) ;
377
378
}
378
379
@@ -390,7 +391,7 @@ impl Session {
390
391
/// # Ok(()) }) }
391
392
/// ```
392
393
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) ;
394
395
}
395
396
396
397
/// predicate function to determine if this session is
@@ -415,7 +416,7 @@ impl Session {
415
416
/// ```
416
417
pub fn is_expired ( & self ) -> bool {
417
418
match self . expiry {
418
- Some ( expiry) => expiry < Utc :: now ( ) ,
419
+ Some ( expiry) => expiry < DateTime :: now_utc ( ) ,
419
420
None => false ,
420
421
}
421
422
}
@@ -509,7 +510,12 @@ impl Session {
509
510
/// ```
510
511
/// Duration from now to the expiry time of this session
511
512
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
+ }
513
519
}
514
520
515
521
/// takes the cookie value and consume this session.
0 commit comments