Lifetime issues with `slice` methods
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 25/100
Hướng nghiên cứu
Start by reproducing the minimal Rust example from the playground link, comparing the slice_axis behavior in foo with slice_axis_inplace in bar and the explicit lifetimes in baz. Trace the slice and slice_axis APIs to determine which lifetime is tied to the returned view; done means the intended foo pattern is either supported with appropriate coverage or documented as requiring explicit lifetimes.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Hello folks,
I think I found an issue in the way lifetimes are inferred when using slice or slice_axis on array views.
Here is a minimal example with three functions, only bar and baz compile, while foo doesn't. I would argue that foo should be able to compile.
use ndarray; // 0.15.6
use ndarray::{ArrayBase, ViewRepr, Axis, Dimension, Slice, ArrayView};
fn main() {
let a = ndarray::array![[1, 2, 3], [4, 5, 6]];
foo(a.view(), ndarray::Axis(1));
}
fn foo<T, D>(arrayview: ArrayView<T, D>, axis: ndarray::Axis) -> ArrayView<T, D>
where
D: Dimension,
{
arrayview.slice_axis(axis, Slice::from(1..2))
}
fn bar<T, D>(arrayview: &mut ArrayView<T, D>, axis: Axis)
where
D: Dimension,
{
arrayview.slice_axis_inplace(axis, Slice::from(1..2));
}
fn baz<'a, 'data: 'a, T, D>(
arrayview: &'a ArrayBase<ViewRepr<&'data T>, D>,
axis: Axis,
) -> ArrayBase<ViewRepr<&'a T>, D>
where
D: Dimension,
{
arrayview.slice_axis(axis, Slice::from(1..2))
}
(playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7d62b20eec45d257e629dae07840544b)
The error is the following:
error[[E0311]](https://doc.rust-lang.org/stable/error-index.html#E0311): the parameter type `D` may not live long enough
--> src/main.rs:14:5
|
14 | arrayview.slice_axis(axis, Slice::from(1..2))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the parameter type `D` must be valid for the anonymous lifetime defined here...
--> src/main.rs:10:25
|
10 | fn foo<T, D>(arrayview: ArrayView<T, D>, axis: ndarray::Axis) -> ArrayView<T, D>
| ^^^^^^^^^^^^^^^
note: ...so that the type `D` will meet its required lifetime bounds
--> src/main.rs:14:5
|
14 | arrayview.slice_axis(axis, Slice::from(1..2))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider adding an explicit lifetime bound...
|
12 | D: Dimension + 'a,
| ++++
error[[E0515]](https://doc.rust-lang.org/stable/error-index.html#E0515): cannot return reference to function parameter `arrayview`
--> src/main.rs:14:5
|
14 | arrayview.slice_axis(axis, Slice::from(1..2))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returns a reference to data owned by the current function
For more information about this error, try `rustc --explain E0515`.
error: could not compile `playground` due to 2 previous errors
My understanding of the problem is the following: the lifetime of the data represented by the array view passed in as argument to foo is not propagated correctly to the one obtained by slicing it, and the output of the slice function is actually depending on the lifetime of the view passed as argument and not the lifetime of the data referenced by the view.
In bar I show how to circumnavigate the issue by slicing in place. Given that views are cheap to create and clone, this is the alternative I'm going with for now.
In baz, I show a version where I explicitly annotate lifetimes to make it work, note how I have to constrain 'a to 'data and how I need to make the argument a reference to avoid having the function drop it while the returned value still references it.
Also, I'm not sure why the error message is angry about the lifetime of D in the first place, I don't see where it's being defined as a reference or a pointer.
I'm not super good at understanding lifetimes but it feels like it should be possible to slice a view and obtain something that doesn't depend on the view itself but on the underlying data only. Or am I missing something here ?
- Ngôn ngữ chính
- Rust
- Star
- 4.3k
- Fork
- 391
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của rust-ndarray/ndarray
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
rust-ndarray/ndarray#1612 · 1 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
rust-ndarray/ndarray#1617 · 1 bình luận ·
-
Stack overflow in `triu` Đang mởbug good first issue
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
rust-ndarray/ndarray#1615 · 1 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
rust-ndarray/ndarray#1610 ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
rust-ndarray/ndarray#1609 ·
Tất cả issue của rust-ndarray/ndarray
Issue tương tự
-
risk:low runtime status:in-progress type:test
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
zeroclaw-labs/zeroclaw#11023 ·
-
good first issue refactor
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
EricSpencer00/Resilient#4835 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
bisq-network/bisq-musig#204 ·
-
agent:ready documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
cesarferreira/stax#890 ·