Skip to content

Commit

Permalink
More safe handling of usize -> isize conversion with sane failure mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Pederson committed Jan 17, 2024
1 parent 6ebeffa commit 3b200f6
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use core::{I2CDevice, I2CMessage, I2CTransfer};
use std::convert::TryFrom;
use std::io;

/// I2C mock result type
Expand Down Expand Up @@ -44,7 +45,12 @@ impl I2CRegisterMap {
fn read(&mut self, data: &mut [u8]) -> I2CResult<()> {
let len = data.len();
data.clone_from_slice(&self.registers[self.offset..(self.offset + len)]);
println!("READ | 0x{:X} : {:?}", self.offset as isize - data.len() as isize, data);
println!(
"READ | 0x{:X} : {:?}",
isize::try_from(self.offset).unwrap_or(0xBAD)
- isize::try_from(data.len()).unwrap_or(0xBAD),
data
);
Ok(())
}

Expand Down

0 comments on commit 3b200f6

Please sign in to comment.