diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-31 13:57:06 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-31 13:57:06 +0100 |
| commit | f8c4b8f33d3da5a943bfa29a14abdb3484122083 (patch) | |
| tree | 2516e74fe6a3f13199c2d407ce106db41e1ead7a | |
| parent | e5c27bcaf0e577f5b94c663f36ddc101ef1432ce (diff) | |
Add Clone and Copy to generated Rust types
18 files changed, 52 insertions, 47 deletions
diff --git a/examples/01-struct/output/rust/src/point.rs b/examples/01-struct/output/rust/src/point.rs index 6a90a6e..6865556 100644 --- a/examples/01-struct/output/rust/src/point.rs +++ b/examples/01-struct/output/rust/src/point.rs @@ -1,14 +1,14 @@ // Generated by hobgoblin. /// A straight line from one point to another. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Line { pub start: crate::point::Point, pub end: crate::point::Point, } /// A 2D point. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Point { pub x: ::core::primitive::i32, pub y: ::core::primitive::i32, diff --git a/examples/02-struct-singleton/output/rust/src/point.rs b/examples/02-struct-singleton/output/rust/src/point.rs index 866f0c5..53af3dd 100644 --- a/examples/02-struct-singleton/output/rust/src/point.rs +++ b/examples/02-struct-singleton/output/rust/src/point.rs @@ -1,4 +1,4 @@ // Generated by hobgoblin. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Origin; diff --git a/examples/03-sum/output/rust/src/shapes.rs b/examples/03-sum/output/rust/src/shapes.rs index a0a9b7f..2712e41 100644 --- a/examples/03-sum/output/rust/src/shapes.rs +++ b/examples/03-sum/output/rust/src/shapes.rs @@ -1,7 +1,7 @@ // Generated by hobgoblin. /// A 2D shape. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub enum Shape { Rectangle(crate::shapes::Rectangle), Triangle(crate::shapes::Triangle), @@ -19,13 +19,13 @@ impl ::std::convert::From::<crate::shapes::Triangle> for crate::shapes::Shape { } } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Rectangle { pub width: ::core::primitive::i32, pub height: ::core::primitive::i32, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Triangle { pub width: ::core::primitive::i32, pub height: ::core::primitive::i32, diff --git a/examples/04-inductive-data-types/output/rust/src/arithmetic.rs b/examples/04-inductive-data-types/output/rust/src/arithmetic.rs index 121c355..248d2c1 100644 --- a/examples/04-inductive-data-types/output/rust/src/arithmetic.rs +++ b/examples/04-inductive-data-types/output/rust/src/arithmetic.rs @@ -1,6 +1,6 @@ // Generated by hobgoblin. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub enum Expression { Add(::std::boxed::Box::<crate::arithmetic::Add>), Const(crate::arithmetic::Const), @@ -18,13 +18,13 @@ impl ::std::convert::From::<crate::arithmetic::Const> for crate::arithmetic::Exp } } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Add { pub left: crate::arithmetic::Expression, pub right: crate::arithmetic::Expression, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Const { pub value: ::core::primitive::i32, } diff --git a/examples/05-inductive-data-types-separate-namespaces/output/rust/src/data/a.rs b/examples/05-inductive-data-types-separate-namespaces/output/rust/src/data/a.rs index 3acec99..1ec823f 100644 --- a/examples/05-inductive-data-types-separate-namespaces/output/rust/src/data/a.rs +++ b/examples/05-inductive-data-types-separate-namespaces/output/rust/src/data/a.rs @@ -1,6 +1,6 @@ // Generated by hobgoblin. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub enum A { A1(::std::boxed::Box::<crate::data::a::A1>), } @@ -11,7 +11,7 @@ impl ::std::convert::From::<crate::data::a::A1> for crate::data::a::A { } } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct A1 { pub b: ::std::option::Option::<crate::data::b::B>, } diff --git a/examples/05-inductive-data-types-separate-namespaces/output/rust/src/data/b.rs b/examples/05-inductive-data-types-separate-namespaces/output/rust/src/data/b.rs index 11dee96..38bf8fc 100644 --- a/examples/05-inductive-data-types-separate-namespaces/output/rust/src/data/b.rs +++ b/examples/05-inductive-data-types-separate-namespaces/output/rust/src/data/b.rs @@ -1,6 +1,6 @@ // Generated by hobgoblin. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct B { pub a: ::std::option::Option::<crate::data::a::A>, } diff --git a/examples/06-enum/output/rust/src/numbers.rs b/examples/06-enum/output/rust/src/numbers.rs index 1c62012..29483ba 100644 --- a/examples/06-enum/output/rust/src/numbers.rs +++ b/examples/06-enum/output/rust/src/numbers.rs @@ -1,7 +1,7 @@ // Generated by hobgoblin. /// A small number. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Copy, Debug, Hash, PartialEq)] pub enum SmallNumber { Zero, One, diff --git a/examples/07-list/output/rust/src/point.rs b/examples/07-list/output/rust/src/point.rs index 2625f9f..ebadf95 100644 --- a/examples/07-list/output/rust/src/point.rs +++ b/examples/07-list/output/rust/src/point.rs @@ -1,11 +1,11 @@ // Generated by hobgoblin. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Path { pub points: ::std::vec::Vec::<crate::point::Point>, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Point { pub x: ::core::primitive::i32, pub y: ::core::primitive::i32, diff --git a/examples/08-native-type/output/rust/src/gen/point.rs b/examples/08-native-type/output/rust/src/gen/point.rs index d383b15..8561605 100644 --- a/examples/08-native-type/output/rust/src/gen/point.rs +++ b/examples/08-native-type/output/rust/src/gen/point.rs @@ -1,7 +1,7 @@ // Generated by hobgoblin. /// A straight line from one point to another. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Line { pub start: crate::point::Point, pub end: crate::point::Point, diff --git a/examples/08-native-type/output/rust/src/point.rs b/examples/08-native-type/output/rust/src/point.rs index c5b73d7..3c763e2 100644 --- a/examples/08-native-type/output/rust/src/point.rs +++ b/examples/08-native-type/output/rust/src/point.rs @@ -1,4 +1,4 @@ include!("gen/point.rs"); -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Point(pub i32, pub i32); diff --git a/examples/09-native-type-custom-config/output/rust/src/main.rs b/examples/09-native-type-custom-config/output/rust/src/main.rs index 10dc437..fa127e8 100644 --- a/examples/09-native-type-custom-config/output/rust/src/main.rs +++ b/examples/09-native-type-custom-config/output/rust/src/main.rs @@ -1,6 +1,6 @@ mod point; -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] struct Point(i32, i32); #[cfg(test)] diff --git a/examples/09-native-type-custom-config/output/rust/src/point.rs b/examples/09-native-type-custom-config/output/rust/src/point.rs index 6ab4330..f25a76f 100644 --- a/examples/09-native-type-custom-config/output/rust/src/point.rs +++ b/examples/09-native-type-custom-config/output/rust/src/point.rs @@ -1,7 +1,7 @@ // Generated by hobgoblin. /// A straight line from one point to another. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Line { pub start: crate::Point, pub end: crate::Point, diff --git a/examples/10-shared/output/rust/src/point.rs b/examples/10-shared/output/rust/src/point.rs index dea7678..7bc470a 100644 --- a/examples/10-shared/output/rust/src/point.rs +++ b/examples/10-shared/output/rust/src/point.rs @@ -1,14 +1,14 @@ // Generated by hobgoblin. /// A straight line from one point to another. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Line { pub start: ::std::sync::Arc::<crate::point::Point>, pub end: ::std::sync::Arc::<crate::point::Point>, } /// A 2D point. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Point { pub x: ::core::primitive::i32, pub y: ::core::primitive::i32, diff --git a/examples/11-custom-code/output/rust/src/gen/shapes.rs b/examples/11-custom-code/output/rust/src/gen/shapes.rs index 815db2a..d1582ad 100644 --- a/examples/11-custom-code/output/rust/src/gen/shapes.rs +++ b/examples/11-custom-code/output/rust/src/gen/shapes.rs @@ -1,6 +1,6 @@ // Generated by hobgoblin. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Square { pub width: ::core::primitive::i32, } diff --git a/examples/12-imports/output/rust/src/line.rs b/examples/12-imports/output/rust/src/line.rs index d383b15..8561605 100644 --- a/examples/12-imports/output/rust/src/line.rs +++ b/examples/12-imports/output/rust/src/line.rs @@ -1,7 +1,7 @@ // Generated by hobgoblin. /// A straight line from one point to another. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Line { pub start: crate::point::Point, pub end: crate::point::Point, diff --git a/examples/12-imports/output/rust/src/point.rs b/examples/12-imports/output/rust/src/point.rs index 943331f..6d9b010 100644 --- a/examples/12-imports/output/rust/src/point.rs +++ b/examples/12-imports/output/rust/src/point.rs @@ -1,7 +1,7 @@ // Generated by hobgoblin. /// A 2D point. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct Point { pub x: ::core::primitive::i32, pub y: ::core::primitive::i32, diff --git a/examples/13-transient-0/output/rust/src/gen/data.rs b/examples/13-transient-0/output/rust/src/gen/data.rs index bbaf4e7..50a0673 100644 --- a/examples/13-transient-0/output/rust/src/gen/data.rs +++ b/examples/13-transient-0/output/rust/src/gen/data.rs @@ -1,70 +1,70 @@ // Generated by hobgoblin. -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructSingleton; -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructWithBool { pub a: ::core::primitive::bool, pub b: ::core::primitive::bool, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructWithInt8 { pub a: ::core::primitive::i8, pub b: ::core::primitive::i8, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructWithInt32 { pub a: ::core::primitive::i32, pub b: ::core::primitive::i32, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructWithInt64 { pub a: ::core::primitive::i64, pub b: ::core::primitive::i64, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructWithString { pub a: ::std::string::String, pub b: ::std::string::String, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructWithStruct { pub inner: crate::data::StructWithInt32, pub c: ::core::primitive::i32, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructWithList { pub a: ::std::vec::Vec::<::core::primitive::i64>, pub b: ::std::vec::Vec::<crate::data::StructWithInt32>, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructWithOption { pub a: ::std::option::Option::<::core::primitive::i64>, pub b: ::std::option::Option::<crate::data::StructWithInt32>, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Copy, Debug, Hash, PartialEq)] pub enum EnumWithVariants { Zero, One, Two, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructWithEnum { pub inner: crate::data::EnumWithVariants, pub c: ::core::primitive::i32, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub enum SumWithVariants { VariantOne(crate::data::VariantOne), VariantTwo(crate::data::VariantTwo), @@ -82,35 +82,35 @@ impl ::std::convert::From::<crate::data::VariantTwo> for crate::data::SumWithVar } } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct VariantOne { pub a: ::core::primitive::i32, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct VariantTwo { pub a: ::core::primitive::i64, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructWithSum { pub inner: crate::data::SumWithVariants, pub b: ::core::primitive::i32, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructWithListOfShared { pub inner: ::std::vec::Vec::<::std::sync::Arc::<crate::data::StructWithInt32>>, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructWithDifferentSharedTypes { pub a: ::std::sync::Arc::<crate::data::StructWithInt32>, pub b: ::std::sync::Arc::<crate::data::StructWithInt64>, pub c: ::std::sync::Arc::<crate::data::StructWithInt32>, } -#[derive(Debug, Hash, PartialEq)] +#[derive(Clone, Debug, Hash, PartialEq)] pub struct StructWithSharedSumAndVariant { pub a: ::std::sync::Arc::<crate::data::SumWithVariants>, pub b: ::std::sync::Arc::<crate::data::VariantOne>, diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttypes/RustTypesGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttypes/RustTypesGenerator.java index 39a0b8b..0aaa723 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttypes/RustTypesGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttypes/RustTypesGenerator.java @@ -93,7 +93,7 @@ public class RustTypesGenerator implements Generator { TypedEnumDefinitionNode enumDefinition, Context context ) { - var rustAttributes = List.of(DERIVE_ATTRIBUTE); + var rustAttributes = List.of(ENUM_DERIVE_ATTRIBUTE); var rustEnumName = this.rustGenerator.generateTypeName(enumDefinition.name()); @@ -114,16 +114,21 @@ public class RustTypesGenerator implements Generator { return List.of(rustEnum); } - private static final RustOuterAttribute DERIVE_ATTRIBUTE = new RustOuterAttribute( + private static final RustOuterAttribute ENUM_DERIVE_ATTRIBUTE = new RustOuterAttribute( RustPath.of("derive"), - Optional.of("(Debug, Hash, PartialEq)") + Optional.of("(Clone, Copy, Debug, Hash, PartialEq)") + ); + + private static final RustOuterAttribute STRUCT_DERIVE_ATTRIBUTE = new RustOuterAttribute( + RustPath.of("derive"), + Optional.of("(Clone, Debug, Hash, PartialEq)") ); private List<RustItem> generateStructDefinition( TypedStructDefinitionNode structDefinition, Context context ) { - var rustAttributes = List.of(DERIVE_ATTRIBUTE); + var rustAttributes = List.of(STRUCT_DERIVE_ATTRIBUTE); var rustStructName = this.rustGenerator.generateTypeName(structDefinition.name()); @@ -150,7 +155,7 @@ public class RustTypesGenerator implements Generator { TypedSumDefinitionNode sumDefinition, Context context ) { - var rustAttributes = List.of(DERIVE_ATTRIBUTE); + var rustAttributes = List.of(STRUCT_DERIVE_ATTRIBUTE); var items = new ArrayList<RustItem>(); |
