embedded_platform/i2c/
begin_write.rs

1//! Defines futures for initiating writes to an I²C peripheral.
2use core::future;
3use core::pin;
4use core::task;
5
6/// A future which initializes writes to an I²C peripheral.
7#[derive(Debug)]
8#[must_use = "futures do nothing unless you `.await` or poll them"]
9pub struct BeginWrite<'a, A>
10where
11    A: super::I2cWrite + Unpin + ?Sized,
12{
13    writer: &'a mut A,
14    address: u8,
15}
16
17/// Creates a new [`BeginWrite`] for the provided I²C peripheral.
18///
19/// The write will access the specified address.
20pub fn begin_write<A>(writer: &mut A, address: u8) -> BeginWrite<A>
21where
22    A: super::I2cWrite + Unpin + ?Sized,
23{
24    BeginWrite { writer, address }
25}
26
27impl<A> future::Future for BeginWrite<'_, A>
28where
29    A: super::I2cWrite + Unpin + ?Sized,
30{
31    type Output = Result<A::Write, 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.writer).poll_begin_write(cx, this.address)
36    }
37}