🔥 Refactor TypeId/Any use, drop own Handle

This commit is contained in:
Manuel Cillero 2024-02-16 17:00:34 +01:00
parent 8402b7946e
commit 169e562488
59 changed files with 137 additions and 289 deletions

View file

@ -1,10 +1,10 @@
use crate::core::action::{Action, ActionsList};
use crate::{Handle, LazyStatic};
use crate::{LazyStatic, TypeId};
use std::collections::HashMap;
use std::sync::RwLock;
pub type KeyAction = (Handle, Option<Handle>, Option<String>);
pub type KeyAction = (TypeId, Option<TypeId>, Option<String>);
// Registered actions.
static ACTIONS: LazyStatic<RwLock<HashMap<KeyAction, ActionsList>>> =
@ -13,8 +13,8 @@ static ACTIONS: LazyStatic<RwLock<HashMap<KeyAction, ActionsList>>> =
pub fn add_action(action: Action) {
let mut actions = ACTIONS.write().unwrap();
let key_action = (
action.handle(),
action.referer_handle(),
action.type_id(),
action.referer_type_id(),
action.referer_id(),
);
if let Some(list) = actions.get_mut(&key_action) {

View file

@ -1,13 +1,8 @@
use crate::{Handle, ImplementHandle, Weight};
use crate::core::AnyBase;
use crate::{TypeId, Weight};
use std::any::Any;
pub trait ActionBase: Any {
fn as_ref_any(&self) -> &dyn Any;
}
pub trait ActionTrait: ActionBase + ImplementHandle + Send + Sync {
fn referer_handle(&self) -> Option<Handle> {
pub trait ActionTrait: AnyBase + Send + Sync {
fn referer_type_id(&self) -> Option<TypeId> {
None
}
@ -20,12 +15,6 @@ pub trait ActionTrait: ActionBase + ImplementHandle + Send + Sync {
}
}
impl<C: ActionTrait> ActionBase for C {
fn as_ref_any(&self) -> &dyn Any {
self
}
}
pub fn action_ref<A: 'static>(action: &dyn ActionTrait) -> &A {
action.as_ref_any().downcast_ref::<A>().unwrap()
action.as_any_ref().downcast_ref::<A>().unwrap()
}