remove some unnecessary unsafe code

This commit is contained in:
anteater
2021-05-10 20:56:29 +00:00
parent 99f062fe31
commit f71e769315

View File

@ -11,7 +11,7 @@ use std::iter::FromIterator;
// and what a convenience layout. "_wide" is not a layout, // and what a convenience layout. "_wide" is not a layout,
// neither is "number" // neither is "number"
/// List of builtin layouts /// List of builtin layouts
const KEYBOARDS: &[(*const str, *const str)] = &[ static KEYBOARDS: &[(&'static str, &'static str)] = &[
// layouts: us must be left as first, as it is the, // layouts: us must be left as first, as it is the,
// fallback layout. // fallback layout.
("us", include_str!("../data/keyboards/us.yaml")), ("us", include_str!("../data/keyboards/us.yaml")),
@ -93,34 +93,20 @@ const KEYBOARDS: &[(*const str, *const str)] = &[
]; ];
pub fn get_keyboard(needle: &str) -> Option<&'static str> { pub fn get_keyboard(needle: &str) -> Option<&'static str> {
// Need to dereference in unsafe code KEYBOARDS.iter().find(|(name, _)| *name == needle).map(|(_, layout)| *layout)
// comparing *const str to &str will compare pointers
KEYBOARDS.iter()
.find(|(name, _)| {
let name: *const str = *name;
(unsafe { &*name }) == needle
})
.map(|(_, value)| {
let value: *const str = *value;
unsafe { &*value }
})
} }
const OVERLAY_NAMES: &[*const str] = &[ static OVERLAY_NAMES: &[&'static str] = &[
"emoji", "emoji",
"terminal", "terminal",
]; ];
pub fn get_overlays() -> Vec<&'static str> { pub fn get_overlays() -> Vec<&'static str> {
OVERLAY_NAMES.iter() OVERLAY_NAMES.to_vec()
.map(|name| {
let name: *const str = *name;
unsafe { &*name }
}).collect()
} }
/// Translations of the layout identifier strings /// Translations of the layout identifier strings
const LAYOUT_NAMES: &[(*const str, *const str)] = &[ static LAYOUT_NAMES: &[(&'static str, &'static str)] = &[
("de-DE", include_str!("../data/langs/de-DE.txt")), ("de-DE", include_str!("../data/langs/de-DE.txt")),
("en-US", include_str!("../data/langs/en-US.txt")), ("en-US", include_str!("../data/langs/en-US.txt")),
("es-ES", include_str!("../data/langs/es-ES.txt")), ("es-ES", include_str!("../data/langs/es-ES.txt")),
@ -135,14 +121,8 @@ pub fn get_layout_names(lang: &str)
-> Option<HashMap<&'static str, Translation<'static>>> -> Option<HashMap<&'static str, Translation<'static>>>
{ {
let translations = LAYOUT_NAMES.iter() let translations = LAYOUT_NAMES.iter()
.find(|(name, _data)| { .find(|(name, _data)| *name == lang)
let name: *const str = *name; .map(|(_name, data)| *data);
(unsafe { &*name }) == lang
})
.map(|(_name, data)| {
let data: *const str = *data;
unsafe { &*data }
});
translations.map(make_mapping) translations.map(make_mapping)
} }