Transmuting associated type of trait ref + data ref

Ouverte
#577 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
5/5
Temps estimé
Plus d'une semaine
Accessibilité débutants
25/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
À l'abandon
Stack technique
rust
Domaine
compilers

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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

Langage dominant
Aucune donnée de langage
Étoiles
760
Forks
68
Métriques de merge des PR
Aucune PR mergée en 30 j

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de rust-lang/unsafe-code-guidelines

Toutes les issues de rust-lang/unsafe-code-guidelines

Issues similaires

Plus d'issues Compilers

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.