ethercrab-rs / ethercrab

EtherCAT master written in pure Rust
256 stars 24 forks source link

Unpack from slice #250

Open jbbjarnason opened 4 days ago

jbbjarnason commented 4 days ago

I changed type of bar to u32 and get ReadBufferTooShort

thread 'basic_struct' panicked at ethercrab-wire/tests/unpack.rs:28:46:
called `Result::unwrap()` on an `Err` value: ReadBufferTooShort
stack backtrace:
   0: rust_begin_unwind
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/std/src/panicking.rs:652:5
   1: core::panicking::panic_fmt
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/result.rs:1654:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/result.rs:1077:23
   4: unpack::basic_struct
             at ./tests/unpack.rs:28:15
   5: unpack::basic_struct::{{closure}}
             at ./tests/unpack.rs:4:18
   6: core::ops::function::FnOnce::call_once
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ops/function.rs:250:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

failures:
    basic_struct
diff --git a/ethercrab-wire/tests/unpack.rs b/ethercrab-wire/tests/unpack.rs
index 408ad53..0b5490a 100644
--- a/ethercrab-wire/tests/unpack.rs
+++ b/ethercrab-wire/tests/unpack.rs
@@ -8,7 +8,7 @@ fn basic_struct() {
         #[wire(bits = 8)]
         foo: u8,
         #[wire(bits = 16)]
-        bar: u16,
+        bar: u32,
         #[wire(bits = 32)]
         baz: u32,
     }

I am guessing this should work?

jamwaffles commented 3 days ago

Hm, yes that should still work because #[wire(bits = 16)] is forcing the length. Thanks for the bug report! I'll take a look when I have a mo :)