embedded_platform/i2c/
begin_read.rs

1//! Defines futures for initiating reads from an I²C peripheral.
2use core::future;
3use core::pin;
4use core::task;
5
6/// A future which initializes reads from an I²C peripheral.
7#[derive(Debug)]
8#[must_use = "futures do nothing unless you `.await` or poll them"]
9pub struct BeginRead<'a, A>
10where
11    A: super::I2cRead + Unpin + ?Sized,
12{
13    reader: &'a mut A,
14    address: u8,
15}
16
17/// Creates a new [`BeginRead`] for the provided I²C peripheral.
18///
19/// The read will access the specified address.
20pub fn begin_read<A>(reader: &mut A, address: u8) -> BeginRead<A>
21where
22    A: super::I2cRead + Unpin + ?Sized,
23{
24    BeginRead { reader, address }
25}
26
27impl<A> future::Future for BeginRead<'_, A>
28where
29    A: super::I2cRead + Unpin + ?Sized,
30{
31    type Output = Result<A::Read, A::Error>;
32
33    fn poll(mut self: pin::Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
34        let this = &mut *self;
35        pin::Pin::new(&mut *this.reader).poll_begin_read(cx, this.address)
36    }
37}