| 1 | //! Types for receiving notifications when USB devices are connected or |
| 2 | //! disconnected from the system. |
| 3 | //! |
| 4 | //! See [`super::watch_devices`] for a usage example. |
| 5 | |
| 6 | use futures_core::Stream; |
| 7 | |
| 8 | use crate::{DeviceId, DeviceInfo}; |
| 9 | |
| 10 | /// Stream of device connection / disconnection events. |
| 11 | /// |
| 12 | /// Call [`super::watch_devices`] to begin watching device |
| 13 | /// events and create a `HotplugWatch`. |
| 14 | pub struct HotplugWatch(pub(crate) crate::platform::HotplugWatch); |
| 15 | |
| 16 | impl Stream for HotplugWatch { |
| 17 | type Item = HotplugEvent; |
| 18 | |
| 19 | fn poll_next( |
| 20 | mut self: std::pin::Pin<&mut Self>, |
| 21 | cx: &mut std::task::Context<'_>, |
| 22 | ) -> std::task::Poll<Option<Self::Item>> { |
| 23 | self.0.poll_next(cx).map(Some) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /// Event returned from the [`HotplugWatch`] stream. |
| 28 | #[derive(Debug)] |
| 29 | pub enum HotplugEvent { |
| 30 | /// A device has been connected. |
| 31 | Connected(DeviceInfo), |
| 32 | |
| 33 | /// A device has been disconnected. |
| 34 | Disconnected(DeviceId), |
| 35 | } |
| 36 | |
| 37 | #[test] |
| 38 | fn assert_send_sync() { |
| 39 | fn require_send_sync<T: Send + Sync>() {} |
| 40 | require_send_sync::<HotplugWatch>(); |
| 41 | } |