diff options
4 files changed, 34 insertions, 28 deletions
diff --git a/examples/11-transient-0/output/rust/src/gen/data/transient_0.rs b/examples/11-transient-0/output/rust/src/gen/data/transient_0.rs index 1a6a009..ea4a459 100644 --- a/examples/11-transient-0/output/rust/src/gen/data/transient_0.rs +++ b/examples/11-transient-0/output/rust/src/gen/data/transient_0.rs @@ -9,7 +9,7 @@ pub fn encode_struct_with_bool(value: &crate::data::StructWithBool, writer: &mut pub fn decode_struct_with_bool(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>>) -> ::std::io::Result::<crate::data::StructWithBool> { let a = crate::transient_0::decode_bool(reader, shared_values)?; let b = crate::transient_0::decode_bool(reader, shared_values)?; - std::io::Result::Ok(crate::data::StructWithBool { a: a, b: b }) + ::std::io::Result::Ok(crate::data::StructWithBool { a: a, b: b }) } pub fn encode_struct_with_int_32(value: &crate::data::StructWithInt32, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -21,7 +21,7 @@ pub fn encode_struct_with_int_32(value: &crate::data::StructWithInt32, writer: & pub fn decode_struct_with_int_32(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>>) -> ::std::io::Result::<crate::data::StructWithInt32> { let a = crate::transient_0::decode_int_32(reader, shared_values)?; let b = crate::transient_0::decode_int_32(reader, shared_values)?; - std::io::Result::Ok(crate::data::StructWithInt32 { a: a, b: b }) + ::std::io::Result::Ok(crate::data::StructWithInt32 { a: a, b: b }) } pub fn encode_struct_with_int_64(value: &crate::data::StructWithInt64, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -33,7 +33,7 @@ pub fn encode_struct_with_int_64(value: &crate::data::StructWithInt64, writer: & pub fn decode_struct_with_int_64(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>>) -> ::std::io::Result::<crate::data::StructWithInt64> { let a = crate::transient_0::decode_int_64(reader, shared_values)?; let b = crate::transient_0::decode_int_64(reader, shared_values)?; - std::io::Result::Ok(crate::data::StructWithInt64 { a: a, b: b }) + ::std::io::Result::Ok(crate::data::StructWithInt64 { a: a, b: b }) } pub fn encode_struct_with_string(value: &crate::data::StructWithString, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -45,7 +45,7 @@ pub fn encode_struct_with_string(value: &crate::data::StructWithString, writer: pub fn decode_struct_with_string(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>>) -> ::std::io::Result::<crate::data::StructWithString> { let a = crate::transient_0::decode_string(reader, shared_values)?; let b = crate::transient_0::decode_string(reader, shared_values)?; - std::io::Result::Ok(crate::data::StructWithString { a: a, b: b }) + ::std::io::Result::Ok(crate::data::StructWithString { a: a, b: b }) } pub fn encode_struct_with_struct(value: &crate::data::StructWithStruct, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -57,7 +57,7 @@ pub fn encode_struct_with_struct(value: &crate::data::StructWithStruct, writer: pub fn decode_struct_with_struct(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>>) -> ::std::io::Result::<crate::data::StructWithStruct> { let inner = crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?; let c = crate::transient_0::decode_int_32(reader, shared_values)?; - std::io::Result::Ok(crate::data::StructWithStruct { inner: inner, c: c }) + ::std::io::Result::Ok(crate::data::StructWithStruct { inner: inner, c: c }) } pub fn encode_struct_with_list(value: &crate::data::StructWithList, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -93,7 +93,7 @@ pub fn decode_struct_with_list(reader: &mut impl std::io::Read, shared_values: & }; elements }; - std::io::Result::Ok(crate::data::StructWithList { a: a, b: b }) + ::std::io::Result::Ok(crate::data::StructWithList { a: a, b: b }) } pub fn encode_struct_with_option(value: &crate::data::StructWithOption, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -135,7 +135,7 @@ pub fn decode_struct_with_option(reader: &mut impl std::io::Read, shared_values: ::std::option::Option::None } }; - std::io::Result::Ok(crate::data::StructWithOption { a: a, b: b }) + ::std::io::Result::Ok(crate::data::StructWithOption { a: a, b: b }) } pub fn encode_enum_with_variants(value: &crate::data::EnumWithVariants, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -148,7 +148,7 @@ pub fn encode_enum_with_variants(value: &crate::data::EnumWithVariants, writer: } pub fn decode_enum_with_variants(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>>) -> ::std::io::Result::<crate::data::EnumWithVariants> { - std::io::Result::Ok(match &crate::transient_0::decode_int_32(reader, shared_values)? { + ::std::io::Result::Ok(match &crate::transient_0::decode_int_32(reader, shared_values)? { 0 => crate::data::EnumWithVariants::Zero, 1 => crate::data::EnumWithVariants::One, 2 => crate::data::EnumWithVariants::Two, @@ -165,7 +165,7 @@ pub fn encode_struct_with_enum(value: &crate::data::StructWithEnum, writer: &mut pub fn decode_struct_with_enum(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>>) -> ::std::io::Result::<crate::data::StructWithEnum> { let inner = crate::data::transient_0::decode_enum_with_variants(reader, shared_values)?; let c = crate::transient_0::decode_int_32(reader, shared_values)?; - std::io::Result::Ok(crate::data::StructWithEnum { inner: inner, c: c }) + ::std::io::Result::Ok(crate::data::StructWithEnum { inner: inner, c: c }) } pub fn encode_sum_with_variants(value: &crate::data::SumWithVariants, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -183,7 +183,7 @@ pub fn encode_sum_with_variants(value: &crate::data::SumWithVariants, writer: &m } pub fn decode_sum_with_variants(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>>) -> ::std::io::Result::<crate::data::SumWithVariants> { - std::io::Result::Ok(match &crate::transient_0::decode_int_32(reader, shared_values)? { + ::std::io::Result::Ok(match &crate::transient_0::decode_int_32(reader, shared_values)? { 0 => crate::data::SumWithVariants::VariantOne(crate::data::transient_0::decode_variant_one(reader, shared_values)?), 1 => crate::data::SumWithVariants::VariantTwo(crate::data::transient_0::decode_variant_two(reader, shared_values)?), _ => todo!(), @@ -197,7 +197,7 @@ pub fn encode_variant_one(value: &crate::data::VariantOne, writer: &mut impl std pub fn decode_variant_one(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>>) -> ::std::io::Result::<crate::data::VariantOne> { let a = crate::transient_0::decode_int_32(reader, shared_values)?; - std::io::Result::Ok(crate::data::VariantOne { a: a }) + ::std::io::Result::Ok(crate::data::VariantOne { a: a }) } pub fn encode_variant_two(value: &crate::data::VariantTwo, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -207,7 +207,7 @@ pub fn encode_variant_two(value: &crate::data::VariantTwo, writer: &mut impl std pub fn decode_variant_two(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>>) -> ::std::io::Result::<crate::data::VariantTwo> { let a = crate::transient_0::decode_int_64(reader, shared_values)?; - std::io::Result::Ok(crate::data::VariantTwo { a: a }) + ::std::io::Result::Ok(crate::data::VariantTwo { a: a }) } pub fn encode_struct_with_sum(value: &crate::data::StructWithSum, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -219,7 +219,7 @@ pub fn encode_struct_with_sum(value: &crate::data::StructWithSum, writer: &mut i pub fn decode_struct_with_sum(reader: &mut impl std::io::Read, shared_values: &mut ::std::vec::Vec::<::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>>) -> ::std::io::Result::<crate::data::StructWithSum> { let inner = crate::data::transient_0::decode_sum_with_variants(reader, shared_values)?; let b = crate::transient_0::decode_int_32(reader, shared_values)?; - std::io::Result::Ok(crate::data::StructWithSum { inner: inner, b: b }) + ::std::io::Result::Ok(crate::data::StructWithSum { inner: inner, b: b }) } pub fn encode_struct_with_list_of_shared(value: &crate::data::StructWithListOfShared, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -263,7 +263,7 @@ pub fn decode_struct_with_list_of_shared(reader: &mut impl std::io::Read, shared }; elements }; - std::io::Result::Ok(crate::data::StructWithListOfShared { inner: inner }) + ::std::io::Result::Ok(crate::data::StructWithListOfShared { inner: inner }) } pub fn encode_struct_with_different_shared_types(value: &crate::data::StructWithDifferentSharedTypes, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -343,7 +343,7 @@ pub fn decode_struct_with_different_shared_types(reader: &mut impl std::io::Read decoded_value } }; - std::io::Result::Ok(crate::data::StructWithDifferentSharedTypes { a: a, b: b, c: c }) + ::std::io::Result::Ok(crate::data::StructWithDifferentSharedTypes { a: a, b: b, c: c }) } pub fn encode_struct_with_shared_sum_and_variant(value: &crate::data::StructWithSharedSumAndVariant, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -399,5 +399,5 @@ pub fn decode_struct_with_shared_sum_and_variant(reader: &mut impl std::io::Read decoded_value } }; - std::io::Result::Ok(crate::data::StructWithSharedSumAndVariant { a: a, b: b }) + ::std::io::Result::Ok(crate::data::StructWithSharedSumAndVariant { a: a, b: b }) } diff --git a/examples/11-transient-0/output/rust/src/gen/transient_0.rs b/examples/11-transient-0/output/rust/src/gen/transient_0.rs index 393b397..d88284e 100644 --- a/examples/11-transient-0/output/rust/src/gen/transient_0.rs +++ b/examples/11-transient-0/output/rust/src/gen/transient_0.rs @@ -18,7 +18,7 @@ pub fn decode_bool(reader: &mut impl std::io::Read, shared_values: &mut ::std::v }; u8::from_le_bytes(bytes) }; - std::io::Result::Ok(if int_value == 0 { + ::std::io::Result::Ok(if int_value == 0 { false } else { true @@ -36,7 +36,7 @@ pub fn decode_int_32(reader: &mut impl std::io::Read, shared_values: &mut ::std: reader.read_exact(&mut bytes)?; bytes }; - std::io::Result::Ok(::core::primitive::i32::from_le_bytes(bytes)) + ::std::io::Result::Ok(::core::primitive::i32::from_le_bytes(bytes)) } pub fn encode_int_64(value: &::core::primitive::i64, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -50,7 +50,7 @@ pub fn decode_int_64(reader: &mut impl std::io::Read, shared_values: &mut ::std: reader.read_exact(&mut bytes)?; bytes }; - std::io::Result::Ok(::core::primitive::i64::from_le_bytes(bytes)) + ::std::io::Result::Ok(::core::primitive::i64::from_le_bytes(bytes)) } pub fn encode_string(value: &::std::string::String, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { @@ -66,5 +66,5 @@ pub fn decode_string(reader: &mut impl std::io::Read, shared_values: &mut ::std: reader.read_exact(&mut bytes)?; bytes }; - std::io::Result::Ok((::std::string::String::from_utf8(bytes)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?) + ::std::io::Result::Ok((::std::string::String::from_utf8(bytes)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?) } 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 734e7f7..3c8000c 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 @@ -518,14 +518,13 @@ public class RustTransient0Generator implements Generator { ) ), Optional.of( - RustPath.global("std", "io", "Result") - .withArgs(List.of(new RustTupleType(List.of()))) + RustTypes.ioResult(RustTypes.UNIT) ), Optional.of(new RustBlockExpression( body, Optional.of( new RustCallExpression( - RustPath.global("std", "io", "Result", "Ok"), + RustTypes.IO_RESULT_OK, List.of(new RustTupleExpression(List.of())) ) ) @@ -548,10 +547,10 @@ public class RustTransient0Generator implements Generator { RustTypes.vec(RustTypes.arc(RustTypes.dyn(RustTypes.ANY, RustTypes.SYNC, RustTypes.SEND))) )) ), - Optional.of(RustPath.global("std", "io", "Result").withArgs(List.of(rustType))), + Optional.of(RustTypes.ioResult(rustType)), Optional.of(new RustBlockExpression( body.statements(), - body.finalOperand().map(finalOperand -> new RustCallExpression(RustPath.of("std", "io", "Result", "Ok"), List.of(finalOperand))) + body.finalOperand().map(finalOperand -> new RustCallExpression(RustTypes.IO_RESULT_OK, List.of(finalOperand))) )) ); } 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 1e43a24..c0ee970 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 @@ -1,8 +1,6 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust; -import org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPath; -import org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTraitObjectType; -import org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType; +import org.zwobble.hobgoblin.compiler.output.lang.rust.ast.*; import java.util.Arrays; import java.util.List; @@ -19,6 +17,8 @@ public class RustTypes { public static RustPath I64 = RustPath.primitive("i64"); public static RustPath USIZE = RustPath.primitive("usize"); + public static RustType UNIT = new RustTupleType(List.of()); + public static RustPath ANY = RustPath.global("std", "any", "Any"); public static RustPath box(RustType elementType) { @@ -31,6 +31,13 @@ public class RustTypes { .withArgs(List.of(keyType, valueType)); } + 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 ioResult(RustType okType) { + return IO_RESULT.withArgs(List.of(okType)); + } + public static RustPath SEND = RustPath.global("std", "marker", "Send"); public static RustPath SYNC = RustPath.global("std", "marker", "Sync"); |
