diff options
| author | Priit Laes <[email protected]> | 2023-12-19 11:48:58 +0200 |
|---|---|---|
| committer | Priit Laes <[email protected]> | 2023-12-19 11:48:58 +0200 |
| commit | fc724dd7075fd833bac3f2a25a96aac76a771ec3 (patch) | |
| tree | eff3b129131fa0090c12799ffe195e079319cc38 /embassy-stm32/src/i2c/mod.rs | |
| parent | 08e9a4d84a51a4a1511dcbbd73808731b4d44f06 (diff) | |
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...
Diffstat (limited to 'embassy-stm32/src/i2c/mod.rs')
| -rw-r--r-- | embassy-stm32/src/i2c/mod.rs | 35 |
1 files 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 { | |||
| 148 | 148 | ||
| 149 | #[allow(dead_code)] | 149 | #[allow(dead_code)] |
| 150 | impl Timeout { | 150 | impl Timeout { |
| 151 | #[cfg(not(feature = "time"))] | ||
| 152 | #[inline] | ||
| 153 | fn check(self) -> Result<(), Error> { | ||
| 154 | Ok(()) | ||
| 155 | } | ||
| 156 | |||
| 157 | #[cfg(feature = "time")] | ||
| 158 | #[inline] | 151 | #[inline] |
| 159 | fn check(self) -> Result<(), Error> { | 152 | fn check(self) -> Result<(), Error> { |
| 153 | #[cfg(feature = "time")] | ||
| 160 | if Instant::now() > self.deadline { | 154 | if Instant::now() > self.deadline { |
| 161 | Err(Error::Timeout) | 155 | return Err(Error::Timeout); |
| 162 | } else { | ||
| 163 | Ok(()) | ||
| 164 | } | 156 | } |
| 165 | } | ||
| 166 | 157 | ||
| 167 | #[cfg(not(feature = "time"))] | 158 | Ok(()) |
| 168 | #[inline] | ||
| 169 | fn with<R>(self, fut: impl Future<Output = Result<R, Error>>) -> impl Future<Output = Result<R, Error>> { | ||
| 170 | fut | ||
| 171 | } | 159 | } |
| 172 | 160 | ||
| 173 | #[cfg(feature = "time")] | ||
| 174 | #[inline] | 161 | #[inline] |
| 175 | fn with<R>(self, fut: impl Future<Output = Result<R, Error>>) -> impl Future<Output = Result<R, Error>> { | 162 | fn with<R>(self, fut: impl Future<Output = Result<R, Error>>) -> impl Future<Output = Result<R, Error>> { |
| 176 | use futures::FutureExt; | 163 | #[cfg(feature = "time")] |
| 164 | { | ||
| 165 | use futures::FutureExt; | ||
| 166 | |||
| 167 | embassy_futures::select::select(embassy_time::Timer::at(self.deadline), fut).map(|r| match r { | ||
| 168 | embassy_futures::select::Either::First(_) => Err(Error::Timeout), | ||
| 169 | embassy_futures::select::Either::Second(r) => r, | ||
| 170 | }) | ||
| 171 | } | ||
| 177 | 172 | ||
| 178 | embassy_futures::select::select(embassy_time::Timer::at(self.deadline), fut).map(|r| match r { | 173 | #[cfg(not(feature = "time"))] |
| 179 | embassy_futures::select::Either::First(_) => Err(Error::Timeout), | 174 | fut |
| 180 | embassy_futures::select::Either::Second(r) => r, | ||
| 181 | }) | ||
| 182 | } | 175 | } |
| 183 | } | 176 | } |
| 184 | 177 | ||
