mirror of
https://github.com/rust-windowing/winit.git
synced 2026-08-29 04:40:04 -04:00
Fix MonitorHandle PartialEq and Hash on iOS (#4013)
This commit is contained in:
@@ -213,3 +213,4 @@ changelog entry.
|
|||||||
- On macOS, fixed the scancode conversion for `IntlBackslash`.
|
- On macOS, fixed the scancode conversion for `IntlBackslash`.
|
||||||
- On macOS, fixed redundant `SurfaceResized` event at window creation.
|
- On macOS, fixed redundant `SurfaceResized` event at window creation.
|
||||||
- On macOS, fix crash when pressing Caps Lock in certain configurations.
|
- On macOS, fix crash when pressing Caps Lock in certain configurations.
|
||||||
|
- On iOS, fixed `MonitorHandle`'s `PartialEq` and `Hash` implementations.
|
||||||
|
|||||||
@@ -101,13 +101,20 @@ impl Clone for MonitorHandle {
|
|||||||
|
|
||||||
impl hash::Hash for MonitorHandle {
|
impl hash::Hash for MonitorHandle {
|
||||||
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
||||||
(self as *const Self).hash(state);
|
// SAFETY: Only getting the pointer.
|
||||||
|
let mtm = unsafe { MainThreadMarker::new_unchecked() };
|
||||||
|
Retained::as_ptr(self.ui_screen.get(mtm)).hash(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for MonitorHandle {
|
impl PartialEq for MonitorHandle {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
ptr::eq(self, other)
|
// SAFETY: Only getting the pointer.
|
||||||
|
let mtm = unsafe { MainThreadMarker::new_unchecked() };
|
||||||
|
ptr::eq(
|
||||||
|
Retained::as_ptr(self.ui_screen.get(mtm)),
|
||||||
|
Retained::as_ptr(other.ui_screen.get(mtm)),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,8 +128,10 @@ impl PartialOrd for MonitorHandle {
|
|||||||
|
|
||||||
impl Ord for MonitorHandle {
|
impl Ord for MonitorHandle {
|
||||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||||
|
// SAFETY: Only getting the pointer.
|
||||||
// TODO: Make a better ordering
|
// TODO: Make a better ordering
|
||||||
(self as *const Self).cmp(&(other as *const Self))
|
let mtm = unsafe { MainThreadMarker::new_unchecked() };
|
||||||
|
Retained::as_ptr(self.ui_screen.get(mtm)).cmp(&Retained::as_ptr(other.ui_screen.get(mtm)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,3 +249,27 @@ pub fn uiscreens(mtm: MainThreadMarker) -> VecDeque<MonitorHandle> {
|
|||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
UIScreen::screens(mtm).into_iter().map(MonitorHandle::new).collect()
|
UIScreen::screens(mtm).into_iter().map(MonitorHandle::new).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use objc2_foundation::NSSet;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
// Test that UIScreen pointer comparisons are correct.
|
||||||
|
#[test]
|
||||||
|
#[allow(deprecated)]
|
||||||
|
fn screen_comparisons() {
|
||||||
|
// Test code, doesn't matter that it's not thread safe
|
||||||
|
let mtm = unsafe { MainThreadMarker::new_unchecked() };
|
||||||
|
|
||||||
|
assert!(ptr::eq(&*UIScreen::mainScreen(mtm), &*UIScreen::mainScreen(mtm)));
|
||||||
|
|
||||||
|
let main = UIScreen::mainScreen(mtm);
|
||||||
|
assert!(UIScreen::screens(mtm).iter().any(|screen| ptr::eq(screen, &*main)));
|
||||||
|
|
||||||
|
assert!(unsafe {
|
||||||
|
NSSet::setWithArray(&UIScreen::screens(mtm)).containsObject(&UIScreen::mainScreen(mtm))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user