Skip to content

Commit

Permalink
Deprecate EventListenerMutable, deny unsafe, & bump ver
Browse files Browse the repository at this point in the history
  • Loading branch information
yavko committed Jul 23, 2023
1 parent a4fd511 commit de58992
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 231 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ lto = true
members = ["hyprland-macros"]

[workspace.package]
version = "0.3.8"
version = "0.3.9"
license = "GPL-3.0-or-later"
repository = "https://github.com/hyprland-community/hyprland-rs"
keywords = ["hyprland", "ipc", "hypr", "wayland", "linux"]
Expand Down
2 changes: 2 additions & 0 deletions src/event_listener/async_im.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ pub struct AsyncEventListener {
}

// Mark the EventListener as thread-safe
#[allow(unsafe_code)]
unsafe impl Send for AsyncEventListener {}
#[allow(unsafe_code)]
unsafe impl Sync for AsyncEventListener {}

impl Default for AsyncEventListener {
Expand Down
149 changes: 0 additions & 149 deletions src/event_listener/async_mut.rs

This file was deleted.

2 changes: 2 additions & 0 deletions src/event_listener/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ pub struct EventListener {
}

// Mark the EventListener as thread-safe
#[allow(unsafe_code)]
unsafe impl Send for EventListener {}
#[allow(unsafe_code)]
unsafe impl Sync for EventListener {}

impl Default for EventListener {
Expand Down
7 changes: 4 additions & 3 deletions src/event_listener/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ pub use crate::event_listener::immutable::EventListener;
mod async_im;
pub use crate::event_listener::async_im::AsyncEventListener;

//mod async_mut;
//pub use crate::event_listener::async_mut::AsyncMutableEventListener;

mod immutable;
#[deprecated(
since = "0.3.9",
note = "It's rarely used and is pretty badly implemented, use the not mutable one."
)]
pub use crate::event_listener::mutable::EventListener as EventListenerMutable;

add_listener!(workspace_change d, WorkspaceType, "on workspace change", "changed workspace to" => id);
Expand Down
2 changes: 2 additions & 0 deletions src/event_listener/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ pub struct EventListener {
}

// Mark the EventListener as thread-safe
#[allow(unsafe_code)]
unsafe impl Send for EventListener {}
#[allow(unsafe_code)]
unsafe impl Sync for EventListener {}

impl Default for EventListener {
Expand Down
91 changes: 16 additions & 75 deletions src/event_listener/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ pub(crate) type VoidFutureMut =
pub(crate) type Closure<T> = EventTypes<dyn Fn(T), dyn Fn(T, &mut State)>;
pub(crate) type AsyncClosure<T> = AsyncEventTypes<
dyn Sync + Send + Fn(T) -> VoidFuture,
dyn Sync + Send + Fn(T, &mut StateV2) -> VoidFutureMut,
dyn Sync + Send + Fn(T, &mut State) -> VoidFutureMut,
>;
pub(crate) type Closures<T> = Vec<Closure<T>>;
pub(crate) type AsyncClosures<T> = Vec<AsyncClosure<T>>;
Expand Down Expand Up @@ -333,7 +333,9 @@ pub struct WindowMoveEvent {
pub workspace_name: String,
}

#[allow(unsafe_code)]
unsafe impl Send for WindowMoveEvent {}
#[allow(unsafe_code)]
unsafe impl Sync for WindowMoveEvent {}
/// The data for the event executed when opening a new window
#[derive(Clone, Debug)]
Expand All @@ -348,7 +350,9 @@ pub struct WindowOpenEvent {
pub window_title: String,
}

#[allow(unsafe_code)]
unsafe impl Send for WindowOpenEvent {}
#[allow(unsafe_code)]
unsafe impl Sync for WindowOpenEvent {}
/// The data for the event executed when changing keyboard layouts
#[derive(Clone, Debug)]
Expand All @@ -359,7 +363,9 @@ pub struct LayoutEvent {
pub layout_name: String,
}

#[allow(unsafe_code)]
unsafe impl Send for LayoutEvent {}
#[allow(unsafe_code)]
unsafe impl Sync for LayoutEvent {}
/// The mutable state available to Closures
#[derive(PartialEq, Eq, Clone, Debug)]
Expand All @@ -372,80 +378,9 @@ pub struct State {
pub fullscreen_state: bool,
}

use std::ops::{Deref, DerefMut};
/// Wrapper type that adds handler for events
#[derive(Clone)]
#[doc(hidden)]
pub struct MutWrapper<'a, 'b, T>(T, &'a (dyn Fn(T) + 'a), &'b (dyn Fn(T) -> VoidFuture + 'b));
impl<T> MutWrapper<'_, '_, T> {
#[allow(dead_code)]
pub(crate) fn update(&mut self, v: T) {
self.0 = v;
}
}
impl<T: PartialEq> PartialEq for MutWrapper<'_, '_, T> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<T: Debug> Debug for MutWrapper<'_, '_, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl<T: Eq> Eq for MutWrapper<'_, '_, T> {}

impl<T> Deref for MutWrapper<'_, '_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: Clone> DerefMut for MutWrapper<'_, '_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.1(self.0.clone());
&mut self.0
}
}

/// The mutable state available to Closures
#[derive(PartialEq, Eq, Clone, Debug)]
#[doc(hidden)]
pub struct StateV2 {
/// The active workspace
pub workspace: MutWrapper<'static, 'static, String>,
/// The active monitor
pub monitor: MutWrapper<'static, 'static, String>,
/// The fullscreen state
pub fullscreen: MutWrapper<'static, 'static, bool>,
}

unsafe impl Send for StateV2 {}
unsafe impl Sync for StateV2 {}
impl StateV2 {
/// Init new state
pub fn new<Str: ToString>(work: Str, mon: Str, full: bool) -> Self {
use crate::dispatch::*;
use hyprland_macros::async_closure;
Self {
workspace: MutWrapper(
work.to_string(),
&|work| {
if let Ok(()) =
crate::dispatch!(Workspace, WorkspaceIdentifierWithSpecial::Name(&work))
{
}
},
&async_closure! { |work| if let Ok(()) =
crate::dispatch!(async; Workspace, WorkspaceIdentifierWithSpecial::Name(&work)).await {}},
),
monitor: MutWrapper(mon.to_string(), &|_| {}, &async_closure! {|_| {}}),
fullscreen: MutWrapper(full, &|_| {}, &async_closure! {|_| {}}),
}
}
}

#[allow(unsafe_code)]
unsafe impl Send for State {}
#[allow(unsafe_code)]
unsafe impl Sync for State {}
impl State {
/// Execute changes in state
Expand Down Expand Up @@ -531,7 +466,7 @@ pub(crate) async fn execute_closure_async<T>(f: &AsyncClosure<T>, val: T) {
pub(crate) async fn execute_closure_async_state<T: Clone>(
f: &AsyncClosure<T>,
val: &T,
state: &mut StateV2,
state: &mut State,
) {
match f {
AsyncEventTypes::MutableState(fun) => fun(val.clone(), state).await,
Expand Down Expand Up @@ -582,7 +517,9 @@ pub struct WindowEventData {
pub window_address: Address,
}

#[allow(unsafe_code)]
unsafe impl Send for WindowEventData {}
#[allow(unsafe_code)]
unsafe impl Sync for WindowEventData {}
/// This tuple struct holds monitor event data
#[derive(Debug, Clone)]
Expand All @@ -593,7 +530,9 @@ pub struct MonitorEventData {
pub workspace: WorkspaceType,
}

#[allow(unsafe_code)]
unsafe impl Send for MonitorEventData {}
#[allow(unsafe_code)]
unsafe impl Sync for MonitorEventData {}
/// This tuple struct holds monitor event data
#[derive(Debug, Clone)]
Expand All @@ -604,7 +543,9 @@ pub struct WindowFloatEventData {
pub is_floating: bool,
}

#[allow(unsafe_code)]
unsafe impl Send for WindowFloatEventData {}
#[allow(unsafe_code)]
unsafe impl Sync for WindowFloatEventData {}
/// This enum holds every event type
#[derive(Debug, Clone)]
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![warn(missing_docs)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]
#![deny(unsafe_code)]

#[macro_use]
extern crate lazy_static;
Expand Down Expand Up @@ -65,4 +66,5 @@ pub(crate) mod unix_async {
}

/// This type provides the result type used everywhere in Hyprland-rs
pub type Result<T> = std::result::Result<T, shared::HyprError>;
pub type Result<T> = std::result::Result<T, shared::HyprError>;

0 comments on commit de58992

Please sign in to comment.