-
Notifications
You must be signed in to change notification settings - Fork 311
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
ndarray
iterators don't always work well with impl Iterator
#1290
Comments
There is something else going on with the slice though, i.e. if you remove the lifetime on return value type, it shows the error message
I.e. notice how the non-opaque type already contains only references to the lifetime |
That doesn't seem to be restricted to slices; the compiler also gives the same sort of warnings in the original example without any lifetime annotations on the return:
Note It seems like some bad type/lifetime inference is going on without lifetime annotations on the return type. Following the compiler's suggestions there leads to non-working code:
|
This is a bug/design limitation that could maybe be resolved in the next iterations of ndarray. It's a bug/limitation because the equivalent code for Vec works. My suspect for explanation is that it's because pub struct ArrayBase<S, D>
where S: RawData
{
/// Data buffer / ownership information. (If owned, contains the data
/// buffer; if borrowed, contains the lifetime and mutability.)
data: S,
/// A non-null pointer into the buffer held by `data`; may point anywhere
/// in its range. If `S: Data`, this pointer must be aligned.
ptr: std::ptr::NonNull<S::Elem>,
/// The lengths of the axes.
dim: D,
/// The element count stride per axis. To be parsed as `isize`.
strides: D,
} But it could also be something similar but for Baseiter (internal), not for ArrayBase. Maybe that makes even more sense, since that one is inside the return type.. |
The internal Baseiter type underlies most of the ndarray iterators, and it used `*mut A` for element type A. Update it to use `NonNull<A>` which behaves identically except it's guaranteed to be non-null and is covariant w.r.t the parameter A. Add compile test from the issue. Fixes #1290
The internal Baseiter type underlies most of the ndarray iterators, and it used `*mut A` for element type A. Update it to use `NonNull<A>` which behaves identically except it's guaranteed to be non-null and is covariant w.r.t the parameter A. Add compile test from the issue. Fixes #1290
The internal Baseiter type underlies most of the ndarray iterators, and it used `*mut A` for element type A. Update it to use `NonNull<A>` which behaves identically except it's guaranteed to be non-null and is covariant w.r.t the parameter A. Add compile test from the issue. Fixes #1290
When writing some iterator helper functions around the base
ndarray
iterators, I found a scenario where the borrow checker rejects what seems to be valid code. The scenario is generating an iterator over indices matching some criteria in anArray
of references.The original problem I had was with a 2D array with more complex types and filtering operation, but the error is the same.
Similar functions which work fine
I thought it might have been a limitation of the Rust compiler, but the same operation with a normal Rust slice (or
Vec
) works fine:It also works if the iterator returns references:
Workaround
I am able to work around the issue by creating a custom type for the iterator, but it seems like all the extra boilerplate shouldn't be necessary, especially since it isn't required when working with
Vec
:EDIT:
I found a much better work-around by specifying a more concrete return type. (Also changed it to
filter_map
to simplify the type).This isn't terrible to work with, but it's still weird that all of the types in
std::collections
work fine withimpl Iterator<Item = usize> + 'a
butndarray
types need the explicit return type.The text was updated successfully, but these errors were encountered: