Skip to content

Commit 8f93f5e

Browse files
author
Paolo Tranquilli
committed
Rust: move to Label<T> and mark unsafety of from_untyped
1 parent faf1eee commit 8f93f5e

File tree

6 files changed

+1874
-2966
lines changed

6 files changed

+1874
-2966
lines changed

misc/codegen/generators/rustgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def _get_type(t: str) -> str:
2020
case "int":
2121
return "usize"
2222
case _ if t[0].isupper():
23-
return f"{t}TrapLabel"
23+
return f"trap::Label<{t}>"
2424
case "boolean":
2525
assert False, "boolean unsupported"
2626
case _:

misc/codegen/templates/rust_classes.mustache

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,6 @@
55
use crate::trap;
66
{{#classes}}
77

8-
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
9-
pub struct {{name}}TrapLabel(trap::UntypedLabel);
10-
11-
impl From<trap::UntypedLabel> for {{name}}TrapLabel {
12-
fn from(value: trap::UntypedLabel) -> Self {
13-
Self(value)
14-
}
15-
}
16-
17-
impl From<{{name}}TrapLabel> for trap::TrapId<{{name}}> {
18-
fn from(value: {{name}}TrapLabel) -> Self {
19-
Self::Label(value)
20-
}
21-
}
22-
23-
impl trap::Label for {{name}}TrapLabel {
24-
fn as_untyped(&self) -> trap::UntypedLabel {
25-
self.0
26-
}
27-
}
28-
29-
impl From<{{name}}TrapLabel> for trap::Arg {
30-
fn from(value: {{name}}TrapLabel) -> Self {
31-
value.0.into()
32-
}
33-
}
34-
358
{{#table_name}}
369
#[derive(Debug)]
3710
pub struct {{name}} {
@@ -48,7 +21,7 @@ impl trap::TrapEntry for {{name}} {
4821
std::mem::replace(&mut self.id, trap::TrapId::Star)
4922
}
5023
51-
fn emit(self, id: Self::Label, out: &mut trap::Writer) {
24+
fn emit(self, id: trap::Label<Self>, out: &mut trap::Writer) {
5225
{{#single_field_entries}}
5326
out.add_tuple("{{table_name}}", vec![id.into(){{#fields}}, self.{{field_name}}.into(){{/fields}}]);
5427
{{/single_field_entries}}
@@ -87,18 +60,14 @@ pub struct {{name}} {
8760
unused: ()
8861
}
8962
{{/table_name}}
90-
91-
impl trap::TrapClass for {{name}} {
92-
type Label = {{name}}TrapLabel;
93-
}
94-
{{/classes}}
95-
96-
// Conversions
97-
{{#classes}}
9863
{{#ancestors}}
99-
impl From<{{name}}TrapLabel> for {{.}}TrapLabel {
100-
fn from(value: {{name}}TrapLabel) -> Self {
101-
value.0.into()
64+
65+
impl From<trap::Label<{{name}}>> for trap::Label<{{.}}> {
66+
fn from(value: trap::Label<{{name}}>) -> Self {
67+
// SAFETY: this is safe because in the dbscheme {{name}} is a subclass of {{.}}
68+
unsafe {
69+
Self::from_untyped(value.as_untyped())
70+
}
10271
}
10372
}
10473
{{/ancestors}}

rust/extractor/src/generated/.generated.list

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)