diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-05 18:44:26 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-05 18:44:26 +0100 |
| commit | 8455bb19b7d4235e667cab9112aa276557969dcc (patch) | |
| tree | 3ad5a7cf2941c4918f9e6de612a711cd8846570f /examples/15-transient-0/output/rust/src | |
| parent | 266e826749b62c46e14d9590351f8c3b2de53512 (diff) | |
Type check generic native types
Diffstat (limited to 'examples/15-transient-0/output/rust/src')
6 files changed, 1003 insertions, 0 deletions
diff --git a/examples/15-transient-0/output/rust/src/data.rs b/examples/15-transient-0/output/rust/src/data.rs new file mode 100644 index 0000000..7a0c4fa --- /dev/null +++ b/examples/15-transient-0/output/rust/src/data.rs @@ -0,0 +1,5 @@ +include!("./gen/data.rs"); + +pub mod transient_0 { + include!("./gen/data/transient_0.rs"); +} diff --git a/examples/15-transient-0/output/rust/src/gen/data.rs b/examples/15-transient-0/output/rust/src/gen/data.rs new file mode 100644 index 0000000..d4847ec --- /dev/null +++ b/examples/15-transient-0/output/rust/src/gen/data.rs @@ -0,0 +1,140 @@ +// Generated by hobgoblin. + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructSingleton; + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithBool { + pub a: ::core::primitive::bool, + pub b: ::core::primitive::bool, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithInt8 { + pub a: ::core::primitive::i8, + pub b: ::core::primitive::i8, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithInt32 { + pub a: ::core::primitive::i32, + pub b: ::core::primitive::i32, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithInt64 { + pub a: ::core::primitive::i64, + pub b: ::core::primitive::i64, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithString { + pub a: ::std::string::String, + pub b: ::std::string::String, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithStruct { + pub inner: crate::data::StructWithInt32, + pub c: ::core::primitive::i32, +} + +#[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(Clone, Debug, Hash, PartialEq)] +pub struct StructWithOption { + pub a: ::std::option::Option::<::core::primitive::i64>, + pub b: ::std::option::Option::<crate::data::StructWithInt32>, +} + +#[derive(Clone, Copy, Debug, Hash, PartialEq)] +pub enum EnumWithVariants { + Zero, + One, + Two, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithEnum { + pub inner: crate::data::EnumWithVariants, + pub c: ::core::primitive::i32, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub enum SumWithVariants { + VariantOne(crate::data::VariantOne), + VariantTwo(crate::data::VariantTwo), +} + +impl ::std::convert::From::<crate::data::VariantOne> for crate::data::SumWithVariants { + fn from(value: crate::data::VariantOne) -> Self { + Self::VariantOne(value) + } +} + +impl ::std::convert::From::<crate::data::VariantTwo> for crate::data::SumWithVariants { + fn from(value: crate::data::VariantTwo) -> Self { + Self::VariantTwo(value) + } +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct VariantOne { + pub a: ::core::primitive::i32, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct VariantTwo { + pub a: ::core::primitive::i64, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub enum SumWithBoxedVariants { + VariantOne(::std::boxed::Box::<crate::data::VariantOne>), + VariantTwo(::std::boxed::Box::<crate::data::VariantTwo>), +} + +impl ::std::convert::From::<crate::data::VariantOne> for crate::data::SumWithBoxedVariants { + fn from(value: crate::data::VariantOne) -> Self { + Self::VariantOne(::std::boxed::Box::new(value)) + } +} + +impl ::std::convert::From::<crate::data::VariantTwo> for crate::data::SumWithBoxedVariants { + fn from(value: crate::data::VariantTwo) -> Self { + Self::VariantTwo(::std::boxed::Box::new(value)) + } +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithSum { + pub inner: crate::data::SumWithVariants, + pub b: ::core::primitive::i32, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithBox { + pub inner: ::std::boxed::Box::<crate::data::StructWithInt32>, +} + +#[derive(Clone, Debug, Hash, PartialEq)] +pub struct StructWithListOfShared { + pub inner: ::std::vec::Vec::<::std::sync::Arc::<crate::data::StructWithInt32>>, +} + +#[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(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/examples/15-transient-0/output/rust/src/gen/data/transient_0.rs b/examples/15-transient-0/output/rust/src/gen/data/transient_0.rs new file mode 100644 index 0000000..f802163 --- /dev/null +++ b/examples/15-transient-0/output/rust/src/gen/data/transient_0.rs @@ -0,0 +1,455 @@ +// Generated by hobgoblin. + +pub fn encode_struct_singleton(value: &crate::data::StructSingleton, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_singleton(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::StructSingleton> { + ::std::io::Result::Ok(crate::data::StructSingleton) +} + +pub fn encode_struct_with_bool(value: &crate::data::StructWithBool, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::transient_0::encode_bool(&value.a, writer, shared_values)?; + crate::transient_0::encode_bool(&value.b, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +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 }) +} + +pub fn encode_struct_with_int_8(value: &crate::data::StructWithInt8, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::transient_0::encode_int_8(&value.a, writer, shared_values)?; + crate::transient_0::encode_int_8(&value.b, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_int_8(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::StructWithInt8> { + let a = crate::transient_0::decode_int_8(reader, shared_values)?; + let b = crate::transient_0::decode_int_8(reader, shared_values)?; + ::std::io::Result::Ok(crate::data::StructWithInt8 { 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::<()> { + crate::transient_0::encode_int_32(&value.a, writer, shared_values)?; + crate::transient_0::encode_int_32(&value.b, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +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 }) +} + +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::<()> { + crate::transient_0::encode_int_64(&value.a, writer, shared_values)?; + crate::transient_0::encode_int_64(&value.b, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +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 }) +} + +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::<()> { + crate::transient_0::encode_string(&value.a, writer, shared_values)?; + crate::transient_0::encode_string(&value.b, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +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 }) +} + +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::<()> { + crate::data::transient_0::encode_struct_with_int_32(&value.inner, writer, shared_values)?; + crate::transient_0::encode_int_32(&value.c, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +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 }) +} + +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::<()> { + { + crate::transient_0::encode_int_64(&(((&value.a).len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer, shared_values)?; + for element in &value.a { + crate::transient_0::encode_int_64(element, writer, shared_values)?; + }; + }; + { + crate::transient_0::encode_int_64(&(((&value.b).len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer, shared_values)?; + for element in &value.b { + crate::data::transient_0::encode_struct_with_int_32(element, writer, shared_values)?; + }; + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_list(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::StructWithList> { + let a = { + let len = crate::transient_0::decode_int_64(reader, shared_values)?; + let mut elements = ::std::vec::Vec::with_capacity((len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?); + for _ in 0..len { + elements.push(crate::transient_0::decode_int_64(reader, shared_values)?); + }; + elements + }; + let b = { + let len = crate::transient_0::decode_int_64(reader, shared_values)?; + let mut elements = ::std::vec::Vec::with_capacity((len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?); + for _ in 0..len { + elements.push(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); + }; + elements + }; + ::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::<()> { + match &value.a { + ::std::option::Option::Some(value) => { + crate::transient_0::encode_bool(&true, writer, shared_values)?; + crate::transient_0::encode_int_64(&value, writer, shared_values)?; + }, + ::std::option::Option::None => { + crate::transient_0::encode_bool(&false, writer, shared_values)?; + }, + }; + match &value.b { + ::std::option::Option::Some(value) => { + crate::transient_0::encode_bool(&true, writer, shared_values)?; + crate::data::transient_0::encode_struct_with_int_32(&value, writer, shared_values)?; + }, + ::std::option::Option::None => { + crate::transient_0::encode_bool(&false, writer, shared_values)?; + }, + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_option(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::StructWithOption> { + let a = { + let is_some = crate::transient_0::decode_bool(reader, shared_values)?; + if is_some { + ::std::option::Option::Some(crate::transient_0::decode_int_64(reader, shared_values)?) + } else { + ::std::option::Option::None + } + }; + let b = { + let is_some = crate::transient_0::decode_bool(reader, shared_values)?; + if is_some { + ::std::option::Option::Some(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?) + } else { + ::std::option::Option::None + } + }; + ::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::<()> { + crate::transient_0::encode_int_32(&match value { + crate::data::EnumWithVariants::Zero => 0, + crate::data::EnumWithVariants::One => 1, + crate::data::EnumWithVariants::Two => 2, + }, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +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)? { + 0 => crate::data::EnumWithVariants::Zero, + 1 => crate::data::EnumWithVariants::One, + 2 => crate::data::EnumWithVariants::Two, + _ => ::std::result::Result::Err(::std::io::Error::new(::std::io::ErrorKind::InvalidData, "unrecognised variant tag for data.EnumWithVariants"))?, + }) +} + +pub fn encode_struct_with_enum(value: &crate::data::StructWithEnum, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::data::transient_0::encode_enum_with_variants(&value.inner, writer, shared_values)?; + crate::transient_0::encode_int_32(&value.c, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +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 }) +} + +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::<()> { + match value { + crate::data::SumWithVariants::VariantOne(value) => { + crate::transient_0::encode_int_32(&0, writer, shared_values)?; + crate::data::transient_0::encode_variant_one(value, writer, shared_values)?; + }, + crate::data::SumWithVariants::VariantTwo(value) => { + crate::transient_0::encode_int_32(&1, writer, shared_values)?; + crate::data::transient_0::encode_variant_two(value, writer, shared_values)?; + }, + }; + ::std::io::Result::Ok(()) +} + +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)? { + 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)?), + _ => ::std::result::Result::Err(::std::io::Error::new(::std::io::ErrorKind::InvalidData, "unrecognised variant tag for data.SumWithVariants"))?, + }) +} + +pub fn encode_variant_one(value: &crate::data::VariantOne, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::transient_0::encode_int_32(&value.a, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +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 }) +} + +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::<()> { + crate::transient_0::encode_int_64(&value.a, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +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 }) +} + +pub fn encode_sum_with_boxed_variants(value: &crate::data::SumWithBoxedVariants, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + match value { + crate::data::SumWithBoxedVariants::VariantOne(value) => { + crate::transient_0::encode_int_32(&0, writer, shared_values)?; + crate::data::transient_0::encode_variant_one(value.as_ref(), writer, shared_values)?; + }, + crate::data::SumWithBoxedVariants::VariantTwo(value) => { + crate::transient_0::encode_int_32(&1, writer, shared_values)?; + crate::data::transient_0::encode_variant_two(value.as_ref(), writer, shared_values)?; + }, + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_sum_with_boxed_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::SumWithBoxedVariants> { + ::std::io::Result::Ok(match &crate::transient_0::decode_int_32(reader, shared_values)? { + 0 => crate::data::SumWithBoxedVariants::VariantOne(::std::boxed::Box::new(crate::data::transient_0::decode_variant_one(reader, shared_values)?)), + 1 => crate::data::SumWithBoxedVariants::VariantTwo(::std::boxed::Box::new(crate::data::transient_0::decode_variant_two(reader, shared_values)?)), + _ => ::std::result::Result::Err(::std::io::Error::new(::std::io::ErrorKind::InvalidData, "unrecognised variant tag for data.SumWithBoxedVariants"))?, + }) +} + +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::<()> { + crate::data::transient_0::encode_sum_with_variants(&value.inner, writer, shared_values)?; + crate::transient_0::encode_int_32(&value.b, writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +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 }) +} + +pub fn encode_struct_with_box(value: &crate::data::StructWithBox, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + crate::data::transient_0::encode_struct_with_int_32((&value.inner).as_ref(), writer, shared_values)?; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_box(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::StructWithBox> { + let inner = ::std::boxed::Box::new(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); + ::std::io::Result::Ok(crate::data::StructWithBox { inner: inner }) +} + +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::<()> { + { + crate::transient_0::encode_int_64(&(((&value.inner).len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer, shared_values)?; + for element in &value.inner { + { + let ptr = ::std::sync::Arc::as_ptr(&element) as ::core::primitive::usize; + match (shared_values.get(&ptr)).copied() { + ::std::option::Option::Some(id) => { + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + }, + ::std::option::Option::None => { + let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + shared_values.insert(ptr, id); + crate::data::transient_0::encode_struct_with_int_32(element.as_ref(), writer, shared_values)?; + }, + }; + }; + }; + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_list_of_shared(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::StructWithListOfShared> { + let inner = { + let len = crate::transient_0::decode_int_64(reader, shared_values)?; + let mut elements = ::std::vec::Vec::with_capacity((len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?); + for _ in 0..len { + elements.push({ + let id = crate::transient_0::decode_int_64(reader, shared_values)?; + if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { + ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? + } else { + let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); + shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>); + decoded_value + } + }); + }; + elements + }; + ::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::<()> { + { + let ptr = ::std::sync::Arc::as_ptr(&&value.a) as ::core::primitive::usize; + match (shared_values.get(&ptr)).copied() { + ::std::option::Option::Some(id) => { + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + }, + ::std::option::Option::None => { + let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + shared_values.insert(ptr, id); + crate::data::transient_0::encode_struct_with_int_32((&value.a).as_ref(), writer, shared_values)?; + }, + }; + }; + { + let ptr = ::std::sync::Arc::as_ptr(&&value.b) as ::core::primitive::usize; + match (shared_values.get(&ptr)).copied() { + ::std::option::Option::Some(id) => { + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + }, + ::std::option::Option::None => { + let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + shared_values.insert(ptr, id); + crate::data::transient_0::encode_struct_with_int_64((&value.b).as_ref(), writer, shared_values)?; + }, + }; + }; + { + let ptr = ::std::sync::Arc::as_ptr(&&value.c) as ::core::primitive::usize; + match (shared_values.get(&ptr)).copied() { + ::std::option::Option::Some(id) => { + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + }, + ::std::option::Option::None => { + let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + shared_values.insert(ptr, id); + crate::data::transient_0::encode_struct_with_int_32((&value.c).as_ref(), writer, shared_values)?; + }, + }; + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_different_shared_types(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::StructWithDifferentSharedTypes> { + let a = { + let id = crate::transient_0::decode_int_64(reader, shared_values)?; + if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { + ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? + } else { + let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); + shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>); + decoded_value + } + }; + let b = { + let id = crate::transient_0::decode_int_64(reader, shared_values)?; + if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { + ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? + } else { + let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_struct_with_int_64(reader, shared_values)?); + shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>); + decoded_value + } + }; + let c = { + let id = crate::transient_0::decode_int_64(reader, shared_values)?; + if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { + ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? + } else { + let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_struct_with_int_32(reader, shared_values)?); + shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>); + decoded_value + } + }; + ::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::<()> { + { + let ptr = ::std::sync::Arc::as_ptr(&&value.a) as ::core::primitive::usize; + match (shared_values.get(&ptr)).copied() { + ::std::option::Option::Some(id) => { + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + }, + ::std::option::Option::None => { + let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + shared_values.insert(ptr, id); + crate::data::transient_0::encode_sum_with_variants((&value.a).as_ref(), writer, shared_values)?; + }, + }; + }; + { + let ptr = ::std::sync::Arc::as_ptr(&&value.b) as ::core::primitive::usize; + match (shared_values.get(&ptr)).copied() { + ::std::option::Option::Some(id) => { + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + }, + ::std::option::Option::None => { + let id = ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?; + crate::transient_0::encode_int_64(&id, writer, shared_values)?; + shared_values.insert(ptr, id); + crate::data::transient_0::encode_variant_one((&value.b).as_ref(), writer, shared_values)?; + }, + }; + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_struct_with_shared_sum_and_variant(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::StructWithSharedSumAndVariant> { + let a = { + let id = crate::transient_0::decode_int_64(reader, shared_values)?; + if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { + ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? + } else { + let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_sum_with_variants(reader, shared_values)?); + shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>); + decoded_value + } + }; + let b = { + let id = crate::transient_0::decode_int_64(reader, shared_values)?; + if id < ((shared_values.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))? { + ((::std::sync::Arc::clone(&shared_values[(::core::primitive::usize::try_from(id)).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?])).downcast()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, "referenced value has wrong type"))? + } else { + let decoded_value = ::std::sync::Arc::new(crate::data::transient_0::decode_variant_one(reader, shared_values)?); + shared_values.push(::std::sync::Arc::clone(&decoded_value) as ::std::sync::Arc::<dyn ::std::any::Any + ::std::marker::Sync + ::std::marker::Send>); + decoded_value + } + }; + ::std::io::Result::Ok(crate::data::StructWithSharedSumAndVariant { a: a, b: b }) +} diff --git a/examples/15-transient-0/output/rust/src/gen/transient_0.rs b/examples/15-transient-0/output/rust/src/gen/transient_0.rs new file mode 100644 index 0000000..6505408 --- /dev/null +++ b/examples/15-transient-0/output/rust/src/gen/transient_0.rs @@ -0,0 +1,84 @@ +// Generated by hobgoblin. + +pub fn encode_bool(value: &::core::primitive::bool, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + if *value { + writer.write_all(&1u8.to_le_bytes())?; + } else { + writer.write_all(&0u8.to_le_bytes())?; + }; + ::std::io::Result::Ok(()) +} + +pub fn decode_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::<::core::primitive::bool> { + let int_value = { + let bytes = { + let mut bytes = [0; 1]; + reader.read_exact(&mut bytes)?; + bytes + }; + u8::from_le_bytes(bytes) + }; + ::std::io::Result::Ok(if int_value == 0 { + false + } else { + true + }) +} + +pub fn encode_int_8(value: &::core::primitive::i8, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + writer.write_all(&value.to_le_bytes())?; + ::std::io::Result::Ok(()) +} + +pub fn decode_int_8(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::<::core::primitive::i8> { + let bytes = { + let mut bytes = [0; 1]; + reader.read_exact(&mut bytes)?; + bytes + }; + ::std::io::Result::Ok(::core::primitive::i8::from_le_bytes(bytes)) +} + +pub fn encode_int_32(value: &::core::primitive::i32, writer: &mut impl std::io::Write, shared_values: &mut ::std::collections::HashMap::<::core::primitive::usize, ::core::primitive::i64>) -> ::std::io::Result::<()> { + writer.write_all(&value.to_le_bytes())?; + ::std::io::Result::Ok(()) +} + +pub fn decode_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::<::core::primitive::i32> { + let bytes = { + let mut bytes = [0; 4]; + reader.read_exact(&mut 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::<()> { + writer.write_all(&value.to_le_bytes())?; + ::std::io::Result::Ok(()) +} + +pub fn decode_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::<::core::primitive::i64> { + let bytes = { + let mut bytes = [0; 8]; + reader.read_exact(&mut 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::<()> { + crate::transient_0::encode_int_64(&((value.len()).try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?, writer, shared_values)?; + writer.write_all(value.as_bytes())?; + ::std::io::Result::Ok(()) +} + +pub fn decode_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::<::std::string::String> { + let len = crate::transient_0::decode_int_64(reader, shared_values)?; + let bytes = { + let mut bytes = vec![0u8; (len.try_into()).map_err(|error| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, error))?]; + 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))?) +} diff --git a/examples/15-transient-0/output/rust/src/lib.rs b/examples/15-transient-0/output/rust/src/lib.rs new file mode 100644 index 0000000..f0f6e24 --- /dev/null +++ b/examples/15-transient-0/output/rust/src/lib.rs @@ -0,0 +1,318 @@ +pub mod data; +pub mod transient_0; + +#[cfg(test)] +mod test { + use std::any::Any; + use std::io::Cursor; + use std::path::PathBuf; + use std::sync::Arc; + use std::collections::HashMap; + use super::data::{EnumWithVariants, StructSingleton, StructWithBool, StructWithBox, StructWithDifferentSharedTypes, StructWithEnum, StructWithInt8, StructWithInt32, StructWithInt64, StructWithList, StructWithListOfShared, StructWithOption, StructWithSharedSumAndVariant, StructWithString, StructWithStruct, StructWithSum, SumWithBoxedVariants, SumWithVariants, VariantOne, VariantTwo }; + + #[test] + fn struct_singleton() { + let value = StructSingleton; + + assert_round_trip_encoding( + "StructSingleton", + value, + super::data::transient_0::encode_struct_singleton, + super::data::transient_0::decode_struct_singleton, + ); + } + + #[test] + fn struct_with_bool() { + let value = StructWithBool { a: true, b: false }; + + assert_round_trip_encoding( + "StructWithBool", + value, + super::data::transient_0::encode_struct_with_bool, + super::data::transient_0::decode_struct_with_bool, + ); + } + + #[test] + fn struct_with_int_8() { + let value = StructWithInt8 { a: 10, b: 25 }; + + assert_round_trip_encoding( + "StructWithInt8", + value, + super::data::transient_0::encode_struct_with_int_8, + super::data::transient_0::decode_struct_with_int_8, + ); + } + + #[test] + fn struct_with_int_32() { + let value = StructWithInt32 { a: 10, b: 25 }; + + assert_round_trip_encoding( + "StructWithInt32", + value, + super::data::transient_0::encode_struct_with_int_32, + super::data::transient_0::decode_struct_with_int_32, + ); + } + + #[test] + fn struct_with_int_64() { + let value = StructWithInt64 { a: 10, b: 25 }; + + assert_round_trip_encoding( + "StructWithInt64", + value, + super::data::transient_0::encode_struct_with_int_64, + super::data::transient_0::decode_struct_with_int_64, + ); + } + + #[test] + fn struct_with_string() { + let value = StructWithString { a: "abc".to_string(), b: "def".to_string() }; + + assert_round_trip_encoding( + "StructWithString", + value, + super::data::transient_0::encode_struct_with_string, + super::data::transient_0::decode_struct_with_string, + ); + } + + #[test] + fn struct_with_struct() { + let value = StructWithStruct { inner: StructWithInt32 { a: 10, b: 25 }, c: 42 }; + + assert_round_trip_encoding( + "StructWithStruct", + value, + super::data::transient_0::encode_struct_with_struct, + super::data::transient_0::decode_struct_with_struct, + ); + } + + #[test] + fn struct_with_list() { + let value = StructWithList { + a: vec![10, 25], + b: vec![ + StructWithInt32 { a: 42, b: 47 }, + StructWithInt32 { a: 52, b: 57 }, + StructWithInt32 { a: 62, b: 67 }, + ], + }; + + assert_round_trip_encoding( + "StructWithList", + value, + super::data::transient_0::encode_struct_with_list, + super::data::transient_0::decode_struct_with_list, + ); + } + + #[test] + fn struct_with_option_none() { + let value = StructWithOption { + a: None, + b: None, + }; + + assert_round_trip_encoding( + "StructWithOption_None", + value, + super::data::transient_0::encode_struct_with_option, + super::data::transient_0::decode_struct_with_option, + ); + } + + #[test] + fn struct_with_option_some() { + let value = StructWithOption { + a: Some(10), + b: Some(StructWithInt32 { a: 42, b: 47 }), + }; + + assert_round_trip_encoding( + "StructWithOption_Some", + value, + super::data::transient_0::encode_struct_with_option, + super::data::transient_0::decode_struct_with_option, + ); + } + + #[test] + fn enum_with_variants() { + assert_round_trip_encoding( + "EnumWithVariants_zero", + EnumWithVariants::Zero, + super::data::transient_0::encode_enum_with_variants, + super::data::transient_0::decode_enum_with_variants, + ); + + assert_round_trip_encoding( + "EnumWithVariants_one", + EnumWithVariants::One, + super::data::transient_0::encode_enum_with_variants, + super::data::transient_0::decode_enum_with_variants, + ); + + assert_round_trip_encoding( + "EnumWithVariants_two", + EnumWithVariants::Two, + super::data::transient_0::encode_enum_with_variants, + super::data::transient_0::decode_enum_with_variants, + ); + } + + #[test] + fn struct_with_enum() { + let value = StructWithEnum { inner: EnumWithVariants::One, c: 42 }; + + assert_round_trip_encoding( + "StructWithEnum", + value, + super::data::transient_0::encode_struct_with_enum, + super::data::transient_0::decode_struct_with_enum, + ); + } + + #[test] + fn sum_with_variants() { + assert_round_trip_encoding( + "SumWithVariants_VariantOne", + SumWithVariants::VariantOne(VariantOne { a: 10 }), + super::data::transient_0::encode_sum_with_variants, + super::data::transient_0::decode_sum_with_variants, + ); + + assert_round_trip_encoding( + "SumWithVariants_VariantTwo", + SumWithVariants::VariantTwo(VariantTwo { a: 25 }), + super::data::transient_0::encode_sum_with_variants, + super::data::transient_0::decode_sum_with_variants, + ); + } + + #[test] + fn sum_with_boxed_variants() { + assert_round_trip_encoding( + "SumWithBoxedVariants_VariantOne", + SumWithBoxedVariants::VariantOne(Box::new(VariantOne { a: 10 })), + super::data::transient_0::encode_sum_with_boxed_variants, + super::data::transient_0::decode_sum_with_boxed_variants, + ); + + assert_round_trip_encoding( + "SumWithBoxedVariants_VariantTwo", + SumWithBoxedVariants::VariantTwo(Box::new(VariantTwo { a: 25 })), + super::data::transient_0::encode_sum_with_boxed_variants, + super::data::transient_0::decode_sum_with_boxed_variants, + ); + } + + #[test] + fn struct_with_sum() { + let value = StructWithSum { + inner: SumWithVariants::VariantTwo(VariantTwo { a: 25 }), + b: 42, + }; + + assert_round_trip_encoding( + "StructWithSum", + value, + super::data::transient_0::encode_struct_with_sum, + super::data::transient_0::decode_struct_with_sum, + ); + } + + #[test] + fn struct_with_box() { + let value = StructWithBox { + inner: Box::new(StructWithInt32 { a: 10, b: 25 }), + }; + + assert_round_trip_encoding( + "StructWithBox", + value, + super::data::transient_0::encode_struct_with_box, + super::data::transient_0::decode_struct_with_box, + ); + } + + #[test] + fn struct_with_list_of_shared() { + let a = Arc::new(StructWithInt32 { a: 10, b: 25 }); + let b = Arc::new(StructWithInt32 { a: 42, b: 47 }); + + let value = StructWithListOfShared { + inner: vec![Arc::clone(&a), Arc::clone(&a), Arc::clone(&b), a, b], + }; + + let decoded_value = assert_round_trip_encoding( + "StructWithListOfShared", + value, + super::data::transient_0::encode_struct_with_list_of_shared, + super::data::transient_0::decode_struct_with_list_of_shared, + ); + assert!(Arc::ptr_eq(&decoded_value.inner[0], &decoded_value.inner[1])); + assert!(Arc::ptr_eq(&decoded_value.inner[0], &decoded_value.inner[3])); + assert!(Arc::ptr_eq(&decoded_value.inner[2], &decoded_value.inner[4])); + } + + #[test] + fn struct_with_different_shared_types() { + let a = Arc::new(StructWithInt32 { a: 10, b: 25 }); + let b = Arc::new(StructWithInt64 { a: 42, b: 47 }); + let c = Arc::new(StructWithInt32 { a: 52, b: 57 }); + + let value = StructWithDifferentSharedTypes { a, b, c }; + + assert_round_trip_encoding( + "StructWithDifferentSharedTypes", + value, + super::data::transient_0::encode_struct_with_different_shared_types, + super::data::transient_0::decode_struct_with_different_shared_types, + ); + } + + #[test] + fn struct_with_shared_sum_and_variant() { + let a = Arc::new(SumWithVariants::VariantOne(VariantOne { a: 10 })); + let b = Arc::new(VariantOne { a: 10 }); + + let value = StructWithSharedSumAndVariant { a, b }; + + assert_round_trip_encoding( + "StructWithSharedSumAndVariant", + value, + super::data::transient_0::encode_struct_with_shared_sum_and_variant, + super::data::transient_0::decode_struct_with_shared_sum_and_variant, + ); + } + + fn assert_round_trip_encoding<T: std::cmp::PartialEq + std::fmt::Debug>( + name: &str, + value: T, + encode: impl Fn(&T, &mut Cursor<Vec<u8>>, &mut HashMap<usize, i64>) -> std::io::Result<()>, + decode: impl Fn(&mut Cursor<Vec<u8>>, &mut Vec<Arc<dyn Any + Sync + Send>>) -> std::io::Result<T>, + ) -> T { + let mut bytes = Cursor::new(Vec::new()); + encode(&value, &mut bytes, &mut HashMap::new()).unwrap(); + + let output_dir = std::env::var("HOBGOBLIN_OUTPUT_DIR").unwrap(); + let transient_0_output_dir = PathBuf::from(output_dir).join("transient-0"); + std::fs::create_dir_all(&transient_0_output_dir).unwrap(); + let transient_0_output_path = transient_0_output_dir.join(name); + std::fs::write(transient_0_output_path, bytes.get_ref()).unwrap(); + + bytes.set_position(0); + let decoded_value = decode(&mut bytes, &mut Vec::new()).unwrap(); + + assert_eq!(value, decoded_value); + + decoded_value + } +} diff --git a/examples/15-transient-0/output/rust/src/transient_0.rs b/examples/15-transient-0/output/rust/src/transient_0.rs new file mode 100644 index 0000000..8b2b800 --- /dev/null +++ b/examples/15-transient-0/output/rust/src/transient_0.rs @@ -0,0 +1 @@ +include!("./gen/transient_0.rs"); |
