Skip to content

Commit

Permalink
Make Keycode internal value private
Browse files Browse the repository at this point in the history
  • Loading branch information
hvox committed Mar 14, 2024
1 parent 7f4b7c9 commit 42b6db6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/sdl2/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ where
.unwrap_or(sys::SDL_Scancode::SDL_SCANCODE_UNKNOWN);
let keycode = keycode
.into()
.map(|kc| kc.0 as sys::SDL_Keycode)
.map(|kc| kc.into())
.unwrap_or(sys::SDL_KeyCode::SDLK_UNKNOWN as i32);
let keymod = keymod.bits() as u16;
sys::SDL_Keysym {
Expand Down
24 changes: 22 additions & 2 deletions src/sdl2/keyboard/keycode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::mem::transmute;
use crate::sys;

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct Keycode(pub i32);
pub struct Keycode(i32);

#[allow(non_upper_case_globals)]
impl Keycode {
Expand Down Expand Up @@ -249,6 +249,10 @@ impl Keycode {
}

impl Keycode {
pub fn into_i32(&self) -> i32 {
return self.0;
}

pub fn from_i32(n: i32) -> Option<Keycode> {
if n != 0 {
return Some(Keycode(n));
Expand All @@ -265,6 +269,22 @@ impl fmt::Display for Keycode {
}
}

use std::ops::Deref;

impl Deref for Keycode {
type Target = i32;

fn deref(&self) -> &i32 {
&self.0
}
}

impl Into<i32> for Keycode {
fn into(self) -> i32 {
self.into_i32()
}
}

use crate::keyboard::Scancode;

impl Keycode {
Expand Down Expand Up @@ -301,7 +321,7 @@ impl Keycode {
// The name string pointer's contents _might_ change, depending on the last call to SDL_GetKeyName.
// Knowing this, we must always return a new string.
unsafe {
let buf = sys::SDL_GetKeyName(self.0);
let buf = sys::SDL_GetKeyName(self.into());
CStr::from_ptr(buf as *const _).to_str().unwrap().to_owned()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/sdl2/keyboard/scancode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ impl Scancode {
#[doc(alias = "SDL_GetScancodeFromKey")]
pub fn from_keycode(keycode: Keycode) -> Option<Scancode> {
unsafe {
match sys::SDL_GetScancodeFromKey(keycode.0) {
match sys::SDL_GetScancodeFromKey(keycode.into()) {
SDL_Scancode::SDL_SCANCODE_UNKNOWN => None,
scancode_id => Scancode::from_i32(scancode_id as i32),
}
Expand Down

0 comments on commit 42b6db6

Please sign in to comment.