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
//! Event codes sent by devices
//!
//! Those codes are used for axis and button mapping.

use std::fmt::Debug;
use std::hash::Hash;
use std::ops::AddAssign;

pub use self::absolute_axis::AbsoluteAxis;
pub use self::key::Key;
use std::marker::PhantomData;

mod absolute_axis;
mod key;

/// Trait common to all event codes
pub trait EventCode<T>
where
	Self: Clone + Copy + Debug + Default + Eq + Hash + Sized,
	T: EventCodeValue,
{
	/// Code count
	const COUNT: T;
	/// Maximum value
	const MAX: T;

	/// Return the default event code iterator
	fn iter() -> IntoIter<Self, T> {
		IntoIter {
			phantom: PhantomData,
			value: unsafe { *(&Self::default() as *const Self as *const T) },
		}
	}
}

/// Trait common to all event code values
pub trait EventCodeValue
where
	Self: AddAssign + Clone + Copy + Debug + Eq + Hash + Ord + Sized,
{
	/// Value of one
	const ONE: Self;
}

impl EventCodeValue for u8 {
	const ONE: Self = 1u8;
}

impl EventCodeValue for u16 {
	const ONE: Self = 1u16;
}

/// Event code iterator
pub struct IntoIter<T, U>
where
	T: EventCode<U>,
	U: EventCodeValue,
{
	pub(crate) phantom: std::marker::PhantomData<T>,
	pub(crate) value: U,
}

impl<T, U> Iterator for IntoIter<T, U>
where
	T: EventCode<U>,
	U: EventCodeValue,
{
	type Item = T;

	fn next(&mut self) -> Option<Self::Item> {
		if self.value >= T::MAX {
			None
		} else {
			let result = Some(unsafe { *(&self.value as *const U as *const T) });
			self.value += U::ONE;
			result
		}
	}
}