nusb/
hotplug.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//! Types for receiving notifications when USB devices are connected or
//! disconnected from the system.
//!
//! See [`super::watch_devices`] for a usage example.

use futures_core::Stream;

use crate::{DeviceId, DeviceInfo};

/// Stream of device connection / disconnection events.
///
/// Call [`super::watch_devices`] to begin watching device
/// events and create a `HotplugWatch`.
pub struct HotplugWatch(pub(crate) crate::platform::HotplugWatch);

impl Stream for HotplugWatch {
    type Item = HotplugEvent;

    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Option<Self::Item>> {
        self.0.poll_next(cx).map(Some)
    }
}

/// Event returned from the [`HotplugWatch`] stream.
#[derive(Debug)]
pub enum HotplugEvent {
    /// A device has been connected.
    Connected(DeviceInfo),

    /// A device has been disconnected.
    Disconnected(DeviceId),
}