Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Owned MatrixView Lifetime issues #1393

Open
benjamin-lieser opened this issue May 15, 2024 · 2 comments
Open

Owned MatrixView Lifetime issues #1393

benjamin-lieser opened this issue May 15, 2024 · 2 comments

Comments

@benjamin-lieser
Copy link

benjamin-lieser commented May 15, 2024

Take the column() function as an example.

If it is called on a MatrixView the lifetime annotations are too restrictive.

Take this code as an example:

fn get_column_iter<'a>(
    matrix: na::DMatrixView<'a, u8>,
    column_id: (usize, usize),
) -> impl Iterator<Item = (u8,u8)> + 'a {
    let (left_id, right_id) = column_id;
    let left_half = matrix.column(left_id).into_iter();
    let right_half = matrix.column(right_id).into_iter();

    std::iter::zip(left_half, right_half)
}

The problem is that column takes a reference to the parameter matrix, so left_half has a lifetime to matrix but it should have the lifetime 'a

So if column (and other function which creates MatrixViews) should propagate the lifetime of the MatrixView on which they are called and not the lifetime of the reference to the MatrixView.

This is similar to #1315 which is also needed for this code to work. (So you need the current main branch otherwise you get different lifetime errors)

@Andlon
Copy link
Collaborator

Andlon commented Jun 21, 2024

There are several issues here, and I'm not quite sure if we can really resolve them. Most of these methods, such as column are methods on the very generic Matrix type. That also includes owned matrices, so column in its current form can never return a lifetime longer than &self, since then it would not be applicable to owned matrices.

I think your could could work if you rather take matrix by reference though. Could this help your use cases? I prefer to take views by value, so I absolutely sympathize, however.

@benjamin-lieser
Copy link
Author

Thanks for the answer, yes it does work by taking references to views. But it is not ideal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants