From fc724dd7075fd833bac3f2a25a96aac76a771ec3 Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Tue, 19 Dec 2023 11:48:58 +0200 Subject: stm32: i2c: Clean up conditional code a bit By moving conditional code inside the functions, we can reduce duplication and in one case we can even eliminate one... --- embassy-stm32/src/i2c/mod.rs | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/embassy-stm32/src/i2c/mod.rs b/embassy-stm32/src/i2c/mod.rs index 9b0b35eca..2416005b5 100644 --- a/embassy-stm32/src/i2c/mod.rs +++ b/embassy-stm32/src/i2c/mod.rs @@ -148,37 +148,30 @@ struct Timeout { #[allow(dead_code)] impl Timeout { - #[cfg(not(feature = "time"))] - #[inline] - fn check(self) -> Result<(), Error> { - Ok(()) - } - - #[cfg(feature = "time")] #[inline] fn check(self) -> Result<(), Error> { + #[cfg(feature = "time")] if Instant::now() > self.deadline { - Err(Error::Timeout) - } else { - Ok(()) + return Err(Error::Timeout); } - } - #[cfg(not(feature = "time"))] - #[inline] - fn with(self, fut: impl Future>) -> impl Future> { - fut + Ok(()) } - #[cfg(feature = "time")] #[inline] fn with(self, fut: impl Future>) -> impl Future> { - use futures::FutureExt; + #[cfg(feature = "time")] + { + use futures::FutureExt; + + embassy_futures::select::select(embassy_time::Timer::at(self.deadline), fut).map(|r| match r { + embassy_futures::select::Either::First(_) => Err(Error::Timeout), + embassy_futures::select::Either::Second(r) => r, + }) + } - embassy_futures::select::select(embassy_time::Timer::at(self.deadline), fut).map(|r| match r { - embassy_futures::select::Either::First(_) => Err(Error::Timeout), - embassy_futures::select::Either::Second(r) => r, - }) + #[cfg(not(feature = "time"))] + fut } } -- cgit