'how to save a function pointer with dyn parameters in a structure
I'm getting
error[E0308]: mismatched types
--> src/main.rs:38:39
|
37 | fn set_source_outcome<S: Source, O: Outcome>(&mut self, selector: fn(s: &S) -> &O) {
| - this type parameter
38 | self.selector = Some(Box::new(selector));
| ^^^^^^^^ expected trait object `dyn Source`, found type parameter `S`
|
= note: expected fn pointer `for<'r> fn(&'r (dyn Source + 'r)) -> &'r (dyn Outcome + 'r)`
found fn pointer `for<'r> fn(&'r S) -> &'r O`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
when trying to save a function with dyn parameters in a structure, like this:
struct X {
selector: Option<Box<fn(s: &dyn Source) -> &dyn Outcome>>,
}
impl X {
fn set_source_outcome<S: Source, O: Outcome>(&mut self, selector: fn(s: &S) -> &O) {
self.selector = Some(Box::new(selector));
}
}
Is there any way to do so? playground: here
Solution 1:[1]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

