Hi,
I want to have a callback that produces nalgebra DMatrixSlice from a know fixed DMatrix. I have the following code snippet:
use nalgebra::*;
struct Compute;
impl<'a> Compute {
fn run(&self, callback: Box<dyn Callback<'a>>) {
let my_slice = (*callback).make_slice(10, 10);
()
}
}
trait Callback<'a> {
fn make_slice(&self, idx: usize, len: usize) -> DMatrixSlice<'a, f32>;
}
struct MyCallback {
matrix: DMatrix<f32>
}
impl<'a> Callback<'a> for MyCallback {
fn make_slice(&self, idx: usize, len: usize) -> DMatrixSlice<'a, f32> {
self.matrix.slice((0, 0), (2, 2))
}
}
fn main() {
let mat = DMatrix::from_vec(3, 3, vec![1f32, 2f32, 3f32, 4f32, 5f32, 6f32, 7f32, 8f32, 9f32]);
let c = Compute{};
c.run(Box::new(MyCallback{
matrix: mat
}));
}
This yields the following errors:
/Users/mariusdanciu/.cargo/bin/cargo run --color=always --package nn --bin example
Compiling nn v0.1.0 (/Users/mariusdanciu/work/dev/projects/nn)
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/neunet/example.rs:23:21
|
23 | self.matrix.slice((0, 0), (2, 2))
| ^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 22:5...
--> src/neunet/example.rs:22:5
|
22 | / fn make_slice(&self, idx: usize, len: usize) -> DMatrixSlice<'a, f32> {
23 | | self.matrix.slice((0, 0), (2, 2))
24 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/neunet/example.rs:23:9
|
23 | self.matrix.slice((0, 0), (2, 2))
| ^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 21:6...
--> src/neunet/example.rs:21:6
|
21 | impl<'a> Callback<'a> for MyCallback {
| ^^
= note: ...so that the expression is assignable:
expected nalgebra::base::matrix::Matrix<_, _, _, nalgebra::base::matrix_slice::SliceStorage<'a, _, _, _, _, _>>
found nalgebra::base::matrix::Matrix<_, _, _, nalgebra::base::matrix_slice::SliceStorage<'_, _, _, _, _, _>>
error: aborting due to previous error
error: Could not compile `nn`.
To learn more, run the command again with --verbose.
Process finished with exit code 101
Any suggestions? I also tried with closures and cannot pass these types of errors.
Appreciate your help,
Marius