diff options
Diffstat (limited to 'src')
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)); |
