11use std:: {
22 fmt,
3+ io:: { Error , ErrorKind , Result } ,
34 sync:: {
45 atomic:: { AtomicU8 , Ordering } ,
56 Arc , RwLock ,
67 } ,
78} ;
89
910use serde:: { de:: DeserializeOwned , Serialize } ;
10- use serde_json:: { from_value, to_value, Value } ;
1111
12- use crate :: { Data , Error , State , CHANGED , PURGED , RENEWED , UNCHANGED } ;
12+ use crate :: { Data , State , CHANGED , PURGED , RENEWED , UNCHANGED } ;
1313
1414/// Session
1515#[ derive( Clone ) ]
@@ -39,24 +39,24 @@ impl Session {
3939 }
4040
4141 /// Gets a value by the key
42- pub fn get < T > ( & self , key : & str ) -> Result < Option < T > , Error >
42+ pub fn get < T > ( & self , key : & str ) -> Result < Option < T > >
4343 where
4444 T : DeserializeOwned ,
4545 {
4646 match self
4747 . lock_data ( )
4848 . read ( )
49- . map_err ( |e| Error :: RwLock ( e . to_string ( ) ) ) ?
49+ . map_err ( into_io_error ) ?
5050 . get ( key)
5151 . cloned ( )
5252 {
53- Some ( t) => from_value ( t) . map ( Some ) . map_err ( Error :: Json ) ,
53+ Some ( t) => serde_json :: from_value ( t) . map ( Some ) . map_err ( Into :: into ) ,
5454 None => Ok ( None ) ,
5555 }
5656 }
5757
5858 /// Sets a value by the key
59- pub fn set < T > ( & self , key : & str , val : T ) -> Result < ( ) , Error >
59+ pub fn set < T > ( & self , key : & str , val : T ) -> Result < ( ) >
6060 where
6161 T : Serialize ,
6262 {
@@ -68,14 +68,17 @@ impl Session {
6868 if status == UNCHANGED {
6969 self . status ( ) . store ( CHANGED , Ordering :: SeqCst ) ;
7070 }
71- d. insert ( key. into ( ) , to_value ( val) . map_err ( Error :: Json ) ?) ;
71+ d. insert (
72+ key. into ( ) ,
73+ serde_json:: to_value ( val) . map_err ( into_io_error) ?,
74+ ) ;
7275 }
7376 }
7477 Ok ( ( ) )
7578 }
7679
7780 /// Removes a value
78- pub fn remove ( & self , key : & str ) -> Option < Value > {
81+ pub fn remove ( & self , key : & str ) -> Option < serde_json :: Value > {
7982 let status = self . status ( ) . load ( Ordering :: Acquire ) ;
8083 // not allowed `PURGED`
8184 if status != PURGED {
@@ -95,7 +98,7 @@ impl Session {
9598 where
9699 T : DeserializeOwned ,
97100 {
98- self . remove ( key) . and_then ( |t| from_value ( t ) . ok ( ) )
101+ serde_json :: from_value ( self . remove ( key) ? ) . ok ( )
99102 }
100103
101104 /// Clears the state
@@ -135,10 +138,10 @@ impl Session {
135138 }
136139
137140 /// Gets all raw key-value data from the session
138- pub fn data ( & self ) -> Result < Data , Error > {
141+ pub fn data ( & self ) -> Result < Data > {
139142 self . lock_data ( )
140143 . read ( )
141- . map_err ( |e| Error :: RwLock ( e . to_string ( ) ) )
144+ . map_err ( into_io_error )
142145 . map ( |d| d. clone ( ) )
143146 }
144147}
@@ -148,3 +151,8 @@ impl fmt::Debug for Session {
148151 self . state . fmt ( f)
149152 }
150153}
154+
155+ #[ inline]
156+ fn into_io_error < E : std:: error:: Error > ( e : E ) -> Error {
157+ Error :: new ( ErrorKind :: Other , e. to_string ( ) )
158+ }
0 commit comments