Browse Source

wrap handle_error function with Arc & implement Sync and Send traits for the function

ghassmo 5 years ago
parent
commit
4c36f822c2
4 changed files with 13 additions and 7 deletions
  1. 2 2
      src/rpc/jsonserver.rs
  2. 2 1
      src/service/cashier.rs
  3. 8 3
      src/service/gateway.rs
  4. 1 1
      src/service/reqrep.rs

+ 2 - 2
src/rpc/jsonserver.rs

@@ -244,9 +244,9 @@ impl RpcInterface {
             }
             }
         });
         });
 
 
-        let mut self1 = self.clone();
+        //let mut self1 = self.clone();
         io.add_method("deposit", move |_| {
         io.add_method("deposit", move |_| {
-            let self2 = self1.clone();
+            //let self2 = self1.clone();
             async move {
             async move {
                 println!("Deposit initiated");
                 println!("Deposit initiated");
                 //let btckey = self2.adapter.deposit().await?;
                 //let btckey = self2.adapter.deposit().await?;

+ 2 - 1
src/service/cashier.rs

@@ -211,12 +211,13 @@ impl CashierClient {
     }
     }
 
 
     pub async fn get_address(&mut self, index: jubjub::SubgroupPoint) -> Result<Option<Address>> {
     pub async fn get_address(&mut self, index: jubjub::SubgroupPoint) -> Result<Option<Address>> {
+        let handle_error = Arc::new(handle_error);
         let rep = self
         let rep = self
             .protocol
             .protocol
             .request(
             .request(
                 CashierCommand::GetDBTC as u8,
                 CashierCommand::GetDBTC as u8,
                 serialize(&index),
                 serialize(&index),
-                &handle_error,
+                handle_error,
             )
             )
             .await?;
             .await?;
 
 

+ 8 - 3
src/service/gateway.rs

@@ -227,12 +227,13 @@ impl GatewayClient {
     }
     }
 
 
     pub async fn get_slab(&mut self, index: u64) -> Result<Option<Slab>> {
     pub async fn get_slab(&mut self, index: u64) -> Result<Option<Slab>> {
+        let handle_error = Arc::new(handle_error);
         let rep = self
         let rep = self
             .protocol
             .protocol
             .request(
             .request(
                 GatewayCommand::GetSlab as u8,
                 GatewayCommand::GetSlab as u8,
                 serialize(&index),
                 serialize(&index),
-                &handle_error,
+                handle_error,
             )
             )
             .await?;
             .await?;
 
 
@@ -251,9 +252,11 @@ impl GatewayClient {
             slab.set_index(last_index + 1);
             slab.set_index(last_index + 1);
             let slab = serialize(&slab);
             let slab = serialize(&slab);
 
 
+            let handle_error = Arc::new(handle_error);
+
             let rep = self
             let rep = self
                 .protocol
                 .protocol
-                .request(GatewayCommand::PutSlab as u8, slab.clone(), &handle_error)
+                .request(GatewayCommand::PutSlab as u8, slab.clone(), handle_error)
                 .await?;
                 .await?;
 
 
             if let Some(_) = rep {
             if let Some(_) = rep {
@@ -264,9 +267,11 @@ impl GatewayClient {
     }
     }
 
 
     pub async fn get_last_index(&mut self) -> Result<u64> {
     pub async fn get_last_index(&mut self) -> Result<u64> {
+        let handle_error = Arc::new(handle_error);
+
         let rep = self
         let rep = self
             .protocol
             .protocol
-            .request(GatewayCommand::GetLastIndex as u8, vec![], &handle_error)
+            .request(GatewayCommand::GetLastIndex as u8, vec![], handle_error)
             .await?;
             .await?;
         if let Some(index) = rep {
         if let Some(index) = rep {
             return Ok(deserialize(&index)?);
             return Ok(deserialize(&index)?);

+ 1 - 1
src/service/reqrep.rs

@@ -148,7 +148,7 @@ impl ReqProtocol {
         &mut self,
         &mut self,
         command: u8,
         command: u8,
         data: Vec<u8>,
         data: Vec<u8>,
-        handle_error: &dyn Fn(u32),
+        handle_error: Arc<dyn Fn(u32) + Send + Sync>,
     ) -> Result<Option<Vec<u8>>> {
     ) -> Result<Option<Vec<u8>>> {
         let request = Request::new(command, data);
         let request = Request::new(command, data);
         let req = serialize(&request);
         let req = serialize(&request);