|
1 | | -use crate::export::NativeClass; |
| 1 | +use std::any::TypeId; |
| 2 | +use std::borrow::Cow; |
| 3 | +use std::collections::HashMap; |
| 4 | + |
2 | 5 | use once_cell::sync::Lazy; |
3 | 6 | use parking_lot::RwLock; |
4 | | -use std::any::TypeId; |
5 | | -use std::collections::HashSet; |
6 | 7 |
|
7 | | -static CLASS_REGISTRY: Lazy<RwLock<HashSet<TypeId>>> = Lazy::new(|| RwLock::new(HashSet::new())); |
| 8 | +use crate::export::NativeClass; |
| 9 | + |
| 10 | +static CLASS_REGISTRY: Lazy<RwLock<HashMap<TypeId, ClassInfo>>> = |
| 11 | + Lazy::new(|| RwLock::new(HashMap::new())); |
| 12 | + |
| 13 | +pub(crate) struct ClassInfo { |
| 14 | + pub name: Cow<'static, str>, |
| 15 | +} |
8 | 16 |
|
9 | | -/// Can be used to validate whether or not `C` has been added using `InitHandle::add_class<C>()` |
10 | | -/// Returns true if added otherwise false. |
| 17 | +/// Access the [`ClassInfo`] of the class `C`. |
11 | 18 | #[inline] |
12 | | -pub(crate) fn is_class_registered<C: NativeClass>() -> bool { |
13 | | - let type_id = TypeId::of::<C>(); |
14 | | - CLASS_REGISTRY.read().contains(&type_id) |
| 19 | +pub(crate) fn with_class_info<C: NativeClass, F, R>(f: F) -> Option<R> |
| 20 | +where |
| 21 | + F: FnOnce(&ClassInfo) -> R, |
| 22 | +{ |
| 23 | + CLASS_REGISTRY.read().get(&TypeId::of::<C>()).map(f) |
| 24 | +} |
| 25 | + |
| 26 | +/// Returns the NativeScript name of the class `C` if it is registered. |
| 27 | +/// Can also be used to validate whether or not `C` has been added using `InitHandle::add_class<C>()` |
| 28 | +#[inline] |
| 29 | +pub(crate) fn class_name<C: NativeClass>() -> Option<Cow<'static, str>> { |
| 30 | + with_class_info::<C, _, _>(|i| i.name.clone()) |
| 31 | +} |
| 32 | + |
| 33 | +/// Returns the NativeScript name of the class `C` if it is registered, or a best-effort description |
| 34 | +/// of the type otherwise. |
| 35 | +/// |
| 36 | +/// The returned string should only be used for diagnostic purposes, not for types that the user |
| 37 | +/// has to name explicitly. The format is not guaranteed. |
| 38 | +#[inline] |
| 39 | +pub(crate) fn class_name_or_default<C: NativeClass>() -> Cow<'static, str> { |
| 40 | + class_name::<C>().unwrap_or_else(|| Cow::Borrowed(std::any::type_name::<C>())) |
15 | 41 | } |
16 | 42 |
|
17 | | -/// Registers the class `C` in the class registry. |
18 | | -/// Returns `true` if `C` was not already present in the list. |
19 | | -/// Returns `false` if `C` was already added. |
| 43 | +/// Registers the class `C` in the class registry, using a custom name. |
| 44 | +/// Returns the old `ClassInfo` if `C` was already added. |
20 | 45 | #[inline] |
21 | | -pub(crate) fn register_class<C: NativeClass>() -> bool { |
| 46 | +pub(crate) fn register_class_as<C: NativeClass>(name: Cow<'static, str>) -> Option<ClassInfo> { |
22 | 47 | let type_id = TypeId::of::<C>(); |
23 | | - CLASS_REGISTRY.write().insert(type_id) |
| 48 | + CLASS_REGISTRY.write().insert(type_id, ClassInfo { name }) |
24 | 49 | } |
25 | 50 |
|
26 | 51 | /// Clears the registry |
|
0 commit comments