rust-embedded / rust-embedded/embedded-hal-mock

Unwrap in I2C transaction

Open
#127 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
146
Forks
29
PR merge metrics
No merged PRs in 30d

Description

Let's say I have an I2C transaction like this one below. Clearly, any of the operations can fail and in this case error should be returned (At least I2c interface allows this). To simulate such failed operation, I was expecting with_error approach to work (Line 3).

let mut i2c = Mock::new(&[
    I2cTransaction::transaction_start(0x76),
    I2cTransaction::write(0x76, vec![0x88]), // .with_error(I2cErrorKind::Other),
    I2cTransaction::read(0x76, vec![0xCC, 0x6D, 0x1B, 0x68, 0x32, 0x00]),
    I2cTransaction::write(0x76, vec![0xA1]),
    I2cTransaction::read(0x76, vec![0x4B]),
    I2cTransaction::write(0x76, vec![0xE1]),
    I2cTransaction::read(0x76, vec![0x70, 0x01, 0x00, 0x13, 0x21, 0x03, 0x1E]),
    I2cTransaction::write(0x76, vec![0xFA]),
    I2cTransaction::read(0x76, vec![0x7E, 0x9B, 0x00, 0x5D, 0xC2])
        .with_error(I2cErrorKind::Other),
    I2cTransaction::transaction_end(0x76),
])

But it does not and causes panic instead. This panic originates from unwrap on the following line:
https://github.com/dbrgn/embedded-hal-mock/blob/e11348e9d34ef5809eb3b13979bab0efd7d92a44/src/eh1/i2c.rs#L273
For me it seems ? would be more appropriate.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at src/eh1/i2c.rs line 273, where the issue reports that an I2C operation error is unwrapped, and inspect the surrounding transaction error handling. Check the existing I2C mock tests and add coverage for the with_error case; done means the configured error is returned instead of causing a panic.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
embedded-iot, testing-qa
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.