diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-03 16:04:46 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-03 16:04:46 +0100 |
| commit | d6e0d3b3560f2a09187f255e43227d4b42642bc9 (patch) | |
| tree | 33f5a9c36fc31bea0cb6d191d0ed8fdd80513c4b /examples | |
| parent | 9b88f692e379490e14e814a9a198bd1b99fc3c09 (diff) | |
Implement From for Rust variants
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/02-sum/output/rust/src/shapes.rs | 8 | ||||
| -rw-r--r-- | examples/03-inductive-data-types/output/rust/src/arithmetic.rs | 8 | ||||
| -rw-r--r-- | examples/03-inductive-data-types/output/rust/src/main.rs | 14 |
3 files changed, 17 insertions, 13 deletions
diff --git a/examples/02-sum/output/rust/src/shapes.rs b/examples/02-sum/output/rust/src/shapes.rs index 31c3db9..d5b7334 100644 --- a/examples/02-sum/output/rust/src/shapes.rs +++ b/examples/02-sum/output/rust/src/shapes.rs @@ -6,12 +6,14 @@ pub enum Shape { Triangle(crate::shapes::Triangle), } -impl Shape { - pub fn rectangle(value: crate::shapes::Rectangle) -> Self { +impl ::std::convert::From<crate::shapes::Rectangle> for crate::shapes::Shape { + fn from(value: crate::shapes::Rectangle) -> Self { Self::Rectangle(value) } +} - pub fn triangle(value: crate::shapes::Triangle) -> Self { +impl ::std::convert::From<crate::shapes::Triangle> for crate::shapes::Shape { + fn from(value: crate::shapes::Triangle) -> Self { Self::Triangle(value) } } 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(); } |
