I have written a problem solver in Rust which as a subroutine needs to make calls to a function which is given as a black box (essentially I would like to give an argument of type Fn(f64) -> f64
).
Essentially I have a function defined as fn solve<F>(f: F) where F : Fn(f64) -> f64 { ... }
which means that I can call solve
like this:
solve(|x| x);
What I would like to do is to pass a more complex function to the solver, i.e. a function which depends on multiple parameters etc.
I would like to be able to pass a struct with a suitable trait implementation to the solver. I tried the following:
struct Test;
impl Fn<(f64,)> for Test {}
This yield the following error:
error: the precise format of `Fn`-family traits' type parameters is subject to change. Use parenthetical notation (Fn(Foo, Bar) -> Baz) instead (see issue #29625)
I would also like to add a trait which includes the Fn
trait (which I don't know how to define, unfortunately). Is that possible as well?
Edit:
Just to clarify: I have been developing in C++ for quite a while, the C++ solution would be to overload the operator()(args)
. In that case I could use a struct
or class
like a function. I would like to be able to
- Pass both functions and structs to the solver as arguments.
- Have an easy way to call the functions. Calling
obj.method(args)
is more complicated thanobj(args)
(in C++). But it seems that this behavior is not achievable currently.