Skip to main content

async_hid/
device.rs

1use std::future::Future;
2
3use crate::backend::{Backend, DynBackend};
4use crate::traits::{AsyncHidFeatureHandle, AsyncHidRead, AsyncHidWrite};
5use crate::HidResult;
6
7/// A reader than can be used to read input reports from a HID device using [AsyncHidRead::read_input_report]
8#[repr(transparent)]
9pub struct DeviceReader(pub(crate) <DynBackend as Backend>::Reader);
10
11/// A writer than can be used to write output reports from a HID device using [AsyncHidWrite::write_output_report]
12#[repr(transparent)]
13pub struct DeviceWriter(pub(crate) <DynBackend as Backend>::Writer);
14
15/// A reader than can be used to read feature reports from a HID device using [AsyncHidRead::read_feature_report]
16#[repr(transparent)]
17pub struct DeviceFeatureHandle(pub(crate) <DynBackend as Backend>::FeatureHandle);
18
19/// Combination of [DeviceReader] and [DeviceWriter]
20///
21/// Can either be destructured or used directly
22pub type DeviceReaderWriter = (DeviceReader, DeviceWriter);
23
24impl AsyncHidRead for DeviceReader {
25    #[inline]
26    fn read_input_report<'a>(&'a mut self, buf: &'a mut [u8]) -> impl Future<Output = HidResult<usize>> + Send + 'a {
27        self.0.read_input_report(buf)
28    }
29}
30
31impl AsyncHidWrite for DeviceWriter {
32    #[inline]
33    fn write_output_report<'a>(&'a mut self, buf: &'a [u8]) -> impl Future<Output = HidResult<()>> + Send + 'a {
34        self.0.write_output_report(buf)
35    }
36}
37
38impl AsyncHidRead for DeviceReaderWriter {
39    #[inline]
40    fn read_input_report<'a>(&'a mut self, buf: &'a mut [u8]) -> impl Future<Output = HidResult<usize>> + Send + 'a {
41        self.0.read_input_report(buf)
42    }
43}
44
45impl AsyncHidWrite for DeviceReaderWriter {
46    #[inline]
47    fn write_output_report<'a>(&'a mut self, buf: &'a [u8]) -> impl Future<Output = HidResult<()>> + Send + 'a {
48        self.1.write_output_report(buf)
49    }
50}
51
52impl AsyncHidFeatureHandle for DeviceFeatureHandle {
53    #[inline]
54    fn read_feature_report<'a>(&'a mut self, buf: &'a mut [u8]) -> impl Future<Output = HidResult<usize>> + Send + 'a {
55        self.0.read_feature_report(buf)
56    }
57
58    #[inline]
59    fn write_feature_report<'a>(&'a mut self, buf: &'a [u8]) -> impl Future<Output = HidResult<()>> + Send + 'a {
60        self.0.write_feature_report(buf)
61    }
62}