Skip to content

Commit

Permalink
Replace WindowId From/Into u64 with methods
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Aug 13, 2024
1 parent 7fbc211 commit e429f56
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 78 deletions.
3 changes: 3 additions & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ changelog entry.
- Implement `Clone`, `Copy`, `Debug`, `Deserialize`, `Eq`, `Hash`, `Ord`, `PartialEq`, `PartialOrd`
and `Serialize` on many types.
- Add `MonitorHandle::current_video_mode()`.
- Add `WindowId::into_raw()` and `from_raw()`.

### Changed

Expand Down Expand Up @@ -127,6 +128,8 @@ changelog entry.
- Remove `MonitorHandle::size()` and `refresh_rate_millihertz()` in favor of
`MonitorHandle::current_video_mode()`.
- On Android, remove all `MonitorHandle` support instead of emitting false data.
- Remove `impl From<u64> for WindowId` and `impl From<WindowId> for u64`. Replaced with
`WindowId::into_raw()` and `from_raw()`.

### Fixed

Expand Down
8 changes: 2 additions & 6 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,16 +673,12 @@ impl WindowId {
pub const fn dummy() -> Self {
WindowId
}
}

impl From<WindowId> for u64 {
fn from(_: WindowId) -> Self {
pub const fn into_raw(self) -> u64 {
0
}
}

impl From<u64> for WindowId {
fn from(_: u64) -> Self {
pub const fn from_raw(_id: u64) -> Self {
Self
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/platform_impl/apple/appkit/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,13 @@ impl WindowId {
pub const fn dummy() -> Self {
Self(0)
}
}

impl From<WindowId> for u64 {
fn from(window_id: WindowId) -> Self {
window_id.0 as u64
pub const fn into_raw(self) -> u64 {
self.0 as u64
}
}

impl From<u64> for WindowId {
fn from(raw_id: u64) -> Self {
Self(raw_id as usize)
pub const fn from_raw(id: u64) -> Self {
Self(id as usize)
}
}

Expand Down
14 changes: 5 additions & 9 deletions src/platform_impl/apple/uikit/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl WinitUIWindow {
}

pub(crate) fn id(&self) -> WindowId {
(self as *const Self as usize as u64).into()
WindowId { window: (self as *const Self as usize) as _ }
}
}

Expand Down Expand Up @@ -679,17 +679,13 @@ impl WindowId {
pub const fn dummy() -> Self {
WindowId { window: std::ptr::null_mut() }
}
}

impl From<WindowId> for u64 {
fn from(window_id: WindowId) -> Self {
window_id.window as u64
pub fn into_raw(self) -> u64 {
self.window as u64
}
}

impl From<u64> for WindowId {
fn from(raw_id: u64) -> Self {
Self { window: raw_id as _ }
pub const fn from_raw(id: u64) -> Self {
Self { window: id as *mut WinitUIWindow }
}
}

Expand Down
18 changes: 7 additions & 11 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,17 @@ pub(crate) enum Window {
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WindowId(u64);

impl From<WindowId> for u64 {
fn from(window_id: WindowId) -> Self {
window_id.0
impl WindowId {
pub const fn dummy() -> Self {
Self(0)
}
}

impl From<u64> for WindowId {
fn from(raw_id: u64) -> Self {
Self(raw_id)
pub const fn into_raw(self) -> u64 {
self.0
}
}

impl WindowId {
pub const fn dummy() -> Self {
Self(0)
pub const fn from_raw(id: u64) -> Self {
Self(id)
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/platform_impl/orbital/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,13 @@ impl WindowId {
pub const fn dummy() -> Self {
WindowId { fd: u64::MAX }
}
}

impl From<WindowId> for u64 {
fn from(id: WindowId) -> Self {
id.fd
pub const fn into_raw(self) -> u64 {
self.fd
}
}

impl From<u64> for WindowId {
fn from(fd: u64) -> Self {
Self { fd }
pub const fn from_raw(id: u64) -> Self {
Self { fd: id }
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct Execution {
suspended: Cell<bool>,
event_loop_recreation: Cell<bool>,
events: RefCell<VecDeque<EventWrapper>>,
id: RefCell<u32>,
id: Cell<u64>,
window: web_sys::Window,
navigator: Navigator,
document: Document,
Expand Down Expand Up @@ -171,7 +171,7 @@ impl Shared {
window,
navigator,
document,
id: RefCell::new(0),
id: Cell::new(0),
all_canvases: RefCell::new(Vec::new()),
redraw_pending: RefCell::new(HashSet::new()),
destroy_pending: RefCell::new(VecDeque::new()),
Expand Down Expand Up @@ -438,11 +438,11 @@ impl Shared {

// Generate a strictly increasing ID
// This is used to differentiate windows when handling events
pub fn generate_id(&self) -> u32 {
let mut id = self.0.id.borrow_mut();
*id += 1;
pub fn generate_id(&self) -> u64 {
let id = self.0.id.get();
self.0.id.set(id.checked_add(1).expect("exhausted `WindowId`"));

*id
id
}

pub fn request_redraw(&self, id: WindowId) {
Expand Down
14 changes: 5 additions & 9 deletions src/platform_impl/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,23 +429,19 @@ impl Drop for Inner {
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WindowId(pub(crate) u32);
pub struct WindowId(pub(crate) u64);

impl WindowId {
pub const fn dummy() -> Self {
Self(0)
}
}

impl From<WindowId> for u64 {
fn from(window_id: WindowId) -> Self {
window_id.0 as u64
pub const fn into_raw(self) -> u64 {
self.0
}
}

impl From<u64> for WindowId {
fn from(raw_id: u64) -> Self {
Self(raw_id as u32)
pub const fn from_raw(id: u64) -> Self {
Self(id)
}
}

Expand Down
16 changes: 6 additions & 10 deletions src/platform_impl/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ impl WindowId {
pub const fn dummy() -> Self {
WindowId(0)
}
}

impl From<WindowId> for u64 {
fn from(window_id: WindowId) -> Self {
window_id.0 as u64
pub const fn into_raw(self) -> u64 {
self.0 as u64
}

pub const fn from_raw(id: u64) -> Self {
Self(id as HWND)
}
}

Expand All @@ -134,12 +136,6 @@ impl From<WindowId> for HWND {
}
}

impl From<u64> for WindowId {
fn from(raw_id: u64) -> Self {
Self(raw_id as HWND)
}
}

#[inline(always)]
const fn get_xbutton_wparam(x: u32) -> u16 {
hiword(x)
Expand Down
18 changes: 7 additions & 11 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,19 @@ impl WindowId {
pub const fn dummy() -> Self {
WindowId(platform_impl::WindowId::dummy())
}
}

impl fmt::Debug for WindowId {
fn fmt(&self, fmtr: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(fmtr)
pub fn into_raw(self) -> u64 {
self.0.into_raw()
}
}

impl From<WindowId> for u64 {
fn from(window_id: WindowId) -> Self {
window_id.0.into()
pub const fn from_raw(id: u64) -> Self {
Self(platform_impl::WindowId::from_raw(id))
}
}

impl From<u64> for WindowId {
fn from(raw_id: u64) -> Self {
Self(raw_id.into())
impl fmt::Debug for WindowId {
fn fmt(&self, fmtr: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(fmtr)
}
}

Expand Down

0 comments on commit e429f56

Please sign in to comment.