|
1 | 1 | use std::any::TypeId; |
2 | 2 | use std::borrow::Cow; |
| 3 | +use std::collections::hash_map::Entry; |
3 | 4 | use std::collections::HashMap; |
| 5 | +use std::fmt; |
4 | 6 |
|
5 | 7 | use once_cell::sync::Lazy; |
6 | 8 | use parking_lot::RwLock; |
7 | 9 |
|
8 | 10 | use crate::export::NativeClass; |
| 11 | +use crate::init::InitLevel; |
9 | 12 |
|
10 | 13 | static CLASS_REGISTRY: Lazy<RwLock<HashMap<TypeId, ClassInfo>>> = |
11 | 14 | Lazy::new(|| RwLock::new(HashMap::new())); |
12 | 15 |
|
| 16 | +#[derive(Clone, Debug)] |
13 | 17 | pub(crate) struct ClassInfo { |
14 | 18 | pub name: Cow<'static, str>, |
| 19 | + pub init_level: InitLevel, |
15 | 20 | } |
16 | 21 |
|
17 | 22 | /// Access the [`ClassInfo`] of the class `C`. |
@@ -40,12 +45,89 @@ pub(crate) fn class_name_or_default<C: NativeClass>() -> Cow<'static, str> { |
40 | 45 | class_name::<C>().unwrap_or_else(|| Cow::Borrowed(std::any::type_name::<C>())) |
41 | 46 | } |
42 | 47 |
|
43 | | -/// Registers the class `C` in the class registry, using a custom name. |
44 | | -/// Returns the old `ClassInfo` if `C` was already added. |
| 48 | +/// Registers the class `C` in the class registry, using a custom name at the given level. |
| 49 | +/// Returns `Ok(true)` if FFI registration needs to be performed. `Ok(false)` if the class has |
| 50 | +/// already been registered on another level. |
| 51 | +/// Returns an error with the old `ClassInfo` if a conflicting entry for `C` was already added. |
45 | 52 | #[inline] |
46 | | -pub(crate) fn register_class_as<C: NativeClass>(name: Cow<'static, str>) -> Option<ClassInfo> { |
| 53 | +pub(crate) fn register_class_as<C: NativeClass>( |
| 54 | + name: Cow<'static, str>, |
| 55 | + init_level: InitLevel, |
| 56 | +) -> Result<bool, RegisterError> { |
47 | 57 | let type_id = TypeId::of::<C>(); |
48 | | - CLASS_REGISTRY.write().insert(type_id, ClassInfo { name }) |
| 58 | + let mut registry = CLASS_REGISTRY.write(); |
| 59 | + match registry.entry(type_id) { |
| 60 | + Entry::Vacant(entry) => { |
| 61 | + entry.insert(ClassInfo { name, init_level }); |
| 62 | + Ok(true) |
| 63 | + } |
| 64 | + Entry::Occupied(entry) => { |
| 65 | + let class_info = entry.get(); |
| 66 | + let kind = if class_info.name != name { |
| 67 | + Some(RegisterErrorKind::ConflictingName) |
| 68 | + } else if class_info.init_level.intersects(init_level) { |
| 69 | + Some(RegisterErrorKind::AlreadyOnSameLevel) |
| 70 | + } else { |
| 71 | + None |
| 72 | + }; |
| 73 | + |
| 74 | + if let Some(kind) = kind { |
| 75 | + Err(RegisterError { |
| 76 | + class_info: class_info.clone(), |
| 77 | + type_name: std::any::type_name::<C>(), |
| 78 | + kind, |
| 79 | + }) |
| 80 | + } else { |
| 81 | + Ok(false) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[inline] |
| 88 | +#[allow(dead_code)] // Currently unused on platforms with inventory support |
| 89 | +pub(crate) fn types_with_init_level(allow: InitLevel, deny: InitLevel) -> Vec<Cow<'static, str>> { |
| 90 | + let registry = CLASS_REGISTRY.read(); |
| 91 | + let mut list = registry |
| 92 | + .values() |
| 93 | + .filter_map(|class_info| { |
| 94 | + (class_info.init_level.intersects(allow) && !class_info.init_level.intersects(deny)) |
| 95 | + .then(|| class_info.name.clone()) |
| 96 | + }) |
| 97 | + .collect::<Vec<_>>(); |
| 98 | + |
| 99 | + list.sort_unstable(); |
| 100 | + list |
| 101 | +} |
| 102 | + |
| 103 | +#[derive(Debug)] |
| 104 | +pub(crate) struct RegisterError { |
| 105 | + pub type_name: &'static str, |
| 106 | + pub class_info: ClassInfo, |
| 107 | + pub kind: RegisterErrorKind, |
| 108 | +} |
| 109 | + |
| 110 | +#[derive(Debug)] |
| 111 | +pub(crate) enum RegisterErrorKind { |
| 112 | + ConflictingName, |
| 113 | + AlreadyOnSameLevel, |
| 114 | +} |
| 115 | + |
| 116 | +impl fmt::Display for RegisterError { |
| 117 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 118 | + match self.kind { |
| 119 | + RegisterErrorKind::ConflictingName => { |
| 120 | + write!( |
| 121 | + f, |
| 122 | + "`{}` has already been registered as `{}`", |
| 123 | + self.type_name, self.class_info.name |
| 124 | + ) |
| 125 | + } |
| 126 | + RegisterErrorKind::AlreadyOnSameLevel => { |
| 127 | + write!(f, "`{}` has already been registered", self.type_name) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
49 | 131 | } |
50 | 132 |
|
51 | 133 | /// Clears the registry |
|
0 commit comments