Merge pull request #2314 from plaes/stm32-i2c-conditional-time

stm32: i2c: Clean up conditional code a bit
This commit is contained in:
Dario Nieuwenhuis 2023-12-19 11:00:37 +00:00 committed by GitHub
commit 6f21f0680e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -148,38 +148,31 @@ struct Timeout {
#[allow(dead_code)] #[allow(dead_code)]
impl Timeout { impl Timeout {
#[cfg(not(feature = "time"))]
#[inline] #[inline]
fn check(self) -> Result<(), Error> { fn check(self) -> Result<(), Error> {
#[cfg(feature = "time")]
if Instant::now() > self.deadline {
return Err(Error::Timeout);
}
Ok(()) Ok(())
} }
#[cfg(feature = "time")]
#[inline] #[inline]
fn check(self) -> Result<(), Error> { fn with<R>(self, fut: impl Future<Output = Result<R, Error>>) -> impl Future<Output = Result<R, Error>> {
if Instant::now() > self.deadline { #[cfg(feature = "time")]
Err(Error::Timeout) {
} else { use futures::FutureExt;
Ok(())
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"))] #[cfg(not(feature = "time"))]
#[inline]
fn with<R>(self, fut: impl Future<Output = Result<R, Error>>) -> impl Future<Output = Result<R, Error>> {
fut fut
} }
#[cfg(feature = "time")]
#[inline]
fn with<R>(self, fut: impl Future<Output = Result<R, Error>>) -> impl Future<Output = Result<R, Error>> {
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,
})
}
} }
pub(crate) mod sealed { pub(crate) mod sealed {