Просмотр исходного кода

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

ghassmo 5 лет назад
Родитель
Сommit
4c36f822c2
4 измененных файлов с 13 добавлено и 7 удалено
  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 |_| {
-            let self2 = self1.clone();
+            //let self2 = self1.clone();
             async move {
                 println!("Deposit initiated");
                 //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>> {
+        let handle_error = Arc::new(handle_error);
         let rep = self
             .protocol
             .request(
                 CashierCommand::GetDBTC as u8,
                 serialize(&index),
-                &handle_error,
+                handle_error,
             )
             .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>> {
+        let handle_error = Arc::new(handle_error);
         let rep = self
             .protocol
             .request(
                 GatewayCommand::GetSlab as u8,
                 serialize(&index),
-                &handle_error,
+                handle_error,
             )
             .await?;
 
@@ -251,9 +252,11 @@ impl GatewayClient {
             slab.set_index(last_index + 1);
             let slab = serialize(&slab);
 
+            let handle_error = Arc::new(handle_error);
+
             let rep = self
                 .protocol
-                .request(GatewayCommand::PutSlab as u8, slab.clone(), &handle_error)
+                .request(GatewayCommand::PutSlab as u8, slab.clone(), handle_error)
                 .await?;
 
             if let Some(_) = rep {
@@ -264,9 +267,11 @@ impl GatewayClient {
     }
 
     pub async fn get_last_index(&mut self) -> Result<u64> {
+        let handle_error = Arc::new(handle_error);
+
         let rep = self
             .protocol
-            .request(GatewayCommand::GetLastIndex as u8, vec![], &handle_error)
+            .request(GatewayCommand::GetLastIndex as u8, vec![], handle_error)
             .await?;
         if let Some(index) = rep {
             return Ok(deserialize(&index)?);

+ 1 - 1
src/service/reqrep.rs

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