From deb235507f2febbae269b2093e30504d2b3244ba Mon Sep 17 00:00:00 2001 From: Elijah Hartvigsen <35054042+Zij-IT@users.noreply.github.com> Date: Sat, 23 Oct 2021 11:39:18 +0200 Subject: [PATCH] Changing return types of get_keys from Option> to Vec (#260) * KeyHandler: Updated return type to Vec on get_keys * Window: Updated Window structs get_keys return type across all currently supported OS's * Updated return type of get_keys of Window, and updated the docs for all related functions * Docs: Corrected incorrect variable ident in docs post update * Resolved error resulting from get_keys return type change * Formatting: Ran cargo fmt Co-authored-by: Zij-IT --- examples/menu.rs | 12 +++++------- examples/noise.rs | 24 ++++++++---------------- src/key_handler.rs | 12 ++++++------ src/lib.rs | 30 ++++++++++++------------------ src/os/macos/mod.rs | 6 +++--- src/os/posix/mod.rs | 6 +++--- src/os/posix/wayland.rs | 6 +++--- src/os/posix/x11.rs | 6 +++--- src/os/redox/mod.rs | 6 +++--- src/os/windows/mod.rs | 6 +++--- 10 files changed, 49 insertions(+), 65 deletions(-) diff --git a/examples/menu.rs b/examples/menu.rs index 44f8d5a..772b6ba 100644 --- a/examples/menu.rs +++ b/examples/menu.rs @@ -100,13 +100,11 @@ fn main() { println!("Menu id {} pressed", menu_id); }); - window.get_keys().map(|keys| { - for t in keys { - match t { - Key::W => println!("holding w!"), - Key::T => println!("holding t!"), - _ => (), - } + window.get_keys().iter().for_each(|key| { + match key { + Key::W => println!("holding w!"), + Key::T => println!("holding t!"), + _ => (), } }); diff --git a/examples/noise.rs b/examples/noise.rs index cea69f5..ce4d21a 100644 --- a/examples/noise.rs +++ b/examples/noise.rs @@ -46,24 +46,16 @@ fn main() { *i = (noise << 16) | (noise << 8) | noise; } - window.get_keys().map(|keys| { - for t in keys { - match t { - Key::W => println!("holding w!"), - Key::T => println!("holding t!"), - _ => (), - } - } + window.get_keys().iter().for_each(|key| match key { + Key::W => println!("holding w!"), + Key::T => println!("holding t!"), + _ => (), }); - window.get_keys_released().map(|keys| { - for t in keys { - match t { - Key::W => println!("released w!"), - Key::T => println!("released t!"), - _ => (), - } - } + window.get_keys_released().iter().for_each(|key| match key { + Key::W => println!("released w!"), + Key::T => println!("released t!"), + _ => (), }); window diff --git a/src/key_handler.rs b/src/key_handler.rs index 172079f..40142bd 100644 --- a/src/key_handler.rs +++ b/src/key_handler.rs @@ -32,7 +32,7 @@ impl KeyHandler { self.keys[key as usize] = state; } - pub fn get_keys(&self) -> Option> { + pub fn get_keys(&self) -> Vec { let mut index: u16 = 0; let mut keys: Vec = Vec::new(); @@ -46,7 +46,7 @@ impl KeyHandler { index += 1; } - Some(keys) + keys } pub fn update(&mut self) { @@ -73,7 +73,7 @@ impl KeyHandler { self.key_callback = Some(callback); } - pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Option> { + pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Vec { let mut index: u16 = 0; let mut keys: Vec = Vec::new(); @@ -89,10 +89,10 @@ impl KeyHandler { index += 1; } - Some(keys) + keys } - pub fn get_keys_released(&self) -> Option> { + pub fn get_keys_released(&self) -> Vec { let mut keys: Vec = Vec::new(); for (idx, is_down) in self.keys.iter().enumerate() { @@ -103,7 +103,7 @@ impl KeyHandler { } } - Some(keys) + keys } #[inline] diff --git a/src/lib.rs b/src/lib.rs index 834614b..ac4e3e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -559,18 +559,16 @@ impl Window { /// ```no_run /// # use minifb::*; /// # let mut window = Window::new("Test", 640, 400, WindowOptions::default()).unwrap(); - /// window.get_keys().map(|keys| { - /// for t in keys { - /// match t { + /// window.get_keys().iter().for_each(|key| + /// match key { /// Key::W => println!("holding w"), /// Key::T => println!("holding t"), /// _ => (), /// } - /// } - /// }); + /// ); /// ``` #[inline] - pub fn get_keys(&self) -> Option> { + pub fn get_keys(&self) -> Vec { self.0.get_keys() } @@ -583,18 +581,16 @@ impl Window { /// ```no_run /// # use minifb::*; /// # let mut window = Window::new("Test", 640, 400, WindowOptions::default()).unwrap(); - /// window.get_keys_pressed(KeyRepeat::No).map(|keys| { - /// for t in keys { - /// match t { + /// window.get_keys_pressed(KeyRepeat::No).iter().for_each(|key| + /// match key { /// Key::W => println!("pressed w"), /// Key::T => println!("pressed t"), /// _ => (), /// } - /// } - /// }); + /// ); /// ``` #[inline] - pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Option> { + pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Vec { self.0.get_keys_pressed(repeat) } @@ -606,18 +602,16 @@ impl Window { /// ```no_run /// # use minifb::*; /// # let mut window = Window::new("Test", 640, 400, WindowOptions::default()).unwrap(); - /// window.get_keys_released().map(|keys| { - /// for t in keys { - /// match t { + /// window.get_keys_released().iter().for_each(|key| + /// match key { /// Key::W => println!("released w"), /// Key::T => println!("released t"), /// _ => (), /// } - /// } - /// }); + /// ); /// ``` #[inline] - pub fn get_keys_released(&self) -> Option> { + pub fn get_keys_released(&self) -> Vec { self.0.get_keys_released() } diff --git a/src/os/macos/mod.rs b/src/os/macos/mod.rs index 6ec1d05..577260b 100644 --- a/src/os/macos/mod.rs +++ b/src/os/macos/mod.rs @@ -479,17 +479,17 @@ impl Window { } #[inline] - pub fn get_keys(&self) -> Option> { + pub fn get_keys(&self) -> Vec { self.key_handler.get_keys() } #[inline] - pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Option> { + pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Vec { self.key_handler.get_keys_pressed(repeat) } #[inline] - pub fn get_keys_released(&self) -> Option> { + pub fn get_keys_released(&self) -> Vec { self.key_handler.get_keys_released() } diff --git a/src/os/posix/mod.rs b/src/os/posix/mod.rs index 1309867..c546e7e 100644 --- a/src/os/posix/mod.rs +++ b/src/os/posix/mod.rs @@ -206,7 +206,7 @@ impl Window { } } - pub fn get_keys(&self) -> Option> { + pub fn get_keys(&self) -> Vec { match *self { #[cfg(feature = "x11")] Window::X11(ref w) => w.get_keys(), @@ -215,7 +215,7 @@ impl Window { } } - pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Option> { + pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Vec { match *self { #[cfg(feature = "x11")] Window::X11(ref w) => w.get_keys_pressed(repeat), @@ -224,7 +224,7 @@ impl Window { } } - pub fn get_keys_released(&self) -> Option> { + pub fn get_keys_released(&self) -> Vec { match *self { #[cfg(feature = "x11")] Window::X11(ref w) => w.get_keys_released(), diff --git a/src/os/posix/wayland.rs b/src/os/posix/wayland.rs index d2ff17d..811b329 100644 --- a/src/os/posix/wayland.rs +++ b/src/os/posix/wayland.rs @@ -588,15 +588,15 @@ impl Window { (self.width as usize, self.height as usize) } - pub fn get_keys(&self) -> Option> { + pub fn get_keys(&self) -> Vec { self.key_handler.get_keys() } - pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Option> { + pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Vec { self.key_handler.get_keys_pressed(repeat) } - pub fn get_keys_released(&self) -> Option> { + pub fn get_keys_released(&self) -> Vec { self.key_handler.get_keys_released() } diff --git a/src/os/posix/x11.rs b/src/os/posix/x11.rs index 462b288..6c64200 100644 --- a/src/os/posix/x11.rs +++ b/src/os/posix/x11.rs @@ -674,17 +674,17 @@ impl Window { } #[inline] - pub fn get_keys(&self) -> Option> { + pub fn get_keys(&self) -> Vec { self.key_handler.get_keys() } #[inline] - pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Option> { + pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Vec { self.key_handler.get_keys_pressed(repeat) } #[inline] - pub fn get_keys_released(&self) -> Option> { + pub fn get_keys_released(&self) -> Vec { self.key_handler.get_keys_released() } diff --git a/src/os/redox/mod.rs b/src/os/redox/mod.rs index 1a21969..62d4a08 100644 --- a/src/os/redox/mod.rs +++ b/src/os/redox/mod.rs @@ -188,15 +188,15 @@ impl Window { self.window.set_mouse_cursor(visibility); } - pub fn get_keys(&self) -> Option> { + pub fn get_keys(&self) -> Vec { self.key_handler.get_keys() } - pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Option> { + pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Vec { self.key_handler.get_keys_pressed(repeat) } - pub fn get_keys_released(&self) -> Option> { + pub fn get_keys_released(&self) -> Vec { self.key_handler.get_keys_released() } diff --git a/src/os/windows/mod.rs b/src/os/windows/mod.rs index 64c2bca..4b0d3f6 100644 --- a/src/os/windows/mod.rs +++ b/src/os/windows/mod.rs @@ -719,17 +719,17 @@ impl Window { } #[inline] - pub fn get_keys(&self) -> Option> { + pub fn get_keys(&self) -> Vec { self.key_handler.get_keys() } #[inline] - pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Option> { + pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Vec { self.key_handler.get_keys_pressed(repeat) } #[inline] - pub fn get_keys_released(&self) -> Option> { + pub fn get_keys_released(&self) -> Vec { self.key_handler.get_keys_released() }