aboutsummaryrefslogtreecommitdiff
path: root/hid/src/devices.rs
blob: 002ffa9c1871c3c210af4a18474f4611a9018328 (plain)
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
use std::marker::PhantomData;

use sys::*;
use Device;

/// An iterator over the available devices.
pub struct Devices<'a> {
	ptr: *mut hid_device_info,
	cur: *mut hid_device_info,

	_marker: PhantomData<&'a ()>,
}

impl<'a> Devices<'a> {
	#[doc(hidden)]
	pub unsafe fn new(vendor: Option<u16>, product: Option<u16>) -> Self {
		let list = hid_enumerate(vendor.unwrap_or(0), product.unwrap_or(0));

		Devices {
			ptr: list,
			cur: list,

			_marker: PhantomData,
		}
	}
}

impl<'a> Iterator for Devices<'a> {
	type Item = Device<'a>;

	fn next(&mut self) -> Option<Self::Item> {
		if self.cur.is_null() {
			return None;
		}

		unsafe {
			let info = Device::new(self.cur);
			self.cur = (*self.cur).next;

			Some(info)
		}
	}
}

impl<'a> Drop for Devices<'a> {
	fn drop(&mut self) {
		unsafe {
			hid_free_enumeration(self.ptr);
		}
	}
}