diff --git a/misc/codegen/generators/rustgen.py b/misc/codegen/generators/rustgen.py index 6fa5d4a05ec..e94157a94a3 100644 --- a/misc/codegen/generators/rustgen.py +++ b/misc/codegen/generators/rustgen.py @@ -20,7 +20,7 @@ def _get_type(t: str) -> str: case "int": return "usize" case _ if t[0].isupper(): - return f"{t}TrapLabel" + return f"trap::Label<{t}>" case "boolean": assert False, "boolean unsupported" case _: diff --git a/misc/codegen/templates/rust_classes.mustache b/misc/codegen/templates/rust_classes.mustache index f749733b2bd..e774197133f 100644 --- a/misc/codegen/templates/rust_classes.mustache +++ b/misc/codegen/templates/rust_classes.mustache @@ -5,33 +5,6 @@ use crate::trap; {{#classes}} -#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct {{name}}TrapLabel(trap::UntypedLabel); - -impl From for {{name}}TrapLabel { - fn from(value: trap::UntypedLabel) -> Self { - Self(value) - } -} - -impl From<{{name}}TrapLabel> for trap::TrapId<{{name}}> { - fn from(value: {{name}}TrapLabel) -> Self { - Self::Label(value) - } -} - -impl trap::Label for {{name}}TrapLabel { - fn as_untyped(&self) -> trap::UntypedLabel { - self.0 - } -} - -impl From<{{name}}TrapLabel> for trap::Arg { - fn from(value: {{name}}TrapLabel) -> Self { - value.0.into() - } -} - {{#table_name}} #[derive(Debug)] pub struct {{name}} { @@ -48,7 +21,7 @@ impl trap::TrapEntry for {{name}} { std::mem::replace(&mut self.id, trap::TrapId::Star) } - fn emit(self, id: Self::Label, out: &mut trap::Writer) { + fn emit(self, id: trap::Label, out: &mut trap::Writer) { {{#single_field_entries}} out.add_tuple("{{table_name}}", vec![id.into(){{#fields}}, self.{{field_name}}.into(){{/fields}}]); {{/single_field_entries}} @@ -87,18 +60,14 @@ pub struct {{name}} { unused: () } {{/table_name}} - -impl trap::TrapClass for {{name}} { - type Label = {{name}}TrapLabel; -} -{{/classes}} - -// Conversions -{{#classes}} {{#ancestors}} -impl From<{{name}}TrapLabel> for {{.}}TrapLabel { - fn from(value: {{name}}TrapLabel) -> Self { - value.0.into() + +impl From> for trap::Label<{{.}}> { + fn from(value: trap::Label<{{name}}>) -> Self { + // SAFETY: this is safe because in the dbscheme {{name}} is a subclass of {{.}} + unsafe { + Self::from_untyped(value.as_untyped()) + } } } {{/ancestors}} diff --git a/rust/extractor/src/generated/.generated.list b/rust/extractor/src/generated/.generated.list index fd7cc5c6614..10382e0fb5d 100644 --- a/rust/extractor/src/generated/.generated.list +++ b/rust/extractor/src/generated/.generated.list @@ -1,2 +1,2 @@ mod.rs 7cdfedcd68cf8e41134daf810c1af78624082b0c3e8be6570339b1a69a5d457e 7cdfedcd68cf8e41134daf810c1af78624082b0c3e8be6570339b1a69a5d457e -top.rs e06dc90de4abd57719786fd5e49e6ea3089ec3ec167c64446e25d95a16b1714c e06dc90de4abd57719786fd5e49e6ea3089ec3ec167c64446e25d95a16b1714c +top.rs 775cb04552f9242ff999b279a375f7a446124f4ef28668e42496751bf69e7dd3 775cb04552f9242ff999b279a375f7a446124f4ef28668e42496751bf69e7dd3 diff --git a/rust/extractor/src/generated/top.rs b/rust/extractor/src/generated/top.rs index 084fdbb61f0..2c476bb3bf8 100644 --- a/rust/extractor/src/generated/top.rs +++ b/rust/extractor/src/generated/top.rs @@ -4,33 +4,6 @@ use crate::trap; -#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct ElementTrapLabel(trap::UntypedLabel); - -impl From for ElementTrapLabel { - fn from(value: trap::UntypedLabel) -> Self { - Self(value) - } -} - -impl From for trap::TrapId { - fn from(value: ElementTrapLabel) -> Self { - Self::Label(value) - } -} - -impl trap::Label for ElementTrapLabel { - fn as_untyped(&self) -> trap::UntypedLabel { - self.0 - } -} - -impl From for trap::Arg { - fn from(value: ElementTrapLabel) -> Self { - value.0.into() - } -} - #[derive(Debug)] pub struct Element { pub id: trap::TrapId, @@ -43,42 +16,11 @@ impl trap::TrapEntry for Element { std::mem::replace(&mut self.id, trap::TrapId::Star) } - fn emit(self, id: Self::Label, out: &mut trap::Writer) { + fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("elements", vec![id.into()]); } } -impl trap::TrapClass for Element { - type Label = ElementTrapLabel; -} - -#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct LocatableTrapLabel(trap::UntypedLabel); - -impl From for LocatableTrapLabel { - fn from(value: trap::UntypedLabel) -> Self { - Self(value) - } -} - -impl From for trap::TrapId { - fn from(value: LocatableTrapLabel) -> Self { - Self::Label(value) - } -} - -impl trap::Label for LocatableTrapLabel { - fn as_untyped(&self) -> trap::UntypedLabel { - self.0 - } -} - -impl From for trap::Arg { - fn from(value: LocatableTrapLabel) -> Self { - value.0.into() - } -} - #[derive(Debug)] pub struct Locatable { pub id: trap::TrapId, @@ -91,39 +33,17 @@ impl trap::TrapEntry for Locatable { std::mem::replace(&mut self.id, trap::TrapId::Star) } - fn emit(self, id: Self::Label, out: &mut trap::Writer) { + fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("locatables", vec![id.into()]); } } -impl trap::TrapClass for Locatable { - type Label = LocatableTrapLabel; -} - -#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct AstNodeTrapLabel(trap::UntypedLabel); - -impl From for AstNodeTrapLabel { - fn from(value: trap::UntypedLabel) -> Self { - Self(value) - } -} - -impl From for trap::TrapId { - fn from(value: AstNodeTrapLabel) -> Self { - Self::Label(value) - } -} - -impl trap::Label for AstNodeTrapLabel { - fn as_untyped(&self) -> trap::UntypedLabel { - self.0 - } -} - -impl From for trap::Arg { - fn from(value: AstNodeTrapLabel) -> Self { - value.0.into() +impl From> for trap::Label { + fn from(value: trap::Label) -> Self { + // SAFETY: this is safe because in the dbscheme Locatable is a subclass of Element + unsafe { + Self::from_untyped(value.as_untyped()) + } } } @@ -139,39 +59,26 @@ impl trap::TrapEntry for AstNode { std::mem::replace(&mut self.id, trap::TrapId::Star) } - fn emit(self, id: Self::Label, out: &mut trap::Writer) { + fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("ast_nodes", vec![id.into()]); } } -impl trap::TrapClass for AstNode { - type Label = AstNodeTrapLabel; -} - -#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct DeclarationTrapLabel(trap::UntypedLabel); - -impl From for DeclarationTrapLabel { - fn from(value: trap::UntypedLabel) -> Self { - Self(value) +impl From> for trap::Label { + fn from(value: trap::Label) -> Self { + // SAFETY: this is safe because in the dbscheme AstNode is a subclass of Element + unsafe { + Self::from_untyped(value.as_untyped()) + } } } -impl From for trap::TrapId { - fn from(value: DeclarationTrapLabel) -> Self { - Self::Label(value) - } -} - -impl trap::Label for DeclarationTrapLabel { - fn as_untyped(&self) -> trap::UntypedLabel { - self.0 - } -} - -impl From for trap::Arg { - fn from(value: DeclarationTrapLabel) -> Self { - value.0.into() +impl From> for trap::Label { + fn from(value: trap::Label) -> Self { + // SAFETY: this is safe because in the dbscheme AstNode is a subclass of Locatable + unsafe { + Self::from_untyped(value.as_untyped()) + } } } @@ -187,39 +94,35 @@ impl trap::TrapEntry for Declaration { std::mem::replace(&mut self.id, trap::TrapId::Star) } - fn emit(self, id: Self::Label, out: &mut trap::Writer) { + fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("declarations", vec![id.into()]); } } -impl trap::TrapClass for Declaration { - type Label = DeclarationTrapLabel; -} - -#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct ExprTrapLabel(trap::UntypedLabel); - -impl From for ExprTrapLabel { - fn from(value: trap::UntypedLabel) -> Self { - Self(value) +impl From> for trap::Label { + fn from(value: trap::Label) -> Self { + // SAFETY: this is safe because in the dbscheme Declaration is a subclass of AstNode + unsafe { + Self::from_untyped(value.as_untyped()) + } } } -impl From for trap::TrapId { - fn from(value: ExprTrapLabel) -> Self { - Self::Label(value) +impl From> for trap::Label { + fn from(value: trap::Label) -> Self { + // SAFETY: this is safe because in the dbscheme Declaration is a subclass of Element + unsafe { + Self::from_untyped(value.as_untyped()) + } } } -impl trap::Label for ExprTrapLabel { - fn as_untyped(&self) -> trap::UntypedLabel { - self.0 - } -} - -impl From for trap::Arg { - fn from(value: ExprTrapLabel) -> Self { - value.0.into() +impl From> for trap::Label { + fn from(value: trap::Label) -> Self { + // SAFETY: this is safe because in the dbscheme Declaration is a subclass of Locatable + unsafe { + Self::from_untyped(value.as_untyped()) + } } } @@ -235,39 +138,35 @@ impl trap::TrapEntry for Expr { std::mem::replace(&mut self.id, trap::TrapId::Star) } - fn emit(self, id: Self::Label, out: &mut trap::Writer) { + fn emit(self, id: trap::Label, out: &mut trap::Writer) { out.add_tuple("exprs", vec![id.into()]); } } -impl trap::TrapClass for Expr { - type Label = ExprTrapLabel; -} - -#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct LabelTrapLabel(trap::UntypedLabel); - -impl From for LabelTrapLabel { - fn from(value: trap::UntypedLabel) -> Self { - Self(value) +impl From> for trap::Label { + fn from(value: trap::Label) -> Self { + // SAFETY: this is safe because in the dbscheme Expr is a subclass of AstNode + unsafe { + Self::from_untyped(value.as_untyped()) + } } } -impl From for trap::TrapId