Transmuting associated type of trait ref + data ref

Đang mở
#577 2 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ó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
rust
Lĩnh vực
compilers

Hướng nghiên cứu

Start with crates/kas-core/src/core/node.rs from the linked full code, then compare the unsafe transmute-based implementation with its safe boxing alternative. Investigate whether the vtable and reference-layout assumptions are guaranteed by Rust's specification, and document whether this pattern has sound specified behavior.

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

Mô tả

I haven't seen this pattern discussed:

pub trait Widget {
    type Data;

    fn as_node<'a>(&'a mut self, data: &'a Self::Data) -> Node<'a>;

    fn child_node<'n>(&'n mut self, data: &'n Self::Data, index: usize) -> Option<Node<'n>>;

    // ... other methods which may take `&Self::Data` but do not use this type in any other way
}

pub struct Node<'a>(
    &'a mut dyn Widget<Data = ()>,
    &'a (),
);

impl<'a> Node<'a> {
    /// Construct
    #[inline(always)]
    pub fn new<T>(widget: &'a mut dyn Widget<Data = T>, data: &'a T) -> Self {
        // Safety: since the vtable for dyn Widget<Data = T> only uses T as &T
        // and T: Sized, the vtable should be equivalent for all T.
        // We ensure here that the type of `data` matches that used by `widget`.
        // NOTE: This makes assumptions beyond Rust's specification.
        use std::mem::transmute;
        unsafe { Node(transmute(widget), transmute(data)) }
    }

    pub fn get_child(&mut self, index: usize) -> Option<Node<'_>> {
        self.0.child_node(self.1, index)
    }
}

I believe this falls into the valley of "not UB but also not specified sound behaviour"? It makes some assumptions about the vtable and about &() not being optimised out, but does appear to work.

Note: type Widget::Data does not have a lifetime bound, but this can be changed if recommended — I don't think this changes anything however.

Full code (including a safe implementation using boxing) can be found here: https://github.com/kas-gui/kas/blob/master/crates/kas-core/src/core/node.rs

Ngôn ngữ chính
Không có dữ liệu ngôn ngữ
Star
760
Fork
68
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

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-lang/unsafe-code-guidelines

Tất cả issue của rust-lang/unsafe-code-guidelines

Issue tương tự

Thêm issue về Compilers

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.