1use std::marker::PhantomData;
2
3use sys::*;
4use Device;
5
6pub struct Devices<'a> {
8 ptr: *mut hid_device_info,
9 cur: *mut hid_device_info,
10
11 _marker: PhantomData<&'a ()>,
12}
13
14impl<'a> Devices<'a> {
15 #[doc(hidden)]
16 pub unsafe fn new(vendor: Option<u16>, product: Option<u16>) -> Self {
17 let list = hid_enumerate(vendor.unwrap_or(0), product.unwrap_or(0));
18
19 Devices {
20 ptr: list,
21 cur: list,
22
23 _marker: PhantomData,
24 }
25 }
26}
27
28impl<'a> Iterator for Devices<'a> {
29 type Item = Device<'a>;
30
31 fn next(&mut self) -> Option<Self::Item> {
32 if self.cur.is_null() {
33 return None;
34 }
35
36 unsafe {
37 let info = Device::new(self.cur);
38 self.cur = (*self.cur).next;
39
40 Some(info)
41 }
42 }
43}
44
45impl<'a> Drop for Devices<'a> {
46 fn drop(&mut self) {
47 unsafe {
48 hid_free_enumeration(self.ptr);
49 }
50 }
51}