Transmuting associated type of trait ref + data ref
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 25/100
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
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus rust-lang/unsafe-code-guidelines
-
I-opsem-nominated
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 35/100
rust-lang/unsafe-code-guidelines#634 · 5 Kommentare ·
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 15/100
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 12/100
rust-lang/unsafe-code-guidelines#632 · 8 Kommentare · 1 Reaktion ·
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 10/100
rust-lang/unsafe-code-guidelines#631 · 7 Kommentare ·
-
I-opsem-nominated
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 20/100
rust-lang/unsafe-code-guidelines#630 · 1 Reaktion ·
Alle Issues in rust-lang/unsafe-code-guidelines
Ähnliche Issues
-
bug core-lang
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Call-argument syntax diagnostic needs example spacing and removal of confusing trailing sentenceOffen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
mw00/project-maya#1368 ·
Maintainer antworten meist innerhalb von 2 Tagen
-
test(bind-json): legacy Solidity fixture requires an unavailable solc on Linux ARM64Evtl. vergeben Ein verknüpfter Pull Request ist offen oder bereits gemergt. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
foundry-rs/foundry#18828 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
Maintainer antworten meist innerhalb von 1 Tag