Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly implement Debug for Window and WindowBuilder #3297

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::{
cell::Cell,
collections::VecDeque,
fmt,
hash::Hash,
sync::{
atomic::{AtomicBool, Ordering},
Expand Down Expand Up @@ -75,7 +76,7 @@ impl<T> PeekableReceiver<T> {
}
}

#[derive(Clone)]
#[derive(Debug, Clone)]
struct SharedFlagSetter {
flag: Arc<AtomicBool>,
}
Expand Down Expand Up @@ -117,6 +118,15 @@ pub struct RedrawRequester {
waker: AndroidAppWaker,
}

// TODO: Derive this once `AndroidAppWaker` implements Debug
impl fmt::Debug for RedrawRequester {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RedrawRequester")
.field("flag", &self.flag)
.finish_non_exhaustive()
}
}

impl RedrawRequester {
fn new(flag: &SharedFlag, waker: AndroidAppWaker) -> Self {
RedrawRequester {
Expand Down Expand Up @@ -751,6 +761,7 @@ impl DeviceId {
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PlatformSpecificWindowBuilderAttributes;

#[derive(Debug)]
pub(crate) struct Window {
app: AndroidApp,
redraw_requester: RedrawRequester,
Expand Down Expand Up @@ -1026,9 +1037,8 @@ impl Window {
#[derive(Default, Clone, Debug)]
pub struct OsError;

use std::fmt::{self, Display, Formatter};
impl Display for OsError {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), fmt::Error> {
impl fmt::Display for OsError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "Android OS Error")
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/platform_impl/ios/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ impl Inner {
}
}

#[derive(Debug)]
pub struct Window {
inner: MainThreadBound<Inner>,
}
Expand Down Expand Up @@ -674,7 +675,7 @@ impl From<&AnyObject> for WindowId {
}
}

#[derive(Clone, Default)]
#[derive(Debug, Clone, Default)]
pub struct PlatformSpecificWindowBuilderAttributes {
pub scale_factor: Option<f64>,
pub valid_orientations: ValidOrientations,
Expand Down
5 changes: 3 additions & 2 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ impl ApplicationName {
}
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct PlatformSpecificWindowBuilderAttributes {
pub name: Option<ApplicationName>,
pub activation_token: Option<ActivationToken>,
#[cfg(x11_platform)]
pub x11: X11WindowBuilderAttributes,
}

#[derive(Clone)]
#[derive(Debug, Clone)]
#[cfg(x11_platform)]
pub struct X11WindowBuilderAttributes {
pub visual_id: Option<x11rb::protocol::xproto::Visualid>,
Expand Down Expand Up @@ -141,6 +141,7 @@ impl fmt::Display for OsError {
}
}

#[derive(Debug)]
pub(crate) enum Window {
#[cfg(x11_platform)]
X(x11::Window),
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/linux/wayland/event_loop/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::{DeviceId, WindowId};

/// An event loop's sink to deliver events from the Wayland event callbacks
/// to the winit's user.
#[derive(Default)]
#[derive(Debug, Default)]
pub struct EventSink {
pub window_events: Vec<Event<()>>,
}
Expand Down
7 changes: 7 additions & 0 deletions src/platform_impl/linux/wayland/window/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! The Wayland window.

use std::fmt;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

Expand Down Expand Up @@ -78,6 +79,12 @@ pub struct Window {
window_events_sink: Arc<Mutex<EventSink>>,
}

impl fmt::Debug for Window {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Window { .. }").finish_non_exhaustive()
}
}

impl Window {
pub(crate) fn new<T>(
event_loop_window_target: &EventLoopWindowTarget<T>,
Expand Down
2 changes: 2 additions & 0 deletions src/platform_impl/linux/x11/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const ICONIC_STATE: u32 = 3;

type X11Source = Generic<BorrowedFd<'static>>;

#[derive(Debug)]
struct WakeSender<T> {
sender: Sender<T>,
waker: Ping,
Expand Down Expand Up @@ -823,6 +824,7 @@ impl DeviceId {
}
}

#[derive(Debug)]
pub(crate) struct Window(Arc<UnownedWindow>);

impl Deref for Window {
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/linux/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl SharedState {
unsafe impl Send for UnownedWindow {}
unsafe impl Sync for UnownedWindow {}

#[derive(Debug)]
pub(crate) struct UnownedWindow {
pub(crate) xconn: Arc<XConnection>, // never changes
xwindow: xproto::Window, // never changes
Expand Down
3 changes: 2 additions & 1 deletion src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use super::ffi::kCGFloatingWindowLevel;
use super::ffi::{kCGNormalWindowLevel, CGSMainConnectionID, CGSSetWindowBackgroundBlurRadius};
use super::monitor::get_display_id;

#[derive(Debug)]
pub(crate) struct Window {
window: MainThreadBound<Id<WinitWindow>>,
// We keep this around so that it doesn't get dropped until the window does.
Expand Down Expand Up @@ -129,7 +130,7 @@ impl From<u64> for WindowId {
}
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct PlatformSpecificWindowBuilderAttributes {
pub movable_by_window_background: bool,
pub titlebar_transparent: bool,
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/orbital/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const ORBITAL_FLAG_BORDERLESS: char = 'l';
const ORBITAL_FLAG_RESIZABLE: char = 'r';
const ORBITAL_FLAG_TRANSPARENT: char = 't';

#[derive(Debug)]
pub struct Window {
window_socket: Arc<RedoxSocket>,
redraws: Arc<Mutex<VecDeque<WindowId>>>,
Expand Down
4 changes: 4 additions & 0 deletions src/platform_impl/web/async/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ pub fn channel<T>() -> (AsyncSender<T>, AsyncReceiver<T>) {
(sender, receiver)
}

#[derive(Debug)]
pub struct AsyncSender<T>(Arc<SenderInner<T>>);

#[derive(Debug)]
struct SenderInner<T> {
// We need to wrap it into a `Mutex` to make it `Sync`. So the sender can't
// be accessed on the main thread, as it could block. Additionally we need
Expand Down Expand Up @@ -64,6 +66,7 @@ impl<T> Drop for SenderInner<T> {
}
}

#[derive(Debug)]
pub struct AsyncReceiver<T> {
receiver: Rc<Receiver<T>>,
shared: Arc<Shared>,
Expand Down Expand Up @@ -117,6 +120,7 @@ impl<T> Drop for AsyncReceiver<T> {
}
}

#[derive(Debug)]
struct Shared {
closed: AtomicBool,
waker: AtomicWaker,
Expand Down
8 changes: 8 additions & 0 deletions src/platform_impl/web/async/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
use super::{channel, AsyncReceiver, AsyncSender, Wrapper};
use std::{
cell::Ref,
fmt,
sync::{Arc, Condvar, Mutex},
};

#[derive(Debug)]
pub struct Dispatcher<T: 'static>(Wrapper<true, T, AsyncSender<Closure<T>>, Closure<T>>);

struct Closure<T>(Box<dyn FnOnce(&T) + Send>);

impl<T> fmt::Debug for Closure<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Closure").finish_non_exhaustive()
}
}

impl<T> Dispatcher<T> {
#[track_caller]
pub fn new(value: T) -> Option<(Self, DispatchRunner<T>)> {
Expand Down
2 changes: 2 additions & 0 deletions src/platform_impl/web/async/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use wasm_bindgen::{JsCast, JsValue};

// Unsafe wrapper type that allows us to use `T` when it's not `Send` from other threads.
// `value` **must** only be accessed on the main thread.
#[derive(Debug)]
pub struct Wrapper<const SYNC: bool, V: 'static, S: Clone + Send, E> {
value: Value<SYNC, V>,
handler: fn(&RefCell<Option<V>>, E),
sender_data: S,
sender_handler: fn(&S, E),
}

#[derive(Debug)]
struct Value<const SYNC: bool, V> {
// SAFETY:
// This value must not be accessed if not on the main thread.
Expand Down
4 changes: 3 additions & 1 deletion src/platform_impl/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;

#[derive(Debug)]
pub struct Window {
inner: Dispatcher<Inner>,
}

#[derive(Debug)]
pub struct Inner {
id: WindowId,
pub window: web_sys::Window,
Expand Down Expand Up @@ -461,7 +463,7 @@ impl From<u64> for WindowId {
}
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct PlatformSpecificWindowBuilderAttributes {
pub(crate) canvas: SendSyncWrapper<Option<backend::RawCanvasType>>,
pub(crate) prevent_default: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::event::DeviceId as RootDeviceId;
use crate::icon::Icon;
use crate::keyboard::Key;

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct PlatformSpecificWindowBuilderAttributes {
pub owner: Option<HWND>,
pub menu: Option<HMENU>,
Expand Down
1 change: 1 addition & 0 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ use crate::{
};

/// The Win32 implementation of the main `Window` object.
#[derive(Debug)]
pub(crate) struct Window {
/// Main handle for the window.
window: HWND,
Expand Down
17 changes: 2 additions & 15 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,11 @@ pub use cursor_icon::{CursorIcon, ParseError as CursorIconParseError};
/// }
/// });
/// ```
#[derive(Debug)]
pub struct Window {
pub(crate) window: platform_impl::Window,
}

impl fmt::Debug for Window {
fn fmt(&self, fmtr: &mut fmt::Formatter<'_>) -> fmt::Result {
fmtr.pad("Window { .. }")
}
}

impl Drop for Window {
fn drop(&mut self) {
self.window.maybe_wait_on_main(|w| {
Expand Down Expand Up @@ -114,7 +109,7 @@ impl From<u64> for WindowId {
}

/// Object that allows building windows.
#[derive(Clone, Default)]
#[derive(Debug, Clone, Default)]
#[must_use]
pub struct WindowBuilder {
/// The attributes to use to create the window.
Expand All @@ -124,14 +119,6 @@ pub struct WindowBuilder {
pub(crate) platform_specific: platform_impl::PlatformSpecificWindowBuilderAttributes,
}

impl fmt::Debug for WindowBuilder {
fn fmt(&self, fmtr: &mut fmt::Formatter<'_>) -> fmt::Result {
fmtr.debug_struct("WindowBuilder")
.field("window", &self.window)
.finish()
}
}

/// Attributes to use when creating a window.
#[derive(Debug, Clone)]
pub struct WindowAttributes {
Expand Down