1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::execution_manager::execution_context::ExecutorDropper;
use std::any::{Any, TypeId};
use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};
use std::sync::Arc;

#[derive(Clone)]
pub struct ExecutorAddress {
    pub(crate) executor_keeper: Arc<ExecutorDropper>,
    pub(crate) init_data: Arc<dyn Any + Sync + Send + 'static>,
    pub(crate) executor_type_id: TypeId,
    pub(crate) executor_internal_id: u64,
}
unsafe impl Send for ExecutorAddress {}
unsafe impl Sync for ExecutorAddress {}

impl ExecutorAddress {
    pub fn is_of_type<T: 'static>(&self) -> bool {
        self.executor_type_id == TypeId::of::<T>()
    }

    pub fn to_weak(&self) -> WeakExecutorAddress {
        WeakExecutorAddress {
            executor_type_id: self.executor_type_id,
            executor_internal_id: self.executor_internal_id,
        }
    }
}

impl Hash for ExecutorAddress {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.executor_type_id.hash(state);
        state.write_u64(self.executor_internal_id);
    }
}
impl PartialEq for ExecutorAddress {
    #[inline(always)]
    fn eq(&self, other: &Self) -> bool {
        (self.executor_type_id, self.executor_internal_id)
            .eq(&(other.executor_type_id, other.executor_internal_id))
    }
}
impl Eq for ExecutorAddress {}

#[derive(Copy, Clone)]
pub struct WeakExecutorAddress {
    pub(crate) executor_type_id: TypeId,
    pub(crate) executor_internal_id: u64,
}

impl Debug for WeakExecutorAddress {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!(
            "({:?},I{})",
            self.executor_type_id, self.executor_internal_id,
        ))
    }
}

impl WeakExecutorAddress {
    pub(crate) fn empty() -> Self {
        Self {
            executor_type_id: TypeId::of::<()>(),
            executor_internal_id: 0,
        }
    }
}

impl Hash for WeakExecutorAddress {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.executor_type_id.hash(state);
        state.write_u64(self.executor_internal_id);
    }
}
impl PartialEq for WeakExecutorAddress {
    #[inline(always)]
    fn eq(&self, other: &Self) -> bool {
        (self.executor_type_id, self.executor_internal_id)
            .eq(&(other.executor_type_id, other.executor_internal_id))
    }
}
impl Eq for WeakExecutorAddress {}