From 2c062f0807dab88d6961dee14a17a1bbd3bb5480 Mon Sep 17 00:00:00 2001 From: Michael Ward Date: Sun, 19 Oct 2025 11:39:50 -0500 Subject: [PATCH 1/5] [Rust] updated how Cargo.toml package name is cleaned --- .../uk/co/real_logic/sbe/generation/rust/RustGenerator.java | 2 +- .../uk/co/real_logic/sbe/generation/rust/RustOutputManager.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java index 746f18d022..61f600a7fb 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java @@ -111,7 +111,7 @@ public void generate() throws IOException // create Cargo.toml try (Writer writer = outputManager.createCargoToml()) { - final String packageName = toLowerSnakeCase(ir.packageName()).replaceAll("[.-]", "_"); + final String packageName = toLowerSnakeCase(ir.packageName().replaceAll("[\\s.-]", "_")); final String namespace; if (ir.namespaceName() == null || ir.namespaceName().equalsIgnoreCase(packageName)) { diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustOutputManager.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustOutputManager.java index fe8ca08e63..26ee4ca98f 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustOutputManager.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustOutputManager.java @@ -60,7 +60,7 @@ public RustOutputManager(final String baseDirName, final String packageName) Verify.notNull(packageName, "packageName"); String dirName = baseDirName.endsWith("" + separatorChar) ? baseDirName : baseDirName + separatorChar; - dirName += packageName.replaceAll("\\.", "_").toLowerCase() + separatorChar; + dirName += packageName.replaceAll("[\\s.-]", "_").toLowerCase() + separatorChar; final String libDirName = dirName; rootDir = new File(libDirName); From 1295e4122deb9ff888f361ac30d9ad81e5b03ebc Mon Sep 17 00:00:00 2001 From: Michael Ward Date: Tue, 21 Oct 2025 06:02:23 -0500 Subject: [PATCH 2/5] [Rust] fixed RustGenerator::generateEncoderVarData() to cap slice len to primitive type size --- build.gradle | 3 +- rust/Cargo.toml | 5 ++- .../basic_variable_length_schema_test.rs | 43 +++++++++++++++++++ .../sbe/generation/rust/RustGenerator.java | 5 ++- 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 rust/tests/basic_variable_length_schema_test.rs diff --git a/build.gradle b/build.gradle index 90102a340e..3d6461d6e7 100644 --- a/build.gradle +++ b/build.gradle @@ -679,8 +679,9 @@ tasks.register('generateRustTestCodecs', JavaExec) { 'sbe-tool/src/test/resources/issue1028.xml', 'sbe-tool/src/test/resources/issue1057.xml', 'sbe-tool/src/test/resources/issue1066.xml', - 'sbe-tool/src/test/resources/fixed-sized-primitive-array-types.xml', + 'sbe-tool/src/test/resources/basic-variable-length-schema.xml', 'sbe-tool/src/test/resources/example-bigendian-test-schema.xml', + 'sbe-tool/src/test/resources/fixed-sized-primitive-array-types.xml', 'sbe-tool/src/test/resources/nested-composite-name.xml', ] } diff --git a/rust/Cargo.toml b/rust/Cargo.toml index b48fd6ee55..f54de60807 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -2,7 +2,7 @@ name = "sbe_rust_example" version = "0.1.0" authors = [] -edition = "2018" +edition = "2021" publish = false [dependencies] @@ -21,10 +21,11 @@ issue_1057 = { path = "../generated/rust/issue1057" } issue_1066 = { path = "../generated/rust/issue1066" } baseline_bigendian = { path = "../generated/rust/baseline-bigendian" } nested_composite_name = { path = "../generated/rust/nested-composite-name" } +sbe_tests = { path = "../generated/rust/sbe_tests" } fixed_sized_primitive_array = { path = "../generated/rust/fixed_sized_primitive_array" } [dev-dependencies] -criterion = "0.5" +criterion = "0.7" [[bench]] name = "car_benchmark" diff --git a/rust/tests/basic_variable_length_schema_test.rs b/rust/tests/basic_variable_length_schema_test.rs new file mode 100644 index 0000000000..c8b4eed86f --- /dev/null +++ b/rust/tests/basic_variable_length_schema_test.rs @@ -0,0 +1,43 @@ +use sbe_tests::{ + message_header_codec::MessageHeaderDecoder, + test_message_1_codec::{ + TestMessage1Decoder, SBE_BLOCK_LENGTH, SBE_SCHEMA_ID, SBE_SCHEMA_VERSION, SBE_TEMPLATE_ID, + }, +}; +use sbe_tests::{ + WriteBuf, + {message_header_codec, test_message_1_codec::TestMessage1Encoder, Encoder, ReadBuf}, +}; + +#[test] +fn should_limit_var_data_length() { + // encode... + let mut buffer = vec![0u8; 1024]; + let mut encoder = TestMessage1Encoder::default(); + encoder = encoder.wrap( + WriteBuf::new(buffer.as_mut_slice()), + message_header_codec::ENCODED_LENGTH, + ); + encoder = encoder.header(0).parent().unwrap(); + + let password: String = (0..1024).map(|_| 'x' as char).collect(); + encoder.encrypted_new_password(password.as_bytes()); + assert_eq!(263, encoder.get_limit()); + + // decode... + let buf = ReadBuf::new(buffer.as_slice()); + let header = MessageHeaderDecoder::default().wrap(buf, 0); + assert_eq!(SBE_BLOCK_LENGTH, header.block_length()); + assert_eq!(SBE_SCHEMA_VERSION, header.version()); + assert_eq!(SBE_TEMPLATE_ID, header.template_id()); + assert_eq!(SBE_SCHEMA_ID, header.schema_id()); + + let mut decoder = TestMessage1Decoder::default().header(header, 0); + let coord = decoder.encrypted_new_password_decoder(); + let password = String::from_utf8_lossy(decoder.encrypted_new_password_slice(coord)); + assert_eq!(254, password.len()); + password + .as_bytes() + .iter() + .for_each(|x| assert_eq!(b'x', *x)); +} diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java index 61f600a7fb..94491b0a15 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java @@ -356,14 +356,15 @@ static void generateEncoderVarData( indent(sb, level, "pub fn %s(&mut self, value: %s) {\n", propertyName, varDataType); indent(sb, level + 1, "let limit = self.get_limit();\n"); - indent(sb, level + 1, "let data_length = value.len();\n"); + indent(sb, level + 1, "let data_length = value.len().min((%s::MAX - 1) as usize);\n", + rustTypeName(lengthType)); indent(sb, level + 1, "self.set_limit(limit + %d + data_length);\n", lengthType.size()); indent(sb, level + 1, "self.get_buf_mut().put_%s_at(limit, data_length as %1$s);\n", rustTypeName(lengthType)); - indent(sb, level + 1, "self.get_buf_mut().put_slice_at(limit + %d, value%s);\n", + indent(sb, level + 1, "self.get_buf_mut().put_slice_at(limit + %d, &value[0..data_length]%s);\n", lengthType.size(), toBytesFn); From c0b9dc1dc401944c91c82c56a9a2dd598906f41c Mon Sep 17 00:00:00 2001 From: Michael Ward Date: Tue, 21 Oct 2025 06:17:46 -0500 Subject: [PATCH 3/5] [Rust] updated test imports --- rust/tests/issue_1066_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/tests/issue_1066_test.rs b/rust/tests/issue_1066_test.rs index 43f7278ebf..2c22c0f44b 100644 --- a/rust/tests/issue_1066_test.rs +++ b/rust/tests/issue_1066_test.rs @@ -1,7 +1,7 @@ use issue_1066::{ issue_1066_codec, issue_1066_codec::{Issue1066Decoder, Issue1066Encoder}, - *, + message_header_codec, ReadBuf, WriteBuf, }; #[test] From 7bb5448089b344b439eaed29e628213aba682457 Mon Sep 17 00:00:00 2001 From: Michael Ward Date: Tue, 21 Oct 2025 06:27:40 -0500 Subject: [PATCH 4/5] [Rust] updated issue1066.xml --- sbe-tool/src/test/resources/issue1066.xml | 42 +++++++++++++---------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/sbe-tool/src/test/resources/issue1066.xml b/sbe-tool/src/test/resources/issue1066.xml index a24e45b030..270926cc70 100644 --- a/sbe-tool/src/test/resources/issue1066.xml +++ b/sbe-tool/src/test/resources/issue1066.xml @@ -1,18 +1,24 @@ - - - - - - - - - - - - - \ No newline at end of file + + + + + + + + + + + + + + \ No newline at end of file From e1577c31f9d358177c789c438ed342aab781ddaa Mon Sep 17 00:00:00 2001 From: Michael Ward Date: Tue, 21 Oct 2025 06:42:28 -0500 Subject: [PATCH 5/5] [Rust] updated rust tests --- rust/Cargo.toml | 4 +- rust/tests/baseline_enum_from_str.rs | 64 +++++++++++++++---- rust/tests/baseline_enum_into.rs | 34 +++++++--- rust/tests/baseline_enum_to_str.rs | 28 ++++++-- rust/tests/baseline_test.rs | 2 +- rust/tests/big_endian_test.rs | 6 +- rust/tests/fixed_sized_primitive_array.rs | 12 ++-- rust/tests/issue_987_test.rs | 7 +- .../basic-variable-length-schema.xml | 45 +++++++------ 9 files changed, 141 insertions(+), 61 deletions(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index f54de60807..76119b11b7 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -19,8 +19,8 @@ issue_987 = { path = "../generated/rust/issue987" } issue_1028 = { path = "../generated/rust/issue1028" } issue_1057 = { path = "../generated/rust/issue1057" } issue_1066 = { path = "../generated/rust/issue1066" } -baseline_bigendian = { path = "../generated/rust/baseline-bigendian" } -nested_composite_name = { path = "../generated/rust/nested-composite-name" } +baseline_bigendian = { path = "../generated/rust/baseline_bigendian" } +nested_composite_name = { path = "../generated/rust/nested_composite_name" } sbe_tests = { path = "../generated/rust/sbe_tests" } fixed_sized_primitive_array = { path = "../generated/rust/fixed_sized_primitive_array" } diff --git a/rust/tests/baseline_enum_from_str.rs b/rust/tests/baseline_enum_from_str.rs index a111d28364..26cf7a9102 100644 --- a/rust/tests/baseline_enum_from_str.rs +++ b/rust/tests/baseline_enum_from_str.rs @@ -1,22 +1,60 @@ +use examples_baseline::boost_type::BoostType; use BoostType::{NullVal, KERS, NITROUS, SUPERCHARGER, TURBO}; -use examples_baseline::{ - boost_type::BoostType, -}; #[test] fn test_boost_type_from_str() -> Result<(), ()> { - assert_eq!("TURBO".parse::()?, TURBO, "Parse \"TURBO\" as BoostType"); - assert_eq!("SUPERCHARGER".parse::()?, SUPERCHARGER, "Parse \"SUPERCHARGER\" as BoostType"); - assert_eq!("NITROUS".parse::()?, NITROUS, "Parse \"NITROUS\" as BoostType"); - assert_eq!("KERS".parse::()?, KERS, "Parse \"KERS\" as BoostType"); + assert_eq!( + "TURBO".parse::()?, + TURBO, + "Parse \"TURBO\" as BoostType" + ); + assert_eq!( + "SUPERCHARGER".parse::()?, + SUPERCHARGER, + "Parse \"SUPERCHARGER\" as BoostType" + ); + assert_eq!( + "NITROUS".parse::()?, + NITROUS, + "Parse \"NITROUS\" as BoostType" + ); + assert_eq!( + "KERS".parse::()?, + KERS, + "Parse \"KERS\" as BoostType" + ); - assert_eq!("Turbo".parse::()?, NullVal, "Parse \"Turbo\" as BoostType"); - assert_eq!("Supercharger".parse::()?, NullVal, "Parse \"Supercharger\" as BoostType"); - assert_eq!("Nitrous".parse::()?, NullVal, "Parse \"Nitrous\" as BoostType"); - assert_eq!("Kers".parse::()?, NullVal, "Parse \"Kers\" as BoostType"); + assert_eq!( + "Turbo".parse::()?, + NullVal, + "Parse \"Turbo\" as BoostType" + ); + assert_eq!( + "Supercharger".parse::()?, + NullVal, + "Parse \"Supercharger\" as BoostType" + ); + assert_eq!( + "Nitrous".parse::()?, + NullVal, + "Parse \"Nitrous\" as BoostType" + ); + assert_eq!( + "Kers".parse::()?, + NullVal, + "Parse \"Kers\" as BoostType" + ); - assert_eq!("AA".parse::()?, NullVal, "Parse \"AA\" as BoostType"); - assert_eq!("".parse::().unwrap(), NullVal, "Parse \"\" as BoostType"); + assert_eq!( + "AA".parse::()?, + NullVal, + "Parse \"AA\" as BoostType" + ); + assert_eq!( + "".parse::().unwrap(), + NullVal, + "Parse \"\" as BoostType" + ); Ok(()) } diff --git a/rust/tests/baseline_enum_into.rs b/rust/tests/baseline_enum_into.rs index 9580e51b0d..0af9b0a585 100644 --- a/rust/tests/baseline_enum_into.rs +++ b/rust/tests/baseline_enum_into.rs @@ -1,14 +1,32 @@ -use examples_baseline::{ - boost_type::BoostType, -}; +use examples_baseline::boost_type::BoostType; #[test] fn test_boost_type_from_str() -> Result<(), ()> { - assert_eq!(>::into(BoostType::TURBO), 84_u8, "BoostType::TURBO into value"); - assert_eq!(>::into(BoostType::SUPERCHARGER), 83_u8, "BoostType::SUPERCHARGER into value"); - assert_eq!(>::into(BoostType::NITROUS), 78_u8, "BoostType::NITROUS into value"); - assert_eq!(>::into(BoostType::KERS), 75_u8, "BoostType::KERS into value"); - assert_eq!(>::into(BoostType::NullVal), 0_u8, "BoostType::NullVal into value"); + assert_eq!( + >::into(BoostType::TURBO), + 84_u8, + "BoostType::TURBO into value" + ); + assert_eq!( + >::into(BoostType::SUPERCHARGER), + 83_u8, + "BoostType::SUPERCHARGER into value" + ); + assert_eq!( + >::into(BoostType::NITROUS), + 78_u8, + "BoostType::NITROUS into value" + ); + assert_eq!( + >::into(BoostType::KERS), + 75_u8, + "BoostType::KERS into value" + ); + assert_eq!( + >::into(BoostType::NullVal), + 0_u8, + "BoostType::NullVal into value" + ); Ok(()) } diff --git a/rust/tests/baseline_enum_to_str.rs b/rust/tests/baseline_enum_to_str.rs index 93f5206517..8739d6ec9d 100644 --- a/rust/tests/baseline_enum_to_str.rs +++ b/rust/tests/baseline_enum_to_str.rs @@ -1,14 +1,28 @@ -use examples_baseline::{ - boost_type::BoostType, -}; +use examples_baseline::boost_type::BoostType; #[test] fn test_boost_type_from_str() -> Result<(), ()> { - assert_eq!(format!("{}", BoostType::TURBO), "TURBO", "Display \"TURBO\""); - assert_eq!(format!("{}", BoostType::SUPERCHARGER), "SUPERCHARGER", "Display \"SUPERCHARGER\""); - assert_eq!(format!("{}", BoostType::NITROUS), "NITROUS", "Display \"NITROUS\""); + assert_eq!( + format!("{}", BoostType::TURBO), + "TURBO", + "Display \"TURBO\"" + ); + assert_eq!( + format!("{}", BoostType::SUPERCHARGER), + "SUPERCHARGER", + "Display \"SUPERCHARGER\"" + ); + assert_eq!( + format!("{}", BoostType::NITROUS), + "NITROUS", + "Display \"NITROUS\"" + ); assert_eq!(format!("{}", BoostType::KERS), "KERS", "Display \"KERS\""); - assert_eq!(format!("{}", BoostType::NullVal), "NullVal", "Display \"NullVal\""); + assert_eq!( + format!("{}", BoostType::NullVal), + "NullVal", + "Display \"NullVal\"" + ); Ok(()) } diff --git a/rust/tests/baseline_test.rs b/rust/tests/baseline_test.rs index ab711b7c84..49486795a2 100644 --- a/rust/tests/baseline_test.rs +++ b/rust/tests/baseline_test.rs @@ -272,4 +272,4 @@ fn test_issue_1018() { assert_eq!(1, examples_baseline::SBE_SCHEMA_ID); assert_eq!(0, examples_baseline::SBE_SCHEMA_VERSION); assert_eq!("5.2", examples_baseline::SBE_SEMANTIC_VERSION); -} \ No newline at end of file +} diff --git a/rust/tests/big_endian_test.rs b/rust/tests/big_endian_test.rs index 1bef7278dd..248a367d52 100644 --- a/rust/tests/big_endian_test.rs +++ b/rust/tests/big_endian_test.rs @@ -1,10 +1,8 @@ +use baseline_bigendian::car_codec::{CarDecoder, CarEncoder, SBE_TEMPLATE_ID}; use baseline_bigendian::{ boolean_type::BooleanType, boost_type::BoostType, - car_codec::{ - encoder::{AccelerationEncoder, FuelFiguresEncoder, PerformanceFiguresEncoder}, - *, - }, + car_codec::encoder::{AccelerationEncoder, FuelFiguresEncoder, PerformanceFiguresEncoder}, message_header_codec, message_header_codec::MessageHeaderDecoder, model::Model, diff --git a/rust/tests/fixed_sized_primitive_array.rs b/rust/tests/fixed_sized_primitive_array.rs index 8542e3d488..59e6df84d6 100644 --- a/rust/tests/fixed_sized_primitive_array.rs +++ b/rust/tests/fixed_sized_primitive_array.rs @@ -337,28 +337,32 @@ fn test_encode_then_decode_non_u8_signed_primitive_slice() { DemoDecoder::fixed_16_i8, DemoEncoder::fixed_16_i8_end, DemoDecoder::fixed_16_i8_end, - i8, i8::from_le_bytes([uninit]) + i8, + i8::from_le_bytes([uninit]) ); run_encode_then_decode_for_array_of_signed_len_16!( DemoEncoder::fixed_16_i16_from_iter, DemoDecoder::fixed_16_i16, DemoEncoder::fixed_16_i16_end, DemoDecoder::fixed_16_i16_end, - i16, i16::from_le_bytes([uninit, uninit]) + i16, + i16::from_le_bytes([uninit, uninit]) ); run_encode_then_decode_for_array_of_signed_len_16!( DemoEncoder::fixed_16_i32_from_iter, DemoDecoder::fixed_16_i32, DemoEncoder::fixed_16_i32_end, DemoDecoder::fixed_16_i32_end, - i32, i32::from_le_bytes([uninit, uninit, uninit, uninit]) + i32, + i32::from_le_bytes([uninit, uninit, uninit, uninit]) ); run_encode_then_decode_for_array_of_signed_len_16!( DemoEncoder::fixed_16_i64_from_iter, DemoDecoder::fixed_16_i64, DemoEncoder::fixed_16_i64_end, DemoDecoder::fixed_16_i64_end, - i64, i64::from_le_bytes([uninit, uninit, uninit, uninit, uninit, uninit, uninit, uninit]) + i64, + i64::from_le_bytes([uninit, uninit, uninit, uninit, uninit, uninit, uninit, uninit]) ); } diff --git a/rust/tests/issue_987_test.rs b/rust/tests/issue_987_test.rs index 4462f58b52..bd496ee801 100644 --- a/rust/tests/issue_987_test.rs +++ b/rust/tests/issue_987_test.rs @@ -1,5 +1,10 @@ use issue_987::{ - issue_987_codec::{Issue987Decoder, Issue987Encoder, SBE_BLOCK_LENGTH, SBE_SCHEMA_ID, SBE_SCHEMA_VERSION, SBE_TEMPLATE_ID}, message_header_codec::MessageHeaderDecoder, * + issue_987_codec::{ + Issue987Decoder, Issue987Encoder, SBE_BLOCK_LENGTH, SBE_SCHEMA_ID, SBE_SCHEMA_VERSION, + SBE_TEMPLATE_ID, + }, + message_header_codec::MessageHeaderDecoder, + *, }; fn create_encoder(buffer: &mut Vec, off: usize) -> Issue987Encoder { diff --git a/sbe-tool/src/test/resources/basic-variable-length-schema.xml b/sbe-tool/src/test/resources/basic-variable-length-schema.xml index bd5dee2de9..39d681b989 100644 --- a/sbe-tool/src/test/resources/basic-variable-length-schema.xml +++ b/sbe-tool/src/test/resources/basic-variable-length-schema.xml @@ -1,22 +1,25 @@ - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + +