drmem_api/driver/
ro_device.rs

1use crate::device;
2use std::future::Future;
3use std::marker::PhantomData;
4use std::pin::Pin;
5
6/// A function that drivers use to report updated values of a device.
7pub type ReportReading = Box<
8    dyn Fn(device::Value) -> Pin<Box<dyn Future<Output = ()> + Send>>
9        + Send
10        + Sync,
11>;
12
13/// Represents a read-only device that uses a specified type for its
14/// reading. Any type that can be converted to a `device::Value` is
15/// acceptable.
16pub struct ReadOnlyDevice<T: Into<device::Value> + Clone> {
17    report_chan: ReportReading,
18    phantom: PhantomData<T>,
19}
20
21impl<T> ReadOnlyDevice<T>
22where
23    T: Into<device::Value> + Clone,
24{
25    /// Returns a new `ReadOnlyDevice` type.
26    pub fn new(report_chan: ReportReading) -> Self {
27        ReadOnlyDevice {
28            report_chan,
29            phantom: PhantomData,
30        }
31    }
32
33    /// Saves a new value, returned by the device, to the backend
34    /// storage.
35    pub fn report_update(
36        &mut self,
37        value: T,
38    ) -> impl Future<Output = ()> + use<'_, T> {
39        (self.report_chan)(value.into())
40    }
41}