From 578bf1f04b48f48225cfa794a27426cca4f6384a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 31 Jul 2026 10:50:10 +0100 Subject: Support singleton structs --- examples/03-sum/output/rust/src/lib.rs | 29 ++++++++++++++++++++++++++++ examples/03-sum/output/rust/src/shapes.rs | 32 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 examples/03-sum/output/rust/src/lib.rs create mode 100644 examples/03-sum/output/rust/src/shapes.rs (limited to 'examples/03-sum/output/rust/src') diff --git a/examples/03-sum/output/rust/src/lib.rs b/examples/03-sum/output/rust/src/lib.rs new file mode 100644 index 0000000..f961002 --- /dev/null +++ b/examples/03-sum/output/rust/src/lib.rs @@ -0,0 +1,29 @@ +mod shapes; + +use shapes::{Rectangle, Shape, Triangle}; + +fn area(shape: &Shape) -> i32 { + match shape { + Shape::Rectangle(rectangle) => rectangle.width * rectangle.height, + Shape::Triangle(triangle) => triangle.width * triangle.height / 2, + } +} + +#[cfg(test)] +mod test { + use super::shapes::{Rectangle, Shape, Triangle}; + + #[test] + fn rectangle() { + let rectangle = Shape::Rectangle(Rectangle { width: 10, height: 25 }); + let area = super::area(&rectangle); + assert_eq!(area, 250); + } + + #[test] + fn triangle() { + let triangle = Shape::Triangle(Triangle { width: 10, height: 25 }); + let area = super::area(&triangle); + assert_eq!(area, 125); + } +} diff --git a/examples/03-sum/output/rust/src/shapes.rs b/examples/03-sum/output/rust/src/shapes.rs new file mode 100644 index 0000000..a0a9b7f --- /dev/null +++ b/examples/03-sum/output/rust/src/shapes.rs @@ -0,0 +1,32 @@ +// Generated by hobgoblin. + +/// A 2D shape. +#[derive(Debug, Hash, PartialEq)] +pub enum Shape { + Rectangle(crate::shapes::Rectangle), + Triangle(crate::shapes::Triangle), +} + +impl ::std::convert::From:: for crate::shapes::Shape { + fn from(value: crate::shapes::Rectangle) -> Self { + Self::Rectangle(value) + } +} + +impl ::std::convert::From:: for crate::shapes::Shape { + fn from(value: crate::shapes::Triangle) -> Self { + Self::Triangle(value) + } +} + +#[derive(Debug, Hash, PartialEq)] +pub struct Rectangle { + pub width: ::core::primitive::i32, + pub height: ::core::primitive::i32, +} + +#[derive(Debug, Hash, PartialEq)] +pub struct Triangle { + pub width: ::core::primitive::i32, + pub height: ::core::primitive::i32, +} -- cgit v1.2.3