Windows: Add #[deny(unsafe_op_in_unsafe_fn)] (#3070)

This commit is contained in:
Mads Marquart 2023-09-29 16:07:44 +02:00 committed by GitHub
parent cb58c49a90
commit b2b4564a5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 339 additions and 289 deletions

View file

@ -30,15 +30,15 @@ pub struct ImeContext {
impl ImeContext {
pub unsafe fn current(hwnd: HWND) -> Self {
let himc = ImmGetContext(hwnd);
let himc = unsafe { ImmGetContext(hwnd) };
ImeContext { hwnd, himc }
}
pub unsafe fn get_composing_text_and_cursor(
&self,
) -> Option<(String, Option<usize>, Option<usize>)> {
let text = self.get_composition_string(GCS_COMPSTR)?;
let attrs = self.get_composition_data(GCS_COMPATTR).unwrap_or_default();
let text = unsafe { self.get_composition_string(GCS_COMPSTR) }?;
let attrs = unsafe { self.get_composition_data(GCS_COMPATTR) }.unwrap_or_default();
let mut first = None;
let mut last = None;
@ -61,7 +61,7 @@ impl ImeContext {
last = Some(text.len());
} else if first.is_none() {
// IME haven't split words and select any clause yet, so trying to retrieve normal cursor.
let cursor = self.get_composition_cursor(&text);
let cursor = unsafe { self.get_composition_cursor(&text) };
first = cursor;
last = cursor;
}
@ -70,17 +70,17 @@ impl ImeContext {
}
pub unsafe fn get_composed_text(&self) -> Option<String> {
self.get_composition_string(GCS_RESULTSTR)
unsafe { self.get_composition_string(GCS_RESULTSTR) }
}
unsafe fn get_composition_cursor(&self, text: &str) -> Option<usize> {
let cursor = ImmGetCompositionStringW(self.himc, GCS_CURSORPOS, null_mut(), 0);
let cursor = unsafe { ImmGetCompositionStringW(self.himc, GCS_CURSORPOS, null_mut(), 0) };
(cursor >= 0).then(|| text.chars().take(cursor as _).map(|c| c.len_utf8()).sum())
}
unsafe fn get_composition_string(&self, gcs_mode: u32) -> Option<String> {
let data = self.get_composition_data(gcs_mode)?;
let (prefix, shorts, suffix) = data.align_to::<u16>();
let data = unsafe { self.get_composition_data(gcs_mode) }?;
let (prefix, shorts, suffix) = unsafe { data.align_to::<u16>() };
if prefix.is_empty() && suffix.is_empty() {
OsString::from_wide(shorts).into_string().ok()
} else {
@ -89,30 +89,32 @@ impl ImeContext {
}
unsafe fn get_composition_data(&self, gcs_mode: u32) -> Option<Vec<u8>> {
let size = match ImmGetCompositionStringW(self.himc, gcs_mode, null_mut(), 0) {
let size = match unsafe { ImmGetCompositionStringW(self.himc, gcs_mode, null_mut(), 0) } {
0 => return Some(Vec::new()),
size if size < 0 => return None,
size => size,
};
let mut buf = Vec::<u8>::with_capacity(size as _);
let size = ImmGetCompositionStringW(
self.himc,
gcs_mode,
buf.as_mut_ptr() as *mut c_void,
size as _,
);
let size = unsafe {
ImmGetCompositionStringW(
self.himc,
gcs_mode,
buf.as_mut_ptr() as *mut c_void,
size as _,
)
};
if size < 0 {
None
} else {
buf.set_len(size as _);
unsafe { buf.set_len(size as _) };
Some(buf)
}
}
pub unsafe fn set_ime_cursor_area(&self, spot: Position, size: Size, scale_factor: f64) {
if !ImeContext::system_has_ime() {
if !unsafe { ImeContext::system_has_ime() } {
return;
}
@ -131,23 +133,23 @@ impl ImeContext {
rcArea: rc_area,
};
ImmSetCandidateWindow(self.himc, &candidate_form);
unsafe { ImmSetCandidateWindow(self.himc, &candidate_form) };
}
pub unsafe fn set_ime_allowed(hwnd: HWND, allowed: bool) {
if !ImeContext::system_has_ime() {
if !unsafe { ImeContext::system_has_ime() } {
return;
}
if allowed {
ImmAssociateContextEx(hwnd, 0, IACE_DEFAULT);
unsafe { ImmAssociateContextEx(hwnd, 0, IACE_DEFAULT) };
} else {
ImmAssociateContextEx(hwnd, 0, IACE_CHILDREN);
unsafe { ImmAssociateContextEx(hwnd, 0, IACE_CHILDREN) };
}
}
unsafe fn system_has_ime() -> bool {
GetSystemMetrics(SM_IMMENABLED) != 0
unsafe { GetSystemMetrics(SM_IMMENABLED) != 0 }
}
}