0

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Thomas Braun
  • 505
  • 3
  • 14
  • **Please** include all of the code needed — I had to add `use` statements. **Please** include the error you are getting. – Shepmaster May 19 '19 at 13:11
  • TL;DR the duplicates: You are using an unstable syntax; use the syntax that you used on the function (`F: FnMut() -> Z`), not on the type (`F: FnMut<()>`). You then have to add a `Box::new`; you can't use `call()` (also unstable); you have to handle the error from `lock`; then work around an issue regarding `DerefMut`. It's unclear why you have duplicated the bound on `F` in two places. [All together, it looks like this](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=70570f1b7f5d871bdba5dfb3d252222f). – Shepmaster May 19 '19 at 13:23

0 Answers0