Skip to content

Add lifetime-preserving version of iterators #1232

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

Closed
RReverser opened this issue Nov 23, 2022 · 3 comments · Fixed by #1510
Closed

Add lifetime-preserving version of iterators #1232

RReverser opened this issue Nov 23, 2022 · 3 comments · Fixed by #1510

Comments

@RReverser
Copy link

Currently, ArrayView has a helpful lifetime-preserving methods like to_slice, but no way to preserve lifetimes of iterators like axis_iter, outer_iter and such.

This makes it difficult to return iterables from helper functions like this:

fn lazy_process(&self) -> impl '_ + Serialize {
	IterSerialize(self.data.outer_iter().map(move |column| {
	    IterSerialize(column.outer_iter().map(|pixel| pixel.to_slice().unwrap())) // <-- cannot return value referencing function parameter `column`
	}))
}

I suggest there should be overrides like into_outer_iter, into_axis_iter etc. on ArrayView that would return iterators tied by the lifetime to the original data instead of the ArrayView itself.

Note: while looking for open issues, the only relevant I found was #320, but there the author could afford to just reshape / flatten the data; that might not always be possible, like in my example above. @bluss mentioned issues and suggestions similar to what I'm describing here though: #320 (comment)

@RReverser
Copy link
Author

For the example above for now found a workaround like this but it's pretty ugly compared to what it could be:

IterSerialize(
    (0..self.data.len_of(ndarray::Axis(0))).map(move |column_i| {
        IterSerialize((0..self.data.len_of(ndarray::Axis(1))).map(move |row_i| {
            self.data
                .slice(ndarray::s![column_i, row_i, ..])
                .to_slice()
                .unwrap()
        }))
    }),
)

@akern40
Copy link
Collaborator

akern40 commented May 20, 2025

You're not gonna believe this... there is an into_axis_iter on the ArrayView types! It got listed as doc(hidden) in 2016 (6d33685) and shortly after as deprecated (5d98d80); I'm frankly not sure why. I'm happy to un-deprecate it and add the other .into_*_iter methods.

@RReverser
Copy link
Author

Thank you!

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

Successfully merging a pull request may close this issue.

2 participants