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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use libc::c_int;

use std::{mem, slice};

use crate::{
    context::{GlobalContext, UsbContext},
    device::{self, Device},
    error,
};
use libusb1_sys::*;

/// A list of detected USB devices.
pub struct DeviceList<T: UsbContext> {
    context: T,
    list: *const *mut libusb_device,
    len: usize,
}

impl<T: UsbContext> Drop for DeviceList<T> {
    /// Frees the device list.
    fn drop(&mut self) {
        unsafe {
            libusb_free_device_list(self.list, 1);
        }
    }
}

impl DeviceList<GlobalContext> {
    pub fn new() -> crate::Result<DeviceList<GlobalContext>> {
        let mut list = mem::MaybeUninit::<*const *mut libusb_device>::uninit();

        let n =
            unsafe { libusb_get_device_list(GlobalContext::default().as_raw(), list.as_mut_ptr()) };

        if n < 0 {
            Err(error::from_libusb(n as c_int))
        } else {
            Ok(unsafe {
                DeviceList {
                    context: Default::default(),
                    list: list.assume_init(),
                    len: n as usize,
                }
            })
        }
    }
}

impl<T: UsbContext> DeviceList<T> {
    pub fn new_with_context(context: T) -> crate::Result<DeviceList<T>> {
        let mut list = mem::MaybeUninit::<*const *mut libusb_device>::uninit();

        let len = unsafe { libusb_get_device_list(context.as_raw(), list.as_mut_ptr()) };

        if len < 0 {
            Err(error::from_libusb(len as c_int))
        } else {
            Ok(unsafe {
                DeviceList {
                    context,
                    list: list.assume_init(),
                    len: len as usize,
                }
            })
        }
    }

    /// Returns the number of devices in the list.
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns true if the list is empty, else returns false.
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Returns an iterator over the devices in the list.
    ///
    /// The iterator yields a sequence of `Device` objects.
    pub fn iter(&self) -> Devices<T> {
        Devices {
            context: self.context.clone(),
            devices: unsafe { slice::from_raw_parts(self.list, self.len) },
            index: 0,
        }
    }
}

/// Iterator over detected USB devices.
pub struct Devices<'a, T> {
    context: T,
    devices: &'a [*mut libusb_device],
    index: usize,
}

impl<'a, T: UsbContext> Iterator for Devices<'a, T> {
    type Item = Device<T>;

    fn next(&mut self) -> Option<Device<T>> {
        if self.index < self.devices.len() {
            let device = self.devices[self.index];

            self.index += 1;
            Some(unsafe {
                device::Device::from_libusb(
                    self.context.clone(),
                    std::ptr::NonNull::new_unchecked(device),
                )
            })
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.devices.len() - self.index;
        (remaining, Some(remaining))
    }
}