chore: refresco desde el monorepo (pin tawasuyu.git v0.2.0)
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}