💥 Oculta as_ref_any() y as_mut_any() en la API

This commit is contained in:
Manuel Cillero 2023-07-11 21:17:35 +02:00
parent 0880964fd1
commit 9a49186ed2
25 changed files with 41 additions and 196 deletions

View file

@ -1,5 +1,5 @@
mod definition;
pub use definition::{action_ref, ActionTrait, AnyAction};
pub use definition::{action_ref, ActionTrait, BaseAction};
mod list;
pub use list::Action;

View file

@ -1,8 +1,12 @@
use crate::Handle;
pub use std::any::Any as AnyAction;
use std::any::Any;
pub trait ActionTrait: AnyAction + Send + Sync {
pub trait BaseAction: Any {
fn as_ref_any(&self) -> &dyn Any;
}
pub trait ActionTrait: BaseAction + Send + Sync {
fn new() -> Self
where
Self: Sized;
@ -12,8 +16,12 @@ pub trait ActionTrait: AnyAction + Send + Sync {
fn weight(&self) -> isize {
0
}
}
fn as_ref_any(&self) -> &dyn AnyAction;
impl<C: ActionTrait> BaseAction for C {
fn as_ref_any(&self) -> &dyn Any {
self
}
}
pub fn action_ref<A: 'static>(action: &dyn ActionTrait) -> &A {

View file

@ -2,7 +2,7 @@ mod context;
pub use context::{Context, ContextOp};
mod definition;
pub use definition::{component_mut, component_ref, AnyComponent, BaseComponent, ComponentTrait};
pub use definition::{component_mut, component_ref, BaseComponent, ComponentTrait};
mod one;
pub use one::OneComponent;
@ -50,10 +50,6 @@ macro_rules! actions_for_component {
fn weight(&self) -> isize {
self.weight
}
fn as_ref_any(&self) -> &dyn AnyAction {
self
}
}
impl [<BeforePrepare $Component>] {
@ -113,10 +109,6 @@ macro_rules! actions_for_component {
fn weight(&self) -> isize {
self.weight
}
fn as_ref_any(&self) -> &dyn AnyAction {
self
}
}
impl [<AfterPrepare $Component>] {

View file

@ -2,13 +2,17 @@ use crate::core::component::Context;
use crate::html::{html, Markup, PrepareMarkup};
use crate::{util, Handle};
pub use std::any::Any as AnyComponent;
use std::any::Any;
pub trait BaseComponent {
pub trait BaseComponent: Any {
fn prepare(&mut self, cx: &mut Context) -> Markup;
fn as_ref_any(&self) -> &dyn Any;
fn as_mut_any(&mut self) -> &mut dyn Any;
}
pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync {
pub trait ComponentTrait: BaseComponent + Send + Sync {
fn new() -> Self
where
Self: Sized;
@ -46,10 +50,6 @@ pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync {
#[allow(unused_variables)]
fn after_prepare_component(&mut self, cx: &mut Context) {}
fn as_ref_any(&self) -> &dyn AnyComponent;
fn as_mut_any(&mut self) -> &mut dyn AnyComponent;
}
impl<C: ComponentTrait> BaseComponent for C {
@ -77,12 +77,20 @@ impl<C: ComponentTrait> BaseComponent for C {
html! {}
}
}
fn as_ref_any(&self) -> &dyn Any {
self
}
fn as_mut_any(&mut self) -> &mut dyn Any {
self
}
}
pub fn component_ref<C: 'static>(component: &dyn ComponentTrait) -> &C {
pub fn component_ref<C: ComponentTrait>(component: &dyn ComponentTrait) -> &C {
component.as_ref_any().downcast_ref::<C>().unwrap()
}
pub fn component_mut<C: 'static>(component: &mut dyn ComponentTrait) -> &mut C {
pub fn component_mut<C: ComponentTrait>(component: &mut dyn ComponentTrait) -> &mut C {
component.as_mut_any().downcast_mut::<C>().unwrap()
}

View file

@ -1,4 +1,6 @@
use crate::prelude::*;
use crate::core::component::{ComponentTrait, Context};
use crate::html::{html, Markup, PrepareMarkup};
use crate::{fn_builder, use_handle, Handle};
use_handle!(COMPONENT_HTML);
@ -17,14 +19,6 @@ impl ComponentTrait for Html {
fn prepare_component(&self, _: &mut Context) -> PrepareMarkup {
PrepareMarkup::With(html! { (self.html()) })
}
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Html {

View file

@ -1,4 +1,7 @@
use crate::prelude::*;
use crate::core::component::{ComponentTrait, Context};
use crate::html::{html, PreEscaped, PrepareMarkup};
use crate::locale::{Loader, Locales};
use crate::{fn_builder, use_handle, Handle};
use std::collections::HashMap;
@ -60,14 +63,6 @@ impl ComponentTrait for L10n {
}),
}
}
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl L10n {

View file

@ -1,4 +1,4 @@
use crate::core::component::{AnyComponent, ComponentTrait, Context};
use crate::core::component::{ComponentTrait, Context};
use crate::html::{html, PrepareMarkup};
use crate::{use_handle, Handle};
@ -22,12 +22,4 @@ impl ComponentTrait for Error403 {
}
})
}
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}

View file

@ -1,4 +1,4 @@
use crate::core::component::{AnyComponent, ComponentTrait, Context};
use crate::core::component::{ComponentTrait, Context};
use crate::html::{html, PrepareMarkup};
use crate::{use_handle, Handle};
@ -22,12 +22,4 @@ impl ComponentTrait for Error404 {
}
})
}
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}

View file

@ -1,4 +1,4 @@
use crate::core::action::{action_ref, run_actions, ActionTrait, AnyAction};
use crate::core::action::{action_ref, run_actions, ActionTrait};
use crate::response::page::action::ActionPage;
use crate::response::page::Page;
use crate::{use_handle, Handle};
@ -25,10 +25,6 @@ impl ActionTrait for ActionAfterPrepareBody {
fn weight(&self) -> isize {
self.weight
}
fn as_ref_any(&self) -> &dyn AnyAction {
self
}
}
impl ActionAfterPrepareBody {

View file

@ -1,4 +1,4 @@
use crate::core::action::{action_ref, run_actions, ActionTrait, AnyAction};
use crate::core::action::{action_ref, run_actions, ActionTrait};
use crate::response::page::action::ActionPage;
use crate::response::page::Page;
use crate::{use_handle, Handle};
@ -25,10 +25,6 @@ impl ActionTrait for ActionBeforePrepareBody {
fn weight(&self) -> isize {
self.weight
}
fn as_ref_any(&self) -> &dyn AnyAction {
self
}
}
impl ActionBeforePrepareBody {