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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! **Cached process lookups with [lunatic](https://crates.io/crates/lunatic).**
//!
//! When a process is lookup, it is cached in the local process to avoid unnecessery future lookups.
//! This is useful for globally registered processes and abstract processes.
//!
//! # Example
//!
//! ```
//! use lunatic::{spawn_link, test};
//! use lunatic_cached_process::{cached_process, CachedLookup, ProcessCached};
//!
//! cached_process! {
//!     static COUNTER_PROCESS: ProcessCached<()> = "counter-process";
//! }
//!
//! let process = spawn_link!(|mailbox: Mailbox<()>| { loop { } });
//! process.register("counter-process");
//!
//! let lookup: Option<Process<T>> = COUNTER_PROCESS.get(); // First call lookup process from host
//! assert!(lookup.is_some());
//!
//! let lookup: Option<Process<T>> = COUNTER_PROCESS.get(); // Subsequent calls will use cached process
//! assert!(lookup.is_some());
//! ```

use std::cell::RefCell;

use lunatic::{process::ProcessRef, Process, ProcessLocal};
use serde::{Deserialize, Serialize};

pub type ProcessCached<T> = CachedProcess<Process<T>>;
pub type ProcessRefCached<T> = CachedProcess<ProcessRef<T>>;

/// Cached process to avoid looking up a global process multiple times.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CachedProcess<T> {
    // TODO: Replace with `Cell` when lunatic gets a new version where `ProcessRef` is `Copy`.
    lookup_state: RefCell<LookupState<T>>,
    process_name: &'static str,
}

impl<T> CachedProcess<T> {
    /// Construct a new process cache with a registered process name.
    pub fn new(name: &'static str) -> Self {
        CachedProcess {
            lookup_state: RefCell::new(LookupState::NotLookedUp),
            process_name: name,
        }
    }

    /// Sets the process cache value.
    pub fn set(&self, value: T) {
        *self.lookup_state.borrow_mut() = LookupState::Present(value);
    }

    /// Resets the process cache, causing subsequent calls to lookup the process again.
    pub fn reset(&self) {
        *self.lookup_state.borrow_mut() = LookupState::NotLookedUp;
    }
}

/// Trait for accessing a static process local cache.
pub trait CachedLookup<T> {
    /// Looks up a process by its name, and caches the result.
    /// Subsequent calls will used the cached value.
    fn get(self) -> Option<T>;

    /// Sets the cached lookup. This will prevent any lookups from being made,
    /// since subsequent calls to [`CachedLookup::get`] will return this cached value.
    fn set(self, value: T);

    /// Resets the cache, causing the next call to [`CachedLookup::get`] to lookup the process again.
    fn reset(self);
}

macro_rules! impl_cached_lookup {
    ($ty:ident, $cache_ty:ident) => {
        impl<T> CachedLookup<$ty<T>> for &'static ProcessLocal<$cache_ty<T>> {
            fn get(self) -> Option<$ty<T>> {
                lookup(self, |name| $ty::lookup(name))
            }

            fn set(self, value: $ty<T>) {
                set(self, value);
            }

            fn reset(self) {
                reset(self)
            }
        }
    };
}

impl_cached_lookup!(Process, ProcessCached);
impl_cached_lookup!(ProcessRef, ProcessRefCached);

/// Macro for defining a process local lookup cache for processes.
///
/// # Examples
///
/// Cached [`lunatic::Process`].
///
/// ```
/// cached_process! {
///     static COUNTER: ProcessCached<CountMessage> = "global-counter-process";
/// }
/// ```
///
/// Cached [`lunatic::process::ProcessRef`].
///
/// ```
/// cached_process! {
///     static COUNTER: ProcessCached<CountMessage> = "global-counter-process-ref";
/// }
/// ```
#[macro_export]
macro_rules! cached_process {
    (
        static $ident:ident : $ty:ty = $name:tt ;
    ) => {
        lunatic::process_local! {
            static $ident: $ty = $crate::CachedProcess::new($name);
        }
    };
}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
enum LookupState<T> {
    NotLookedUp,
    NotPresent,
    Present(T),
}

impl<T> Default for LookupState<T> {
    fn default() -> Self {
        LookupState::NotLookedUp
    }
}

fn lookup<F, T>(cached_process: &'static ProcessLocal<CachedProcess<T>>, f: F) -> Option<T>
where
    F: Fn(&'static str) -> Option<T>,
    T: Clone,
{
    cached_process.with(|proc| {
        let proc_ref = proc.lookup_state.borrow();
        match &*proc_ref {
            LookupState::NotLookedUp => {
                println!("Not looked up");
                std::mem::drop(proc_ref);
                match f(proc.process_name) {
                    Some(process) => {
                        *proc.lookup_state.borrow_mut() = LookupState::Present(process.clone()); // TODO: Replace clone with copy
                        Some(process)
                    }
                    None => {
                        *proc.lookup_state.borrow_mut() = LookupState::NotPresent;
                        None
                    }
                }
            }
            LookupState::NotPresent => {
                println!("Not present");
                None
            }
            LookupState::Present(process) => {
                println!("Present");
                Some(process.clone()) // TODO: Replace clone with copy
            }
        }
    })
}

fn set<T>(cached_process: &'static ProcessLocal<CachedProcess<T>>, value: T) {
    cached_process.with(|proc| proc.set(value))
}

fn reset<T>(cached_process: &'static ProcessLocal<CachedProcess<T>>) {
    cached_process.with(|proc| proc.reset())
}