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) ]
@@ -368,7 +369,7 @@ impl Session {
368
369
/// assert!(session.expiry().is_some());
369
370
/// # Ok(()) }) }
370
371
/// ```
371
- pub fn expiry ( & self ) -> Option < & DateTime < Utc > > {
372
+ pub fn expiry ( & self ) -> Option < & DateTime > {
372
373
self . expiry . as_ref ( )
373
374
}
374
375
@@ -381,11 +382,11 @@ impl Session {
381
382
/// # fn main() -> async_session::Result { async_std::task::block_on(async {
382
383
/// let mut session = Session::new();
383
384
/// assert_eq!(None, session.expiry());
384
- /// session.set_expiry(chrono::Utc::now ());
385
+ /// session.set_expiry(time::OffsetDateTime::now_utc ());
385
386
/// assert!(session.expiry().is_some());
386
387
/// # Ok(()) }) }
387
388
/// ```
388
- pub fn set_expiry ( & mut self , expiry : DateTime < Utc > ) {
389
+ pub fn set_expiry ( & mut self , expiry : DateTime ) {
389
390
self . expiry = Some ( expiry) ;
390
391
}
391
392
@@ -403,7 +404,7 @@ impl Session {
403
404
/// # Ok(()) }) }
404
405
/// ```
405
406
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) ;
407
408
}
408
409
409
410
/// predicate function to determine if this session is
@@ -428,7 +429,7 @@ impl Session {
428
429
/// ```
429
430
pub fn is_expired ( & self ) -> bool {
430
431
match self . expiry {
431
- Some ( expiry) => expiry < Utc :: now ( ) ,
432
+ Some ( expiry) => expiry < DateTime :: now_utc ( ) ,
432
433
None => false ,
433
434
}
434
435
}
@@ -522,7 +523,12 @@ impl Session {
522
523
/// ```
523
524
/// Duration from now to the expiry time of this session
524
525
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
+ }
526
532
}
527
533
528
534
/// takes the cookie value and consume this session.
0 commit comments