gom 0.1.7

A simple Rust global object manager
Documentation
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use core::panic;
use std::{
    any::{Any, TypeId},
    cell::RefCell,
    collections::HashMap,
    marker::PhantomData,
    sync::RwLock,
};

use lazy_static::lazy_static;

macro_rules! thread_deadlock {
    () => {
        panic!("Thread deadlock!")
    };
}

lazy_static! {
    static ref _TABLE: RwLock<HashMap<TypeId, RwLock<HashMap<String, RwLock<Box<dyn Any + Send + Sync>>>>>> =
        RwLock::new(HashMap::new());
}

thread_local! {
    static _LOCAL_TABLE: RefCell<HashMap<TypeId, HashMap<String, Box<dyn Any>>>> =
        RefCell::new(HashMap::new());
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum Context {
    With(String, TypeId),
    Apply(String, TypeId),
}

enum Lock {
    Global,
    Type,
    Key,
}

thread_local! {
    // 上下文访问栈
    static CONTEXT: RefCell<Vec<Context>> = RefCell::new(Vec::new());
}

struct ContextOperator;
impl ContextOperator {
    fn push(ctx: Context) {
        CONTEXT.with(|ctx_cell| {
            ctx_cell.borrow_mut().push(ctx);
        });
    }

    fn pop() {
        CONTEXT.with(|ctx_cell| ctx_cell.borrow_mut().pop());
    }

    fn cannot_lock_write_lock<T: 'static>(name: &str, lock: Lock) -> bool {
        match lock {
            Lock::Global => CONTEXT.with_borrow(|v| !v.is_empty()),
            Lock::Type => CONTEXT.with_borrow(|v| {
                v.iter().any(|x| match x {
                    Context::With(_, type_id) | Context::Apply(_, type_id) => {
                        type_id == &TypeId::of::<T>()
                    }
                })
            }),
            Lock::Key => CONTEXT.with_borrow(|v| {
                v.iter().any(|x| match x {
                    Context::With(key, type_id) | Context::Apply(key, type_id) => {
                        key == name && type_id == &TypeId::of::<T>()
                    }
                })
            }),
        }
    }
}

// 检查如果获取写锁是否会导致死锁
fn check_write_deadlock<T: 'static>(name: &str, lock: Lock) {
    if ContextOperator::cannot_lock_write_lock::<T>(name, lock) {
        thread_deadlock!();
    }
}

// 检查如果获取读锁是否会导致死锁
fn check_read_deadlock<T: 'static>(name: &str) {
    if CONTEXT.with_borrow(|v| {
        v.iter().any(|x| match x {
            Context::Apply(s, type_id) => s == name && type_id == &TypeId::of::<T>(),
            _ => false,
        })
    }) {
        thread_deadlock!();
    }
}

#[cfg(debug_assertions)]
macro_rules! check_deadlock {
    (mut $type:ty : $name:expr ; $em:expr) => {
        $crate::check_write_deadlock::<$type>($name, $em);
    };
    (ref $type:ty : $name:expr) => {
        $crate::check_read_deadlock::<$type>($name);
    };
}

#[cfg(not(debug_assertions))]
macro_rules! check_deadlock {
    (mut $type:ty : $name:expr ; $em:expr) => {};
    (ref $type:ty : $name:expr) => {};
}

/// 用于访问注册表的类型
///
/// # 注解
///
/// + 其索引方式是:`类型-键` 唯一,因而同一个键可以对应多个不同类型的值
/// + 如果闭包中使用了不恰当的嵌套,可能会导致线程死锁
pub struct Registry<T> {
    _marker: PhantomData<T>,
}

impl<T: 'static + Send + Sync + Any> Registry<T> {
    fn _register(name: &str, value: T) -> Option<()> {
        let type_id = TypeId::of::<T>();
        let has_type = {
            let map = _TABLE.read().ok()?;
            map.contains_key(&type_id)
        };
        if !has_type {
            check_deadlock!(mut T:name;Lock::Global);
            let mut map = _TABLE.write().ok()?;
            map.insert(type_id, RwLock::new(HashMap::new()));
        }
        let map = _TABLE.read().ok()?;
        check_deadlock!(mut T:name;Lock::Type);
        let mut type_map = map.get(&type_id)?.write().ok()?;
        type_map.insert(String::from(name), RwLock::new(Box::new(value)));
        Some(())
    }

    /// 向注册表中注册一个新值
    ///
    /// 如果相同的键已存在,那么旧值将会被新值替换
    ///
    /// # 示例
    ///
    /// ```rust
    /// use gom::Registry;
    ///
    /// Registry::<i32>::register("my_key", 42);
    /// Registry::register("my_key", 64);
    /// ```
    pub fn register(name: &str, value: T) -> Result<(), ()> {
        Self::_register(name, value).ok_or(())
    }

    /// 从注册表中移除指定键对应的值
    ///
    /// 如果键不存在,则返回 `None`
    ///
    /// # 示例
    ///
    /// ```rust
    /// use gom::Registry;
    ///
    /// Registry::<i32>::register("my_key", 42);
    /// assert_eq!(Registry::<i32>::remove("my_key"), Some(42));
    /// assert_eq!(Registry::<i32>::remove("my_key"), None);
    /// ```
    pub fn remove(name: &str) -> Option<T> {
        let type_id = TypeId::of::<T>();
        let lock_value = {
            let map = _TABLE.read().ok()?;
            let type_map = map.get(&type_id)?;
            check_deadlock!(mut T:name;Lock::Type);
            let mut type_map = type_map.write().ok()?;
            type_map.remove(name)?
        };
        let value = lock_value.into_inner().ok()?;
        let type_value = value.downcast::<T>().ok()?;
        Some(*type_value)
    }

    fn _exists(name: &str) -> Option<bool> {
        let type_id = TypeId::of::<T>();
        let map = _TABLE.read().ok()?;
        let lock_type_map = map.get(&type_id)?;
        let type_map = lock_type_map.read().ok()?;
        Some(type_map.contains_key(name))
    }

    /// 判断指定键是否存在于注册表中
    ///
    /// # 示例
    ///
    /// ```rust
    /// use gom::Registry;
    ///
    /// Registry::<i32>::register("my_key", 42);
    /// assert_eq!(Registry::<i32>::exists("my_key"), true);
    /// assert_eq!(Registry::<i32>::exists("other_key"), false);
    /// ```
    pub fn exists(name: &str) -> bool {
        Self::_exists(name).unwrap_or(false)
    }

    /// 向注册表中的指定键应用一个函数,该函数可以修改注册表中的值
    ///
    /// 如果键不存在,则返回 `None`;否则,返回闭包函数的返回值
    ///
    /// # 示例
    /// ```rust
    /// use gom::Registry;
    ///
    /// Registry::<i32>::register("my_key", 42);
    /// assert_eq!(Registry::<i32>::apply("my_key", |v| { *v += 1; *v }), Some(43));
    /// assert_eq!(Registry::<i32>::apply("other_key", |v| *v += 1), None);
    /// ```
    pub fn apply<R, F: FnOnce(&mut T) -> R>(name: &str, func: F) -> Option<R> {
        let type_id = TypeId::of::<T>();
        let type_map = _TABLE.read().ok()?;
        let type_map = type_map.get(&type_id)?.read().ok()?;
        check_deadlock!(mut T:name;Lock::Key);
        let mut value = type_map.get(name)?.write().ok()?;
        let var = value.downcast_mut::<T>()?;
        ContextOperator::push(Context::Apply(String::from(name), type_id));
        let ret = Some(func(var));
        ContextOperator::pop();
        ret
    }

    /// 向注册表中的指定键应用一个函数,该函数仅能读取注册表中的值
    ///
    /// 如果键不存在,则返回 `None`;否则,返回闭包函数的返回值
    ///
    /// # 示例
    /// ```rust
    /// use gom::Registry;
    ///
    /// Registry::<i32>::register("my_key", 42);
    /// assert_eq!(Registry::<i32>::with("my_key", |v| *v), Some(42));
    /// assert_eq!(Registry::<i32>::with("other_key", |v| *v), None);
    /// ```
    pub fn with<R, F: FnOnce(&T) -> R>(name: &str, func: F) -> Option<R> {
        let type_id = TypeId::of::<T>();
        let type_map = _TABLE.read().ok()?;
        let type_map = type_map.get(&type_id)?.read().ok()?;
        check_deadlock!(ref T:name);
        let value = type_map.get(name)?.read().ok()?;
        let var = value.downcast_ref::<T>()?;
        ContextOperator::push(Context::With(String::from(name), type_id));
        let ret = Some(func(var));
        ContextOperator::pop();
        ret
    }

    /// 使用新值替换注册表中的指定键对应的值
    ///
    /// 如果键不存在,则返回 `None` 并且不会注册新值;否则,返回旧值
    ///
    /// # 示例
    /// ```rust
    /// use gom::Registry;
    ///
    /// Registry::<i32>::register("my_key", 42);
    /// assert_eq!(Registry::<i32>::replace("my_key", 64), Some(42));
    /// assert_eq!(Registry::<i32>::replace("other_key", 32), None);
    /// ```
    pub fn replace(name: &str, value: T) -> Option<T> {
        let type_id = TypeId::of::<T>();
        let type_map = _TABLE.read().ok()?;
        let type_map = type_map.get(&type_id)?;
        let value = {
            check_deadlock!(mut T:name;Lock::Type);
            let mut type_map = type_map.write().ok()?;
            let ret = type_map.remove(name)?;
            type_map.insert(String::from(name), RwLock::new(Box::new(value)));
            ret
        };
        let value = value.into_inner().ok()?;
        let type_value = value.downcast::<T>().ok()?;
        Some(*type_value)
    }

    /// 与 `replace` 相同,但已弃用,请使用 `replace` 替代
    #[deprecated(since = "0.1.6", note = "use `replace` instead")]
    pub fn take(name: &str, value: T) -> Option<T> {
        Self::replace(name, value)
    }
}

/// 针对于线程局部变量的注册表
pub struct LocalRegistry<T> {
    _marker: PhantomData<T>,
}

impl<T: 'static> LocalRegistry<T> {
    /// 向注册表中注册一个新值
    ///
    /// 如果相同的键已存在,那么旧值将会被新值替换
    ///
    /// # 示例
    /// ```rust
    /// use gom::LocalRegistry;
    ///
    /// LocalRegistry::<i32>::register("my_key", 42);
    /// ```
    pub fn register(name: &str, value: T) {
        let type_id = TypeId::of::<T>();
        let has_type = _LOCAL_TABLE.with_borrow(|table| table.contains_key(&type_id));
        if !has_type {
            _LOCAL_TABLE.with_borrow_mut(|table| {
                table.insert(type_id, HashMap::new());
            });
        }
        _LOCAL_TABLE.with_borrow_mut(|table| {
            let type_map = table.get_mut(&type_id).unwrap();
            type_map.insert(String::from(name), Box::new(value));
        })
    }

    /// 从注册表中移除指定键对应的值
    ///
    /// 如果键不存在,则返回 `None`
    ///
    /// # 示例
    /// ```rust
    /// use gom::LocalRegistry;
    ///
    /// LocalRegistry::<i32>::register("my_key", 42);
    /// assert_eq!(LocalRegistry::<i32>::remove("my_key"), Some(42));
    /// assert_eq!(LocalRegistry::<i32>::remove("my_key"), None);
    /// ```
    pub fn remove(name: &str) -> Option<T> {
        let type_id = TypeId::of::<T>();
        let value = _LOCAL_TABLE.with_borrow_mut(|table| {
            let type_map = table.get_mut(&type_id)?;
            type_map.remove(name)
        })?;
        let value = value.downcast::<T>().ok()?;
        Some(*value)
    }

    /// 判断指定键是否存在于注册表中
    ///
    /// # 示例
    /// ```rust
    /// use gom::LocalRegistry;
    ///
    /// LocalRegistry::<i32>::register("my_key", 42);
    /// assert_eq!(LocalRegistry::<i32>::exists("my_key"), true);
    /// assert_eq!(LocalRegistry::<i32>::exists("other_key"), false);
    /// ```
    pub fn exists(name: &str) -> bool {
        let type_id = TypeId::of::<T>();
        _LOCAL_TABLE.with_borrow(|table| {
            let type_map = table.get(&type_id).unwrap();
            type_map.contains_key(name)
        })
    }

    /// 向注册表中的指定键应用一个函数,该函数可以修改注册表中的值
    ///
    /// 如果键不存在,则返回 `None`;否则,返回闭包函数的返回值
    ///
    /// # 示例
    /// ```rust
    /// use gom::LocalRegistry;
    ///
    /// LocalRegistry::register("my_key", 42);
    /// assert_eq!(LocalRegistry::<i32>::apply("my_key", |v| { *v += 1; *v }), Some(43));
    /// assert_eq!(LocalRegistry::<i32>::apply("other_key", |v| *v += 1), None);
    /// ```
    pub fn apply<R, F: FnOnce(&mut T) -> R>(name: &str, func: F) -> Option<R> {
        let type_id = TypeId::of::<T>();
        _LOCAL_TABLE.with_borrow_mut(|table| {
            let type_map = table.get_mut(&type_id)?;
            let value = type_map.get_mut(name)?;
            let value = value.downcast_mut::<T>()?;
            Some(func(value))
        })
    }

    /// 向注册表中的指定键应用一个函数,该函数仅能读取注册表中的值
    ///
    /// 如果键不存在,则返回 `None`;否则,返回闭包函数的返回值
    ///
    /// # 示例
    /// ```rust
    /// use gom::LocalRegistry;
    ///
    /// LocalRegistry::<i32>::register("my_key", 42);
    /// assert_eq!(LocalRegistry::<i32>::with("my_key", |v| *v), Some(42));
    /// assert_eq!(LocalRegistry::<i32>::with("other_key", |v| *v), None);
    /// ```
    pub fn with<R, F: FnOnce(&T) -> R>(name: &str, func: F) -> Option<R> {
        let type_id = TypeId::of::<T>();
        _LOCAL_TABLE.with_borrow(|table| {
            let type_map = table.get(&type_id)?;
            let value = type_map.get(name)?;
            let value = value.downcast_ref::<T>()?;
            Some(func(value))
        })
    }

    /// 使用新值替换注册表中的指定键对应的值
    ///
    /// 如果键不存在,则返回 `None` 并且不会注册新值;否则,返回旧值
    ///
    /// # 示例
    /// ```rust
    /// use gom::LocalRegistry;
    ///
    /// LocalRegistry::<i32>::register("my_key", 42);
    /// assert_eq!(LocalRegistry::<i32>::replace("my_key", 64), Some(42));
    /// assert_eq!(LocalRegistry::<i32>::replace("other_key", 32), None);
    /// ```
    pub fn replace(name: &str, value: T) -> Option<T> {
        let type_id = TypeId::of::<T>();
        let value = _LOCAL_TABLE.with_borrow_mut(|table| {
            let type_map = table.get_mut(&type_id)?;
            type_map.insert(name.to_string(), Box::new(value))
        })?;
        let value = value.downcast::<T>().ok()?;
        Some(*value)
    }
}

/// Make a identifier string with the given path
///
/// ```rust
/// use gom::id;
///
/// const MY_ID: &str = id!(my.module.MyType);
/// const OTHER_ID: &str = id!(@MY_ID.other.OtherType);
///
/// assert_eq!(MY_ID, ".my.module.MyType");
/// assert_eq!(OTHER_ID, ".my.module.MyType.other.OtherType");
/// ```
#[macro_export]
macro_rules! id {
    ($($x:ident).+) => {
        concat!($('.', stringify!($x)),+)
    };
    (@ $root:ident . $($x:ident).+) => {
        constcat::concat!($root, concat!($('.', stringify!($x)),+))
    }
}