From 400e82326d12e3ec20abd9aa1b4a5edeee63388d Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Sun, 15 Jan 2023 11:52:12 +0000 Subject: [PATCH] memory: Fix undefined behaviour As warned by the compiler. At the same time drop support for older rustc, as the code was dead anyway. --- src/keyboard.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/keyboard.rs b/src/keyboard.rs index 8c3f5d35..12acfc91 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -143,18 +143,17 @@ type SingleKeyMap = [Option; 256]; fn single_key_map_new() -> SingleKeyMap { // Why can't we just initialize arrays without tricks -_- ? - unsafe { - // Inspired by - // https://www.reddit.com/r/rust/comments/5n7bh1/how_to_create_an_array_of_a_type_with_clone_but/ - #[cfg(feature = "rustc_less_1_36")] - let mut array: SingleKeyMap = mem::uninitialized(); - #[cfg(not(feature = "rustc_less_1_36"))] - let mut array: SingleKeyMap = mem::MaybeUninit::uninit().assume_init(); + // Inspired by + // https://www.reddit.com/r/rust/comments/5n7bh1/how_to_create_an_array_of_a_type_with_clone_but/ + let mut array = mem::MaybeUninit::::uninit(); - for element in array.iter_mut() { + unsafe { + let arref = &mut *array.as_mut_ptr(); + for element in arref.iter_mut() { ptr::write(element, None); } - array + + array.assume_init() } }