Skip to content

Commit

Permalink
Add more specific error RequestIgnored
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Feb 9, 2024
1 parent 4112fcc commit 2443baa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Unreleased` header.

# Unreleased

- **Breaking:** Use `RequestIgnored` as the error type in `InnerSizeWriter::request_inner_size`.
- Fix compatibility with 32-bit platforms without 64-bit atomics.
- On X11, fix swapped instance and general class names.
- **Breaking:** Removed unnecessary generic parameter `T` from `EventLoopWindowTarget`.
Expand Down
32 changes: 28 additions & 4 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
//!
//! [`EventLoop::run(...)`]: crate::event_loop::EventLoop::run
//! [`ControlFlow::WaitUntil`]: crate::event_loop::ControlFlow::WaitUntil
use std::error::Error;
use std::fmt;
use std::path::PathBuf;
use std::sync::{Mutex, Weak};
#[cfg(not(web_platform))]
Expand All @@ -43,7 +45,6 @@ use smol_str::SmolStr;
#[cfg(web_platform)]
use web_time::Instant;

use crate::error::ExternalError;
#[cfg(doc)]
use crate::window::Window;
use crate::{
Expand Down Expand Up @@ -1126,16 +1127,23 @@ impl InnerSizeWriter {
Self { new_inner_size }
}

/// Try to request inner size which will be set synchroniously on the window.
/// Try to request a new inner size which will be set synchronously on the
/// window.
///
///
/// # Errors
///
/// This method returns an error when the request was ignored because it
/// was done asynchronously, outside the event loop callback.
pub fn request_inner_size(
&mut self,
new_inner_size: PhysicalSize<u32>,
) -> Result<(), ExternalError> {
) -> Result<(), RequestIgnored> {
if let Some(inner) = self.new_inner_size.upgrade() {
*inner.lock().unwrap() = new_inner_size;
Ok(())
} else {
Err(ExternalError::Ignored)
Err(RequestIgnored { _priv: () })
}
}
}
Expand All @@ -1146,6 +1154,22 @@ impl PartialEq for InnerSizeWriter {
}
}

/// The request to change the inner size synchronously was ignored.
///
/// See [`InnerSizeWriter::request_inner_size`] for details.
#[derive(Debug, Clone)] // Explicitly not other traits, in case we want to extend it in the future
pub struct RequestIgnored {
_priv: (),
}

impl fmt::Display for RequestIgnored {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.write_str("the request was ignored")
}
}

impl Error for RequestIgnored {}

#[cfg(test)]
mod tests {
use crate::event;
Expand Down

0 comments on commit 2443baa

Please sign in to comment.