Kea*_*uan 2 closures channel rust
我想实现一个执行器,可以执行队列中的打包任务。首先,我希望使用闭包将不同类型的函数变成一种类型,并将闭包发送到通道中,然后在另一个线程中接收并执行它。代码如下:
use std::thread;
use std::sync::mpsc;
macro_rules! packed_task {
($f:ident, $($arg:expr),*) => {move ||{
$f($($arg,)*)
}};
}
macro_rules! packed_method_task {
($f:ident,$ins:ident, $($arg:expr),*) => {move ||{
$ins.$f($($arg,)*);
}};
($f:ident,$ins:ident $($arg:expr),*) => {move ||{
$ins.$f($($arg,)*);
}};
}
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
fn area1(&self, w:u32, h:u32) -> u32 {
w*h
}
}
fn invoke_1(a:i32, b:i32, c:i32)->i32{
let fc = |x,y,z| x+y+z + 1;
return packed_task!(fc, a, b,c)();
}
fn main() {
println!("{}", invoke_1(1,2,3));
let rect1 = Rectangle { width: 30, height: 50 };
let b = packed_method_task!(area1, rect1, 60, 90);
let (tx, rx) = mpsc::channel();
let handle= thread::spawn(move || {
let _received = rx.recv().unwrap();
_received();
});
tx.send(b).unwrap();
handle.join();
}
Run Code Online (Sandbox Code Playgroud)
但我编译时出现错误:
| let (tx, rx) = mpsc::channel();
| -------- consider giving this pattern the explicit type `(Sender<T>, std::sync::mpsc::Receiver<T>)`, with the type parameters
specified
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
每个闭包都是它自己的类型,并且 Rust(当前)没有任何语法来注释此类类型。即使确实如此,为这样的闭包创建一个通道也有点毫无意义,因为只有一个闭包可以通过通道发送。
相反,发送特征对象:
let b = Box::new(packed_method_task!(area1, rect1, 60, 90));
let (tx, rx) = mpsc::channel::<Box<dyn Send + Fn()>>();
Run Code Online (Sandbox Code Playgroud)
游乐场。