summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-09 17:12:46 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-09 17:12:46 +0100
commitcc0f7190d1254ec89354cea8b2ed0aaecbb3b391 (patch)
tree5f93488e63a23c5903f514ccd87a0801d3222eaf /src/main/java
parenta52ff23faed545f7f8ec2b0fa3cb0c7a31df3068 (diff)
Generate Encode and Decode types
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java68
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustTypes.java2
2 files changed, 59 insertions, 11 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java
index 9d959bd..49a35f6 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java
@@ -71,6 +71,8 @@ public class RustTransient0Generator implements Generator {
var rustModule = new RustModule(
rustModuleName,
List.of(
+ generateEncodeType(),
+ generateDecodeType(),
generateEncodeBoolFunction(),
generateDecodeBoolFunction(),
generateEncodeInt8Function(),
@@ -87,6 +89,47 @@ public class RustTransient0Generator implements Generator {
this.rustGenerator.write(rustModule);
}
+ private RustItem generateEncodeType() {
+ var valueType = RustIdentifier.of("T");
+ var writeType = RustIdentifier.of("TWrite");
+ return new RustTypeAlias(
+ Optional.of(RustVisibility.PUB),
+ RustIdentifier.of("Encode"),
+ List.of(
+ new RustTypeParam(valueType, List.of()),
+ new RustTypeParam(writeType, List.of(RustTypes.IO_WRITE))
+ ),
+ new RustFunctionPointerType(
+ List.of(
+ new RustSharedReferenceType(RustPath.of(valueType)),
+ new RustMutableReferenceType(RustPath.of(writeType)),
+ encoderSharedValuesType()
+ ),
+ RustTypes.ioResult(RustTypes.UNIT)
+ )
+ );
+ }
+
+ private RustItem generateDecodeType() {
+ var valueType = RustIdentifier.of("T");
+ var readType = RustIdentifier.of("TRead");
+ return new RustTypeAlias(
+ Optional.of(RustVisibility.PUB),
+ RustIdentifier.of("Decode"),
+ List.of(
+ new RustTypeParam(readType, List.of(RustTypes.IO_READ)),
+ new RustTypeParam(valueType, List.of())
+ ),
+ new RustFunctionPointerType(
+ List.of(
+ new RustMutableReferenceType(RustPath.of(readType)),
+ decoderSharedValuesType()
+ ),
+ RustTypes.ioResult(RustPath.of(valueType))
+ )
+ );
+ }
+
private RustItem generateEncodeBoolFunction() {
return generateEncodeFunction(
NativeTypes.BOOL,
@@ -548,12 +591,13 @@ public class RustTransient0Generator implements Generator {
return List.of(
new RustFunctionParam(VALUE_NAME, new RustSharedReferenceType(rustType)),
new RustFunctionParam(WRITER_NAME, new RustMutableReferenceType(new RustImplTraitType(RustPath.of("std", "io", "Write")))),
- new RustFunctionParam(
- SHARED_VALUES_NAME,
- new RustMutableReferenceType(
- RustTypes.hashMap(RustTypes.USIZE, RustPath.primitive("i64"))
- )
- )
+ new RustFunctionParam(SHARED_VALUES_NAME, encoderSharedValuesType())
+ );
+ }
+
+ private static RustMutableReferenceType encoderSharedValuesType() {
+ return new RustMutableReferenceType(
+ RustTypes.hashMap(RustTypes.USIZE, RustPath.primitive("i64"))
);
}
@@ -578,11 +622,13 @@ public class RustTransient0Generator implements Generator {
private static List<RustFunctionParam> generateDecodeParams() {
return List.of(
new RustFunctionParam(READER_NAME, new RustMutableReferenceType(new RustImplTraitType(RustPath.of("std", "io", "Read")))),
- new RustFunctionParam(
- SHARED_VALUES_NAME, new RustMutableReferenceType(
- RustTypes.vec(RustTypes.arc(RustTypes.dyn(RustTypes.ANY, RustTypes.SYNC, RustTypes.SEND)))
- )
- )
+ new RustFunctionParam(SHARED_VALUES_NAME, decoderSharedValuesType())
+ );
+ }
+
+ private static RustMutableReferenceType decoderSharedValuesType() {
+ return new RustMutableReferenceType(
+ RustTypes.vec(RustTypes.arc(RustTypes.dyn(RustTypes.ANY, RustTypes.SYNC, RustTypes.SEND)))
);
}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustTypes.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustTypes.java
index 3c5ab6c..b3d65d4 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustTypes.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustTypes.java
@@ -36,8 +36,10 @@ public class RustTypes {
public static RustPath IO_ERROR = RustPath.global("std", "io", "Error");
public static RustPath IO_ERROR_KIND = RustPath.global("std", "io", "ErrorKind");
public static RustPath IO_ERROR_KIND_INVALID_DATA = IO_ERROR_KIND.addSegment(RustPathSegment.of("InvalidData"));
+ public static RustPath IO_READ = RustPath.global("std", "io", "Read");
public static RustPath IO_RESULT = RustPath.global("std", "io", "Result");
public static RustPath IO_RESULT_OK = IO_RESULT.addSegment(RustPathSegment.of("Ok"));
+ public static RustPath IO_WRITE = RustPath.global("std", "io", "Write");
public static RustPath ioResult(RustType okType) {
return IO_RESULT.withArgs(List.of(okType));