gioser

tawasuyu/mirada

Sovereign tiling Wayland compositor + window manager in Rust, on Llimphi.

Rust 50Markdown 30TOML 211282 Definiciones

⎇ main vendor/smithay/src/utils/hook.rs Rust · 53 líneas · 1.0 KB Historia Quién tocó qué crudo

chore: refresco desde el monorepo (pin tawasuyu.git v0.2.0) a91da3fea · Sergio ·

1use std::sync::Arc;
2
3crate::utils::ids::id_gen!(hooks_id);
4
5/// Unique hook identifier used to unregister commit/descruction hooks
6#[derive(Debug, Clone, Eq, PartialEq)]
7pub struct HookId(Arc<InnerId>);
8
9pub(crate) struct Hook<T: ?Sized> {
10    pub id: HookId,
11    pub cb: Arc<T>,
12}
13
14impl<T: ?Sized> std::fmt::Debug for Hook<T> {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        f.debug_struct("Hook")
17            .field("id", &self.id)
18            .finish_non_exhaustive()
19    }
20}
21
22impl<T: ?Sized> Clone for Hook<T> {
23    fn clone(&self) -> Self {
24        Self {
25            id: self.id.clone(),
26            cb: self.cb.clone(),
27        }
28    }
29}
30
31impl<T: ?Sized> Hook<T> {
32    pub fn new(cb: Arc<T>) -> Self {
33        Self {
34            id: HookId(Arc::new(InnerId::new())),
35            cb,
36        }
37    }
38}
39
40#[derive(Debug, Eq, PartialEq)]
41struct InnerId(usize);
42
43impl InnerId {
44    fn new() -> Self {
45        Self(hooks_id::next())
46    }
47}
48
49impl Drop for InnerId {
50    fn drop(&mut self) {
51        hooks_id::remove(self.0);
52    }
53}