Alternative to cloning tokio channel's sender for futures' closures
我正在使用
1 2 3 4 5 6 7 | // Defining the task let task = self.some_future_using .map(move |resp| println!("OK: {}", resp)) .map_err(move |e| println!("Error: {}",e)); // Spawning the task tokio::spawn(task); |
我想通过有限的tokio通道发送结果,而不是简单地记录结果。
1 2 3 4 5 6 7 8 9 10 | // Defines the channel let (tx, rx) = mpsc::channel(10); // Defining the task let task = self.some_future_using .map(|resp| /* Send Ok(resp) to tx */ ) .map_err(|e| /* Send Err(e) to tx */); // Spawning the task tokio::spawn(task); |
由于两个闭包都可能超出定义
1 2 3 4 5 6 7 8 9 10 11 12 | // Defines the channel let (tx, rx) = mpsc::channel(10); let mut tx_ok = tx.clone(); let mut tx_err = tx.clone(); // Defining the task let task = self.some_future_using .map(move |resp| tx_ok.try_send(Ok(())).unwrap() ) .map_err(move |e| tx_err.try_send(Ok(())).unwrap() ); // Spawning the task tokio::spawn(task); |
在使用组合器(
克隆是唯一的解决方案吗?我们能否在不为使用该声明的每个闭包克隆通道发送者的情况下实现相同的目的?
Could we achieve the same without cloning the channel's sender for each declared closure that uses it?
不。这是共享
通道通过将共享资源package在
克隆