I have a function that returns an impl Fn(Vector4<f64>) -> Vector4<f64>
. I would like to stick the result inside a Box<dyn Fn(Vector4<f64>) -> Vector4<f64>>
, but the compiler gives me an error when I try to do this. Everything seems to work fine if I replace Vector4<f64>
with u8
. Is there a reason that this does not work with Vector4
?
Example code:
extern crate nalgebra; // 0.21.1
use nalgebra::Vector4;
struct A {
par: Box<dyn Fn(Vector4<f64>) -> Vector4<f64>>
}
fn b() -> impl Fn(Vector4<f64>) -> Vector4<f64> {
|v| v
}
struct C {
par2: Box<dyn Fn(u8) -> u8>
}
fn d() -> impl Fn(u8) -> u8 {
|x| x
}
fn main() {
let par = Box::new(b());
// error
let a = A { par };
let par2 = Box::new(d());
// okay
let c = C { par2 };
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0277]: expected a `std::ops::Fn<(nalgebra::Matrix<f64, nalgebra::U4, nalgebra::U1, nalgebra::ArrayStorage<f64, nalgebra::U4, nalgebra::U1>>,)>` closure, found `impl std::ops::Fn<(nalgebra::Matrix<f64, nalgebra::U4, nalgebra::U1, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<f64, nalgebra::U4>>::Buffer>,)>`
--> src/lib.rs:23:17
|
23 | let a = A { par };
| ^^^ expected an `Fn<(nalgebra::Matrix<f64, nalgebra::U4, nalgebra::U1, nalgebra::ArrayStorage<f64, nalgebra::U4, nalgebra::U1>>,)>` closure, found `impl std::ops::Fn<(nalgebra::Matrix<f64, nalgebra::U4, nalgebra::U1, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<f64, nalgebra::U4>>::Buffer>,)>`
|
= help: the trait `std::ops::Fn<(nalgebra::Matrix<f64, nalgebra::U4, nalgebra::U1, nalgebra::ArrayStorage<f64, nalgebra::U4, nalgebra::U1>>,)>` is not implemented for `impl std::ops::Fn<(nalgebra::Matrix<f64, nalgebra::U4, nalgebra::U1, <nalgebra::DefaultAllocator as nalgebra::allocator::Allocator<f64, nalgebra::U4>>::Buffer>,)>`
= note: required for the cast to the object type `dyn std::ops::Fn(nalgebra::Matrix<f64, nalgebra::U4, nalgebra::U1, nalgebra::ArrayStorage<f64, nalgebra::U4, nalgebra::U1>>) -> nalgebra::Matrix<f64, nalgebra::U4, nalgebra::U1, nalgebra::ArrayStorage<f64, nalgebra::U4, nalgebra::U1>>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.