diff --git a/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs b/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs
index b80aa46..060f791 100644
--- a/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs
+++ b/Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs
@@ -474,6 +474,58 @@ public void EntryPointArguments_LeaveOutTheProgramPath()
});
}
+ ///
+ /// Tests that an enumeration's values can be compared, held in a record and read out of
+ /// &self, which each need a trait the enumeration has to derive (#255).
+ ///
+ [TestMethod]
+ public void EnumValuesCompareCopyAndSitInARecord_Compile()
+ {
+ if (ToolchainHarness.FindOnPath("--version", "rustc") is null)
+ {
+ Assert.Inconclusive("No Rust compiler on the path, so nothing was compiled.");
+ return;
+ }
+
+ // same(a, b) -> bool { return a == b; }
+ FunctionDeclaration same = new("same") { ReturnType = "bool" };
+ same.Parameters.Add(new Parameter("a", "Colour"));
+ same.Parameters.Add(new Parameter("b", "Colour"));
+ same.Body.Add(new ReturnStatement(
+ new BinaryExpression(new VariableReference("a"), BinaryOperator.Equal, new VariableReference("b"))));
+
+ // A record's derive needs every field to implement what it derives.
+ ClassDeclaration paint = new("Paint") { Kind = TypeDeclarationKind.Struct, IsRecord = true };
+ paint.Members.Add(new VariableDeclaration("colour", "Colour"));
+
+ // colour(&self) -> Colour { return self.colour; }
+ FunctionDeclaration colour = new("colour") { ReturnType = "Colour", IsReadOnly = true };
+ colour.Body.Add(new ReturnStatement(new VariableReference("self.colour")));
+ paint.Members.Add(colour);
+
+ SourceFile file = new("enums");
+ file.Members.Add(CompiledExemplar.Colour());
+ file.Members.Add(same);
+ file.Members.Add(paint);
+
+ ToolchainHarness.InTemporaryDirectory(directory =>
+ {
+ string source = new RustGenerator().Generate(file);
+ string allowed = "#![allow(unused_parens, dead_code)]";
+
+ File.WriteAllText(
+ Path.Combine(directory, "enums.rs"),
+ $"{allowed}{Environment.NewLine}{source}");
+
+ (int exitCode, string output) = ToolchainHarness.Run(
+ "rustc",
+ "--crate-type lib --edition 2021 -o enums.rlib enums.rs",
+ directory);
+
+ Assert.AreEqual(0, exitCode, $"rustc rejected the generated source:{Environment.NewLine}{source}{Environment.NewLine}{output}");
+ });
+ }
+
///
/// Builds a file holding one of everything the generator has a spelling for.
///
diff --git a/Coder.Test/Languages/RustGeneratorTests.cs b/Coder.Test/Languages/RustGeneratorTests.cs
index ad2d6b8..4bea166 100644
--- a/Coder.Test/Languages/RustGeneratorTests.cs
+++ b/Coder.Test/Languages/RustGeneratorTests.cs
@@ -558,7 +558,7 @@ public void Enum_IsScopedAndCanFixItsRepresentation()
string code = Generator.Generate(colour);
Assert.AreEqual(
- $"#[repr(i32)]{NewLine}pub enum Colour {{{NewLine} Red = 1,{NewLine} Green,{NewLine}}}{NewLine}",
+ $"#[repr(i32)]{NewLine}#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]{NewLine}pub enum Colour {{{NewLine} Red = 1,{NewLine} Green,{NewLine}}}{NewLine}",
code);
}
@@ -572,7 +572,8 @@ public void Enum_WithNoVariants_HasNoRepr()
string code = Generator.Generate(new EnumDeclaration("Empty") { UnderlyingType = "int" });
Assert.IsFalse(code.Contains("#[repr", StringComparison.Ordinal));
- StringAssert.StartsWith(code, "pub enum Empty {", StringComparison.Ordinal);
+ StringAssert.StartsWith(code, "#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]", StringComparison.Ordinal);
+ Assert.Contains("pub enum Empty {", code, StringComparison.Ordinal);
}
///
diff --git a/Coder/Languages/RustGenerator.cs b/Coder/Languages/RustGenerator.cs
index f2c909e..24009f7 100644
--- a/Coder/Languages/RustGenerator.cs
+++ b/Coder/Languages/RustGenerator.cs
@@ -1673,6 +1673,12 @@ private static string SpellStorageType(TypeReference type)
/// do. A fixed underlying type is #[repr], which is the guarantee rather than a comment
/// about one. An enumeration with no variants has no representation to fix, and rustc rejects
/// #[repr] on one (E0084), so it is left off.
+ ///
+ /// Every enumeration derives the traits a value of it needs to be compared, copied and printed.
+ /// The reason a plain struct derives nothing, that one of its fields might not implement them,
+ /// cannot apply to a fieldless enumeration, and without them a == b is E0369, a record
+ /// holding one is E0277, and reading one out of &self is E0507.
+ ///
///
protected override void GenerateEnumDeclaration(EnumDeclaration enumDecl, CodeBlocker code)
{
@@ -1687,6 +1693,7 @@ protected override void GenerateEnumDeclaration(EnumDeclaration enumDecl, CodeBl
}
string name = enumDecl.Name ?? "UnnamedEnum";
+ code.WriteLine("#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]");
code.Write($"{SpellVisibilityOf(enumDecl)}enum {name} ");
// A variant is not a value in a discriminant, so one naming another is cast to the