Transmuting associated type of trait ref + data ref

Offen
#577 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
25/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
rust
Bereich
compilers

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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

Vorherrschende Sprache
Keine Sprachdaten
Sterne
760
Forks
68
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Entwicklungsumgebung

Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus rust-lang/unsafe-code-guidelines

Alle Issues in rust-lang/unsafe-code-guidelines

Ähnliche Issues

Weitere Issues zu Compilers

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.