Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(dead_code)]
- #[derive(PartialEq, Debug)]
- enum Event {}
- #[derive(PartialEq, Debug)]
- enum EventStatus {}
- struct Input<'a, T: USBKeyOut> {
- events: Vec<(Event, EventStatus)>,
- handlers: &'a mut [Box<dyn ProcessKeys<T>>],
- output: T,
- }
- impl<T: USBKeyOut> Input<'_, T> {
- fn new(handlers: &mut [Box<dyn ProcessKeys<T>>], output: T) -> Input<T> {
- Input {
- events: Vec::new(),
- handlers,
- output,
- }
- }
- fn handle_keys(&mut self) -> Result<(), String> {
- Ok(())
- }
- }
- trait ProcessKeys<T: USBKeyOut> {
- fn process_keys(&mut self, events: &mut Vec<(Event, EventStatus)>, output: &mut T) -> ();
- }
- trait USBKeyOut {}
- struct ToggleMacro<'a, T, F1: FnMut(&mut T)> {
- keycode: u32,
- on_toggle_on: F1,
- state: bool,
- phantom: core::marker::PhantomData<&'a T>,
- }
- impl<'a, T: USBKeyOut, F1: FnMut(&mut T)> ToggleMacro<'a, T, F1> {
- fn new(trigger: u32, on_toggle_on: F1) -> ToggleMacro<'a, T, F1> {
- ToggleMacro {
- keycode: trigger,
- on_toggle_on,
- state: false,
- phantom: core::marker::PhantomData,
- }
- }
- }
- impl<T: USBKeyOut, F1: FnMut(&mut T)> ProcessKeys<T>
- for ToggleMacro<'_, T, F1>
- {
- fn process_keys(&mut self, _events: &mut Vec<(Event, EventStatus)>, output: &mut T) -> () {
- (self.on_toggle_on)(output);
- }
- }
- #[cfg(test)]
- #[macro_use]
- extern crate std;
- mod tests {
- #[allow(unused_imports)]
- use crate::{Event, Input, ProcessKeys, ToggleMacro, USBKeyOut};
- struct KeyOutCatcher {
- keys_registered: Vec<u8>,
- reports: Vec<Vec<u8>>,
- }
- impl KeyOutCatcher {
- fn new() -> KeyOutCatcher {
- KeyOutCatcher {
- keys_registered: Vec::new(),
- reports: Vec::new(),
- }
- }
- }
- impl USBKeyOut for KeyOutCatcher {}
- #[test]
- fn test_toggle_macro() {
- use core::cell::RefCell;
- let down_counter = RefCell::new(0);
- let _up_counter = RefCell::new(0);
- let t = ToggleMacro::new(
- 0xF0000u32,
- |_output: &mut KeyOutCatcher| {
- let mut dc = down_counter.borrow_mut();
- *dc += 1;
- },
- );
- let mut h = [Box::new(t) as Box<dyn ProcessKeys<KeyOutCatcher>>];
- let mut input = Input::new(&mut h, KeyOutCatcher::new());
- input.handle_keys().unwrap();
- assert!(*down_counter.borrow() == 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement