I want an arbitrary input, F
, that returns an arbitrary value, Z
. From the API perspective, I want something like this:
let mut action = ExecutableAction::create(move || {
true
});
In this case, Z
is an instance of bool
and F
is a function which returns Z
.
I tried this code:
use std::sync::{Arc, Mutex};
pub struct ExecutableAction<F, Z>
where
F: FnMut() -> Z + Send + Sync,
{
action: Arc<Mutex<Box<F>>>,
}
impl<F, Z> ExecutableAction<F, Z>
where
F: FnMut<()> + Send + Sync,
Z: Send + Sync,
{
pub fn create(closure: F) -> Self
where
F: FnMut() -> Z + Send + Sync,
{
Self {
action: Arc::new(Mutex::new(closure)),
}
}
pub fn call(&mut self) -> Z {
self.action.lock().call()
}
}
error[E0658]: the precise format of `Fn`-family traits' type parameters is subject to change. Use parenthetical notation (Fn(Foo, Bar) -> Baz) instead (see issue #29625)
--> src/lib.rs:12:8
|
12 | F: FnMut<()> + Send + Sync,
| ^^^^^^^^^
I'm sure my syntax is off, as it does not compile. How could I go about making this generically applicable? Also, what about denoting the arguments of F
with a type parameter R
? How might this be added on here?