From 87167e5f8210759bd44d2b118b018822c131ec42 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Wed, 10 Jun 2020 18:35:49 +0200 Subject: [PATCH] Fix compile and clippy errors (why the same again!?) --- src/ds18b20.rs | 12 ++++++---- src/lib.rs | 59 ++++++++++++++++++++++++++++---------------------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/ds18b20.rs b/src/ds18b20.rs index 2a7c9f4..d4e4bda 100644 --- a/src/ds18b20.rs +++ b/src/ds18b20.rs @@ -33,10 +33,10 @@ pub enum MeasureResolution { impl MeasureResolution { pub fn time_ms(&self) -> u16 { match self { - &MeasureResolution::TC8 => 94, - &MeasureResolution::TC4 => 188, - &MeasureResolution::TC2 => 375, - &MeasureResolution::TC => 750, + MeasureResolution::TC8 => 94, + MeasureResolution::TC4 => 188, + MeasureResolution::TC2 => 375, + MeasureResolution::TC => 750, } } } @@ -58,6 +58,10 @@ impl DS18B20 { } } + /// # Safety + /// + /// This is marked as unsafe because it does not check whether the given address + /// is compatible with a DS18B20 device. It assumes so. pub unsafe fn new_forced(device: Device) -> DS18B20 { DS18B20 { device, diff --git a/src/lib.rs b/src/lib.rs index 445d943..54feade 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ pub enum Command { } #[derive(Debug)] -pub enum Error { +pub enum Error { WireNotHigh, CrcMismatch(u8, u8), FamilyCodeMismatch(u8, u8), @@ -45,27 +45,31 @@ pub struct Device { } impl Device { - pub fn from_str(string: &str) -> Result { - if string.len() < 23 { + pub fn family_code(&self) -> u8 { + self.address[0] + } +} + +impl core::str::FromStr for Device { + type Err = core::num::ParseIntError; + + fn from_str(s: &str) -> Result { + if s.len() < 23 { let _ = u8::from_str_radix("", 16)?; // this causes a ParseIntError::Empty } Ok(Device { address: [ - u8::from_str_radix(&string[0..2], 16)?, - u8::from_str_radix(&string[3..5], 16)?, - u8::from_str_radix(&string[6..8], 16)?, - u8::from_str_radix(&string[9..11], 16)?, - u8::from_str_radix(&string[12..14], 16)?, - u8::from_str_radix(&string[15..17], 16)?, - u8::from_str_radix(&string[18..20], 16)?, - u8::from_str_radix(&string[21..23], 16)?, + u8::from_str_radix(&s[0..2], 16)?, + u8::from_str_radix(&s[3..5], 16)?, + u8::from_str_radix(&s[6..8], 16)?, + u8::from_str_radix(&s[9..11], 16)?, + u8::from_str_radix(&s[12..14], 16)?, + u8::from_str_radix(&s[15..17], 16)?, + u8::from_str_radix(&s[18..20], 16)?, + u8::from_str_radix(&s[21..23], 16)?, ], }) } - - pub fn family_code(&self) -> u8 { - self.address[0] - } } #[derive(Debug, Clone, Copy, PartialEq)] @@ -75,7 +79,13 @@ enum SearchState { End, } -#[derive(Clone)] +impl Default for SearchState { + fn default() -> Self { + SearchState::Initialized + } +} + +#[derive(Clone, Default)] pub struct DeviceSearch { address: [u8; 8], discrepancies: [u8; 8], @@ -84,11 +94,7 @@ pub struct DeviceSearch { impl DeviceSearch { pub fn new() -> DeviceSearch { - DeviceSearch { - address: [0u8; ADDRESS_BYTES as usize], - discrepancies: [0u8; ADDRESS_BYTES as usize], - state: SearchState::Initialized, - } + DeviceSearch::default() } pub fn new_for_family(family: u8) -> DeviceSearch { @@ -129,6 +135,7 @@ impl DeviceSearch { DeviceSearch::reset_bit(&mut self.discrepancies, bit); } + #[allow(unused)] // useful method anyway? fn write_bit_in_discrepancy(&mut self, bit: u8, value: bool) { if value { self.set_bit_in_discrepancy(bit); @@ -283,7 +290,7 @@ impl> OneWire { ) -> Result<(), Error> { self.reset(delay)?; self.select(delay, device)?; - self.select(delay, device); + self.select(delay, device)?; self.read_bytes(delay, read)?; Ok(()) } @@ -296,7 +303,7 @@ impl> OneWire { ) -> Result<(), Error> { self.reset(delay)?; self.select(delay, device)?; - self.select(delay, device); + self.select(delay, device)?; self.write_bytes(delay, write)?; Ok(()) } @@ -411,7 +418,7 @@ impl> OneWire { rom.state = SearchState::DeviceFound; } Ok(Some(Device { - address: rom.address.clone(), + address: rom.address, })) } @@ -455,8 +462,8 @@ impl> OneWire { } pub fn read_bytes(&mut self, delay: &mut impl DelayUs, dst: &mut [u8]) -> Result<(), E> { - for i in 0..dst.len() { - dst[i] = self.read_byte(delay)?; + for d in dst { + *d = self.read_byte(delay)?; } Ok(()) } -- GitLab