I want to create a structure that has a field mut_f
that contains mutable thread safe function(closure) that type is A -> A
For the initialization, the value is identity
closure.
let identity = |a| a;
struct Foo<A> {
mut_f: Arc<Mutex<dyn FnOnce(A) -> A>>
}
let foo = Foo {
mut_f: identity //error
};
This obviously has the error:
let identity = |a| a; mismatched types
expected struct
std::sync::Arc
, found closurenote: expected struct
std::sync::Arc<std::sync::Mutex<(dyn std::ops::FnOnce(_) -> _ + 'static)>>
found closure[closure@src/main.rs:36:20: 36:25]
help: use parentheses to call this closure:(a)
rustc(E0308) main.rs(36, 20): the found closure main.rs(44, 17): expected structstd::sync::Arc
, found closure No quick fixes available
and basically, I want to keep the closure definition as simple as possible because later I need to compose another functions(closures) in function composition manner.
So my question is what is the adequate/smart way to Cast closures to the Arc<Mutex<dyn FnOnce(A) -> A>>
type?