summaryrefslogtreecommitdiff
path: root/examples/03-inductive-data-types
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-03 16:04:46 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-03 16:04:46 +0100
commitd6e0d3b3560f2a09187f255e43227d4b42642bc9 (patch)
tree33f5a9c36fc31bea0cb6d191d0ed8fdd80513c4b /examples/03-inductive-data-types
parent9b88f692e379490e14e814a9a198bd1b99fc3c09 (diff)
Implement From for Rust variants
Diffstat (limited to 'examples/03-inductive-data-types')
-rw-r--r--examples/03-inductive-data-types/output/rust/src/arithmetic.rs8
-rw-r--r--examples/03-inductive-data-types/output/rust/src/main.rs14
2 files changed, 12 insertions, 10 deletions
diff --git a/examples/03-inductive-data-types/output/rust/src/arithmetic.rs b/examples/03-inductive-data-types/output/rust/src/arithmetic.rs
index d6944a9..ac7c740 100644
--- a/examples/03-inductive-data-types/output/rust/src/arithmetic.rs
+++ b/examples/03-inductive-data-types/output/rust/src/arithmetic.rs
@@ -5,12 +5,14 @@ pub enum Expression {
Const(crate::arithmetic::Const),
}
-impl Expression {
- pub fn add(value: crate::arithmetic::Add) -> Self {
+impl ::std::convert::From<crate::arithmetic::Add> for crate::arithmetic::Expression {
+ fn from(value: crate::arithmetic::Add) -> Self {
Self::Add(::std::boxed::Box::new(value))
}
+}
- pub fn r#const(value: crate::arithmetic::Const) -> Self {
+impl ::std::convert::From<crate::arithmetic::Const> for crate::arithmetic::Expression {
+ fn from(value: crate::arithmetic::Const) -> Self {
Self::Const(value)
}
}
diff --git a/examples/03-inductive-data-types/output/rust/src/main.rs b/examples/03-inductive-data-types/output/rust/src/main.rs
index 28f7cec..61fd820 100644
--- a/examples/03-inductive-data-types/output/rust/src/main.rs
+++ b/examples/03-inductive-data-types/output/rust/src/main.rs
@@ -3,11 +3,11 @@ mod arithmetic;
use arithmetic::{Add, Const, Expression};
fn main() {
- let expression = Expression::add(Add {
- left: Expression::add(Add {
- left: Expression::r#const(Const { value: 42 }),
- right: Expression::r#const(Const { value: 47 }),
- }),
- right: Expression::r#const(Const { value: 52 }),
- });
+ let expression: Expression = Add {
+ left: Add {
+ left: Const { value: 42 }.into(),
+ right: Const { value: 47 }.into(),
+ }.into(),
+ right: Const { value: 52 }.into(),
+ }.into();
}