6060macro_rules! canister_state {
6161 ( $type: ty) => {
6262 thread_local! {
63- static __STATE: std:: cell:: RefCell <Option <std :: rc :: Rc < $type> >> = std:: cell:: RefCell :: default ( ) ;
63+ static __STATE: std:: cell:: RefCell <Option <$type>> = std:: cell:: RefCell :: default ( ) ;
6464 }
6565
6666 const __STATE_ALREADY_INITIALIZED: & str = "State has already been initialized" ;
@@ -74,12 +74,11 @@ macro_rules! canister_state {
7474 /// # Panics
7575 /// Panics if the state has already been initialized
7676 pub fn init_state( state: $type) {
77- __STATE. with( |s| {
78- let mut lock = s. write( ) . unwrap( ) ;
79- if lock. is_some( ) {
77+ __STATE. with_borrow_mut( |s| {
78+ if s. is_some( ) {
8079 panic!( "{}" , __STATE_ALREADY_INITIALIZED) ;
8180 } else {
82- * s = Some ( std :: rc :: Rc :: new ( state) ) ;
81+ * s = Some ( state) ;
8382 }
8483 } ) ;
8584 }
@@ -95,7 +94,7 @@ macro_rules! canister_state {
9594 /// # Panics
9695 /// Panics if the state has not been initialized
9796 pub fn replace_state( state: $type) -> $type {
98- __STATE. replace( Some ( std :: rc :: Rc :: new ( state) ) ) . expect( __STATE_NOT_INITIALIZED) . as_ref ( ) . clone ( )
97+ __STATE. replace( Some ( state) ) . expect( __STATE_NOT_INITIALIZED)
9998 }
10099
101100 /// Takes ownership of the current state.
@@ -106,7 +105,7 @@ macro_rules! canister_state {
106105 /// # Panics
107106 /// Panics if the state has not been initialized
108107 pub fn take_state( ) -> $type {
109- __STATE. take( ) . expect( __STATE_NOT_INITIALIZED) . as_ref ( ) . clone ( )
108+ __STATE. take( ) . expect( __STATE_NOT_INITIALIZED)
110109 }
111110
112111 /// Reads the state using a closure.
@@ -123,7 +122,7 @@ macro_rules! canister_state {
123122 where
124123 F : FnOnce ( & $type) -> R ,
125124 {
126- __STATE. with_borrow( |s| f( s. as_ref( ) . expect( __STATE_NOT_INITIALIZED) . as_ref ( ) ) )
125+ __STATE. with_borrow( |s| f( s. as_ref( ) . expect( __STATE_NOT_INITIALIZED) ) )
127126 }
128127
129128 /// Mutates the state using a closure.
@@ -139,104 +138,6 @@ macro_rules! canister_state {
139138 pub fn mutate_state<F , R >( f: F ) -> R
140139 where
141140 F : FnOnce ( & mut $type) -> R ,
142- {
143- __STATE. with_borrow_mut( |s| {
144- let state = std:: rc:: Rc :: make_mut( s. as_mut( ) . expect( __STATE_NOT_INITIALIZED) ) ;
145- f( state)
146- } )
147- }
148-
149- /// Trait for async functions that can be used with state operations
150- /// This is temporary trait to be replaced once rust release official AsyncFn trait definition.
151- /// see https://github.com/rust-lang/rust/pull/132706
152- pub trait AsyncFn <Args >: FnOnce ( Args ) {
153- type Future : std:: future:: Future <Output = Self :: Output > + Send + ' static ;
154- type Output ;
155- }
156-
157- impl <F , Args , Fut , Out > AsyncFn <Args > for F
158- where
159- F : FnOnce ( Args , Output = Fut ) ,
160- Fut : std:: future:: Future <Output = Out > + Send + ' static ,
161- {
162- type Future = Fut ;
163- type Output = Out ;
164- }
165-
166- /// Reads the state using an async closure.
167- ///
168- /// # Arguments
169- /// * `f` - An async closure that takes a reference to the state and returns a value
170- ///
171- /// # Returns
172- /// A Future that will resolve to the result of the closure
173- ///
174- /// # Panics
175- /// Panics if the state has not been initialized
176- pub fn read_state_async<F >( f: F ) -> F :: Future
177- where
178- F : AsyncFn <( & $type, ) >,
179- F :: Future : Send + ' static ,
180- {
181- __STATE. with_borrow( |s| {
182- let state = s. as_ref( ) . expect( __STATE_NOT_INITIALIZED) . as_ref( ) ;
183- f( state)
184- } )
185- }
186-
187- /// Mutates the state using an async closure.
188- ///
189- /// # Arguments
190- /// * `f` - An async closure that takes a mutable reference to the state and returns a value
191- ///
192- /// # Returns
193- /// A Future that will resolve to the result of the closure
194- ///
195- /// # Panics
196- /// Panics if the state has not been initialized
197- pub fn mutate_state_async<F >( f: F ) -> F :: Future
198- where
199- F : AsyncFn <( & mut $type, ) >,
200- F :: Future : Send + ' static ,
201- {
202- __STATE. with_borrow_mut( |s| {
203- let state = std:: rc:: Rc :: make_mut( s. as_mut( ) . expect( __STATE_NOT_INITIALIZED) ) ;
204- f( state)
205- } )
206- }
207-
208- /// Reads the state using an async closure.
209- ///
210- /// # Arguments
211- /// * `f` - An async closure that takes a reference to the state and returns a future
212- ///
213- /// # Returns
214- /// A Future that will resolve to the result of the closure
215- ///
216- /// # Panics
217- /// Panics if the state has not been initialized
218- pub fn read_state_async<F , Fut , R >( f: F ) -> impl std:: future:: Future <Output = R >
219- where
220- F : for <' a> FnOnce ( & ' a $type) -> Fut ,
221- Fut : std:: future:: Future <Output = R >,
222- {
223- __STATE. with_borrow_mut( |s| f( s. as_mut( ) . expect( __STATE_NOT_INITIALIZED) ) )
224- }
225-
226- /// Mutates the state using an async closure.
227- ///
228- /// # Arguments
229- /// * `f` - An async closure that takes a mutable reference to the state and returns a future
230- ///
231- /// # Returns
232- /// A Future that will resolve to the result of the closure
233- ///
234- /// # Panics
235- /// Panics if the state has not been initialized
236- pub fn mutate_state_async<F , Fut , R >( f: F ) -> impl std:: future:: Future <Output = R >
237- where
238- F : for <' a> FnOnce ( & ' a mut $type) -> Fut ,
239- Fut : std:: future:: Future <Output = R >,
240141 {
241142 __STATE. with_borrow_mut( |s| f( s. as_mut( ) . expect( __STATE_NOT_INITIALIZED) ) )
242143 }
0 commit comments