Mutual referencing and self-referencing structures not possible with Vec, FnvIndexMap, SortedLinkedList but with std variants

Đang mở
#296 5 bình luận 0 reaction 0 người được giao Xem trên GitHub

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
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
data

Hướng nghiên cứu

Bắt đầu với ví dụ Rust tối giản trong issue và so sánh heapless::Vec với std::vec::Vec và biến thể mảng. Sau đó, kiểm tra src/vec.rs xung quanh phần khởi tạo const được trích dẫn và lần theo cách FnvIndexMap sử dụng heapless::Vec. Công việc được xem là hoàn tất khi xác định được liệu hành vi lifetime có phải là chủ đích hay không và ghi lại kết luận, hoặc thêm phạm vi kiểm thử hồi quy tập trung nếu dự án hỗ trợ bản sửa lỗi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug status: blocked

Good day, sorry I don't really know how to name the problem, so here is a "minimal" example.

The following code will lead me into this error:

error[E0597]: `holder.b` does not live long enough
  --> src/main.rs:39:39
   |
39 |     holder.c.lookup.borrow_mut().push(&holder.b);
   |                                       ^^^^^^^^^ borrowed value does not live long enough
40 | }
   | -
   | |
   | `holder.b` dropped here while still borrowed
   | borrow might be used here, when `holder` is dropped and runs the destructor for type `Holder<'_>`
use core::cell::RefCell;

use heapless::{FnvIndexMap, Vec};

struct A {}

struct B<'a> {
    ref_a: RefCell<Option<&'a A>>,
}

struct C<'a> {
    lookup: RefCell<Vec<&'a B<'a>, 4>>,
}

struct Holder<'a> {
    c: C<'a>,
    b: B<'a>,
}

fn main() {
    let c = C {
        lookup: RefCell::new(Vec::new()),
    };

    let b = B {
        ref_a: RefCell::new(None),
    };

    let holder = Holder { c, b };

    holder.c.lookup.borrow_mut().push(&holder.b);
}

But with std::vec::Vec it is fine: lookup: RefCell<Vec<&'a B<'a>>>,
And also with a simple array.

use core::cell::RefCell;

use heapless::{FnvIndexMap, Vec};

struct A {}

struct B<'a> {
    ref_a: RefCell<Option<&'a A>>,
}

struct C<'a> {
    lookup: RefCell<[Option<&'a B<'a>>; 4]>,
}

struct Holder<'a> {
    c: C<'a>,
    b: B<'a>,
}

fn main() {
    let c = C {
        lookup: RefCell::new([None;4]),
    };

    let b = B {
        ref_a: RefCell::new(None),
    };

    let holder = Holder { c, b };

    holder.c.lookup.borrow_mut()[0] = Some(&holder.b);
}

I would expect this to be fine with heapless::Vec as well. The same problem exists with the heapless::FnvIndexMap which uses heapless::Vec internally.
Can this have something to do with the const initialization here ?
So my question is this intentional/ a trade-off that was deliberately chosen?

Ngôn ngữ chính
Rust
Star
2k
Fork
254
Merge trung bình
3 ngày 2 giờ
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của rust-embedded/heapless

Tất cả issue của rust-embedded/heapless

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.