Skip to content

Commit

Permalink
[Rust] encoding primitive arrays now supports slice instead of array …
Browse files Browse the repository at this point in the history
…(issue #1021) (#1040)

* [Rust] codegen now implements: "From<&'a mut WriteBuf<'a>> for &'a mut [u8]"

* [Rust] refactored codegen of primitive array encoding to accept slice instead of array

* [Rust] updated some tests

* [Rust] updated codegen to create additional function for primitive arrays that will accept an "impl Iterator".  This allows an arbitrary number of items to be passed in, which will guarantee the correct number of items are encoded (defined 'nullValue' will be used where appropriate)

* [Rust] generate at_most_*_items_from_slice setter for fixed sized primitive array in message

* [Rust] add padded support for slice setter for primitive array field

* [Rust] simplify impl of generated slice aware methods

* merging in PR

* [Rust] updated generator to create "_from_iter" and "_zero_padded" functions for primitive array

* [Rust] updated generator to create "_from_iter" and "_zero_padded" functions for primitive array

* fixed formatting issues

* removed unused format argument

---------

Co-authored-by: Michael Ward <[email protected]>
Co-authored-by: Elvis Wang <[email protected]>
  • Loading branch information
3 people authored Dec 30, 2024
1 parent 34f2c32 commit a1bc244
Show file tree
Hide file tree
Showing 10 changed files with 1,369 additions and 75 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ tasks.register('generateRustTestCodecs', JavaExec) {
'sbe-tool/src/test/resources/issue984.xml',
'sbe-tool/src/test/resources/issue987.xml',
'sbe-tool/src/test/resources/issue1028.xml',
'sbe-tool/src/test/resources/fixed-sized-primitive-array-types.xml',
'sbe-tool/src/test/resources/example-bigendian-test-schema.xml',
'sbe-tool/src/test/resources/nested-composite-name.xml',
]
Expand Down
1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ issue_987 = { path = "../generated/rust/issue987" }
issue_1028 = { path = "../generated/rust/issue1028" }
baseline_bigendian = { path = "../generated/rust/baseline-bigendian" }
nested_composite_name = { path = "../generated/rust/nested-composite-name" }
fixed_sized_primitive_array = { path = "../generated/rust/fixed_sized_primitive_array" }

[dev-dependencies]
criterion = "0.5"
Expand Down
21 changes: 11 additions & 10 deletions rust/tests/baseline_enum_from_str.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
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::<BoostType>()?, BoostType::TURBO, "Parse \"TURBO\" as BoostType");
assert_eq!("SUPERCHARGER".parse::<BoostType>()?, BoostType::SUPERCHARGER, "Parse \"SUPERCHARGER\" as BoostType");
assert_eq!("NITROUS".parse::<BoostType>()?, BoostType::NITROUS, "Parse \"NITROUS\" as BoostType");
assert_eq!("KERS".parse::<BoostType>()?, BoostType::KERS, "Parse \"KERS\" as BoostType");
assert_eq!("TURBO".parse::<BoostType>()?, TURBO, "Parse \"TURBO\" as BoostType");
assert_eq!("SUPERCHARGER".parse::<BoostType>()?, SUPERCHARGER, "Parse \"SUPERCHARGER\" as BoostType");
assert_eq!("NITROUS".parse::<BoostType>()?, NITROUS, "Parse \"NITROUS\" as BoostType");
assert_eq!("KERS".parse::<BoostType>()?, KERS, "Parse \"KERS\" as BoostType");

assert_eq!("Turbo".parse::<BoostType>()?, BoostType::NullVal, "Parse \"Turbo\" as BoostType");
assert_eq!("Supercharger".parse::<BoostType>()?, BoostType::NullVal, "Parse \"Supercharger\" as BoostType");
assert_eq!("Nitrous".parse::<BoostType>()?, BoostType::NullVal, "Parse \"Nitrous\" as BoostType");
assert_eq!("Kers".parse::<BoostType>()?, BoostType::NullVal, "Parse \"Kers\" as BoostType");
assert_eq!("Turbo".parse::<BoostType>()?, NullVal, "Parse \"Turbo\" as BoostType");
assert_eq!("Supercharger".parse::<BoostType>()?, NullVal, "Parse \"Supercharger\" as BoostType");
assert_eq!("Nitrous".parse::<BoostType>()?, NullVal, "Parse \"Nitrous\" as BoostType");
assert_eq!("Kers".parse::<BoostType>()?, NullVal, "Parse \"Kers\" as BoostType");

assert_eq!("AA".parse::<BoostType>()?, BoostType::NullVal, "Parse \"AA\" as BoostType");
assert_eq!("".parse::<BoostType>().unwrap(), BoostType::NullVal, "Parse \"\" as BoostType");
assert_eq!("AA".parse::<BoostType>()?, NullVal, "Parse \"AA\" as BoostType");
assert_eq!("".parse::<BoostType>().unwrap(), NullVal, "Parse \"\" as BoostType");

Ok(())
}
4 changes: 2 additions & 2 deletions rust/tests/extension_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn encode_car_from_scratch() -> SbeResult<(usize, Vec<u8>)> {
car.available(BooleanType::T);
car.code(Model::A);
car.some_numbers(&[0, 1, 2, 3]);
car.vehicle_code(&[97, 98, 99, 100, 101, 102]); // abcdef
car.vehicle_code_from_iter(b"abcdef_extra_is_ignored".into_iter().copied()); // abcdef

extras.set_cruise_control(true);
extras.set_sports_pack(true);
Expand All @@ -197,7 +197,7 @@ fn encode_car_from_scratch() -> SbeResult<(usize, Vec<u8>)> {
let mut engine = car.engine_encoder();
engine.capacity(2000);
engine.num_cylinders(4);
engine.manufacturer_code(&[49, 50, 51]); // 123
engine.manufacturer_code(b"123");
engine.efficiency(35);
engine.booster_enabled(BooleanType::T);
let mut booster = engine.booster_encoder();
Expand Down
Loading

0 comments on commit a1bc244

Please sign in to comment.