一个 FnMut
闭包由于明显的原因无法进行克隆,但是一个 Fn
闭包具有不可变的作用域;是否有某种方式可以创建一个 Fn
闭包的“副本”?
尝试克隆它会导致以下错误:
error[E0599]: no method named `clone` found for type `std::boxed::Box<std::ops::Fn(i8, i8) -> i8 + std::marker::Send + 'static>` in the current scope
--> src/main.rs:22:25
|
22 | fp: self.fp.clone(),
| ^^^^^
|
= note: self.fp is a function, perhaps you wish to call it
= note: the method `clone` exists but the following trait bounds were not satisfied:
`std::boxed::Box<std::ops::Fn(i8, i8) -> i8 + std::marker::Send> : std::clone::Clone`
将裸指针传递给Fn
是否安全,例如:
let func_pnt = &mut Box<Fn<...> + Send> as *mut Box<Fn<...>>
从技术上讲,以上方法可以行得通,但看起来相当奇怪。
以下是我正在尝试的示例:
use std::thread;
struct WithCall {
fp: Box<Fn(i8, i8) -> i8 + Send>,
}
impl WithCall {
pub fn new(fp: Box<Fn(i8, i8) -> i8 + Send>) -> WithCall {
WithCall { fp: fp }
}
pub fn run(&self, a: i8, b: i8) -> i8 {
(self.fp)(a, b)
}
}
impl Clone for WithCall {
fn clone(&self) -> WithCall {
WithCall {
fp: self.fp.clone(),
}
}
}
fn main() {
let adder = WithCall::new(Box::new(|a, b| a + b));
println!("{}", adder.run(1, 2));
let add_a = adder.clone();
let add_b = adder.clone();
let a = thread::spawn(move || {
println!("In remote thread: {}", add_a.run(10, 10));
});
let b = thread::spawn(move || {
println!("In remote thread: {}", add_b.run(10, 10));
});
a.join().expect("Thread A panicked");
b.join().expect("Thread B panicked");
}
我有一个在结构体中的闭包,我需要将该结构体传递给多个线程。但我不能直接传递,也不能克隆它,因为无法克隆Box<Fn<>>
,也无法克隆&Fn<...>
。
?Sized
表示它可以与 trait 对象一起使用。 - huoncore::ops::Fn(i8, i8) -> i8 + Send
没有实现 traitcore::marker::Sized
。 - Doug&Fn()
克隆直接作用。我预计,没有.clone()
也会出现相同的错误消息。 - huon