Skip to content

Commit a1bc244

Browse files
mwardMichael Wardwbprime
authored
[Rust] encoding primitive arrays now supports slice instead of array (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 <mward@drw.com> Co-authored-by: Elvis Wang <mail@wangbo.im>
1 parent 34f2c32 commit a1bc244

File tree

10 files changed

+1369
-75
lines changed

10 files changed

+1369
-75
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ tasks.register('generateRustTestCodecs', JavaExec) {
665665
'sbe-tool/src/test/resources/issue984.xml',
666666
'sbe-tool/src/test/resources/issue987.xml',
667667
'sbe-tool/src/test/resources/issue1028.xml',
668+
'sbe-tool/src/test/resources/fixed-sized-primitive-array-types.xml',
668669
'sbe-tool/src/test/resources/example-bigendian-test-schema.xml',
669670
'sbe-tool/src/test/resources/nested-composite-name.xml',
670671
]

rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ issue_987 = { path = "../generated/rust/issue987" }
1919
issue_1028 = { path = "../generated/rust/issue1028" }
2020
baseline_bigendian = { path = "../generated/rust/baseline-bigendian" }
2121
nested_composite_name = { path = "../generated/rust/nested-composite-name" }
22+
fixed_sized_primitive_array = { path = "../generated/rust/fixed_sized_primitive_array" }
2223

2324
[dev-dependencies]
2425
criterion = "0.5"

rust/tests/baseline_enum_from_str.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1+
use BoostType::{NullVal, KERS, NITROUS, SUPERCHARGER, TURBO};
12
use examples_baseline::{
23
boost_type::BoostType,
34
};
45

56
#[test]
67
fn test_boost_type_from_str() -> Result<(), ()> {
7-
assert_eq!("TURBO".parse::<BoostType>()?, BoostType::TURBO, "Parse \"TURBO\" as BoostType");
8-
assert_eq!("SUPERCHARGER".parse::<BoostType>()?, BoostType::SUPERCHARGER, "Parse \"SUPERCHARGER\" as BoostType");
9-
assert_eq!("NITROUS".parse::<BoostType>()?, BoostType::NITROUS, "Parse \"NITROUS\" as BoostType");
10-
assert_eq!("KERS".parse::<BoostType>()?, BoostType::KERS, "Parse \"KERS\" as BoostType");
8+
assert_eq!("TURBO".parse::<BoostType>()?, TURBO, "Parse \"TURBO\" as BoostType");
9+
assert_eq!("SUPERCHARGER".parse::<BoostType>()?, SUPERCHARGER, "Parse \"SUPERCHARGER\" as BoostType");
10+
assert_eq!("NITROUS".parse::<BoostType>()?, NITROUS, "Parse \"NITROUS\" as BoostType");
11+
assert_eq!("KERS".parse::<BoostType>()?, KERS, "Parse \"KERS\" as BoostType");
1112

12-
assert_eq!("Turbo".parse::<BoostType>()?, BoostType::NullVal, "Parse \"Turbo\" as BoostType");
13-
assert_eq!("Supercharger".parse::<BoostType>()?, BoostType::NullVal, "Parse \"Supercharger\" as BoostType");
14-
assert_eq!("Nitrous".parse::<BoostType>()?, BoostType::NullVal, "Parse \"Nitrous\" as BoostType");
15-
assert_eq!("Kers".parse::<BoostType>()?, BoostType::NullVal, "Parse \"Kers\" as BoostType");
13+
assert_eq!("Turbo".parse::<BoostType>()?, NullVal, "Parse \"Turbo\" as BoostType");
14+
assert_eq!("Supercharger".parse::<BoostType>()?, NullVal, "Parse \"Supercharger\" as BoostType");
15+
assert_eq!("Nitrous".parse::<BoostType>()?, NullVal, "Parse \"Nitrous\" as BoostType");
16+
assert_eq!("Kers".parse::<BoostType>()?, NullVal, "Parse \"Kers\" as BoostType");
1617

17-
assert_eq!("AA".parse::<BoostType>()?, BoostType::NullVal, "Parse \"AA\" as BoostType");
18-
assert_eq!("".parse::<BoostType>().unwrap(), BoostType::NullVal, "Parse \"\" as BoostType");
18+
assert_eq!("AA".parse::<BoostType>()?, NullVal, "Parse \"AA\" as BoostType");
19+
assert_eq!("".parse::<BoostType>().unwrap(), NullVal, "Parse \"\" as BoostType");
1920

2021
Ok(())
2122
}

rust/tests/extension_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ fn encode_car_from_scratch() -> SbeResult<(usize, Vec<u8>)> {
187187
car.available(BooleanType::T);
188188
car.code(Model::A);
189189
car.some_numbers(&[0, 1, 2, 3]);
190-
car.vehicle_code(&[97, 98, 99, 100, 101, 102]); // abcdef
190+
car.vehicle_code_from_iter(b"abcdef_extra_is_ignored".into_iter().copied()); // abcdef
191191

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

0 commit comments

Comments
 (0)