💥 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

@ -76,14 +76,6 @@ impl ComponentTrait for MegaMenuItem {
}), }),
} }
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl MegaMenuItem { impl MegaMenuItem {
@ -222,14 +214,6 @@ impl ComponentTrait for MegaMenu {
fn after_prepare_component(&mut self, cx: &mut Context) { fn after_prepare_component(&mut self, cx: &mut Context) {
run_actions_after_prepare_megamenu(self, cx); run_actions_after_prepare_megamenu(self, cx);
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl MegaMenu { impl MegaMenu {

View file

@ -84,14 +84,6 @@ impl ComponentTrait for Anchor {
} }
}) })
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Anchor { impl Anchor {

View file

@ -58,14 +58,6 @@ impl ComponentTrait for Block {
fn after_prepare_component(&mut self, cx: &mut Context) { fn after_prepare_component(&mut self, cx: &mut Context) {
run_actions_after_prepare_block(self, cx); run_actions_after_prepare_block(self, cx);
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Block { impl Block {

View file

@ -95,14 +95,6 @@ impl ComponentTrait for Container {
fn after_prepare_component(&mut self, cx: &mut Context) { fn after_prepare_component(&mut self, cx: &mut Context) {
run_actions_after_prepare_container(self, cx); run_actions_after_prepare_container(self, cx);
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Container { impl Container {

View file

@ -67,14 +67,6 @@ impl ComponentTrait for Button {
} }
}) })
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Button { impl Button {

View file

@ -72,14 +72,6 @@ impl ComponentTrait for Date {
} }
}) })
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Date { impl Date {

View file

@ -73,14 +73,6 @@ impl ComponentTrait for Form {
fn after_prepare_component(&mut self, cx: &mut Context) { fn after_prepare_component(&mut self, cx: &mut Context) {
run_actions_after_prepare_form(self, cx); run_actions_after_prepare_form(self, cx);
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Form { impl Form {

View file

@ -29,14 +29,6 @@ impl ComponentTrait for Hidden {
input type="hidden" id=[id] name=[self.name().get()] value=[self.value().get()]; input type="hidden" id=[id] name=[self.name().get()] value=[self.value().get()];
}) })
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Hidden { impl Hidden {

View file

@ -106,14 +106,6 @@ impl ComponentTrait for Input {
} }
}) })
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Input { impl Input {

View file

@ -84,14 +84,6 @@ impl ComponentTrait for Column {
fn after_prepare_component(&mut self, cx: &mut Context) { fn after_prepare_component(&mut self, cx: &mut Context) {
run_actions_after_prepare_column(self, cx); run_actions_after_prepare_column(self, cx);
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Column { impl Column {

View file

@ -53,14 +53,6 @@ impl ComponentTrait for Row {
fn after_prepare_component(&mut self, cx: &mut Context) { fn after_prepare_component(&mut self, cx: &mut Context) {
run_actions_after_prepare_row(self, cx); run_actions_after_prepare_row(self, cx);
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Row { impl Row {

View file

@ -73,14 +73,6 @@ impl ComponentTrait for Heading {
HeadingType::H6 => h6 id=[id] class=[classes] { (self.text().prepare(cx)) }, HeadingType::H6 => h6 id=[id] class=[classes] { (self.text().prepare(cx)) },
}}) }})
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Heading { impl Heading {

View file

@ -37,14 +37,6 @@ impl ComponentTrait for Icon {
fn prepare_component(&self, _: &mut Context) -> PrepareMarkup { fn prepare_component(&self, _: &mut Context) -> PrepareMarkup {
PrepareMarkup::With(html! { i class=[self.classes().get()] {}; }) PrepareMarkup::With(html! { i class=[self.classes().get()] {}; })
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Icon { impl Icon {

View file

@ -42,14 +42,6 @@ impl ComponentTrait for Image {
class=[self.classes().get()]; class=[self.classes().get()];
}) })
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Image { impl Image {

View file

@ -56,14 +56,6 @@ impl ComponentTrait for Paragraph {
} }
}) })
} }
fn as_ref_any(&self) -> &dyn AnyComponent {
self
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
} }
impl Paragraph { impl Paragraph {

View file

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

View file

@ -1,8 +1,12 @@
use crate::Handle; 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 fn new() -> Self
where where
Self: Sized; Self: Sized;
@ -12,8 +16,12 @@ pub trait ActionTrait: AnyAction + Send + Sync {
fn weight(&self) -> isize { fn weight(&self) -> isize {
0 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 { pub fn action_ref<A: 'static>(action: &dyn ActionTrait) -> &A {

View file

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

View file

@ -2,13 +2,17 @@ use crate::core::component::Context;
use crate::html::{html, Markup, PrepareMarkup}; use crate::html::{html, Markup, PrepareMarkup};
use crate::{util, Handle}; 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 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 fn new() -> Self
where where
Self: Sized; Self: Sized;
@ -46,10 +50,6 @@ pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync {
#[allow(unused_variables)] #[allow(unused_variables)]
fn after_prepare_component(&mut self, cx: &mut Context) {} 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 { impl<C: ComponentTrait> BaseComponent for C {
@ -77,12 +77,20 @@ impl<C: ComponentTrait> BaseComponent for C {
html! {} html! {}
} }
} }
fn as_ref_any(&self) -> &dyn Any {
self
} }
pub fn component_ref<C: 'static>(component: &dyn ComponentTrait) -> &C { fn as_mut_any(&mut self) -> &mut dyn Any {
self
}
}
pub fn component_ref<C: ComponentTrait>(component: &dyn ComponentTrait) -> &C {
component.as_ref_any().downcast_ref::<C>().unwrap() 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() 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); use_handle!(COMPONENT_HTML);
@ -17,14 +19,6 @@ impl ComponentTrait for Html {
fn prepare_component(&self, _: &mut Context) -> PrepareMarkup { fn prepare_component(&self, _: &mut Context) -> PrepareMarkup {
PrepareMarkup::With(html! { (self.html()) }) 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 { 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; 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 { 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::html::{html, PrepareMarkup};
use crate::{use_handle, Handle}; 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::html::{html, PrepareMarkup};
use crate::{use_handle, Handle}; 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::action::ActionPage;
use crate::response::page::Page; use crate::response::page::Page;
use crate::{use_handle, Handle}; use crate::{use_handle, Handle};
@ -25,10 +25,6 @@ impl ActionTrait for ActionAfterPrepareBody {
fn weight(&self) -> isize { fn weight(&self) -> isize {
self.weight self.weight
} }
fn as_ref_any(&self) -> &dyn AnyAction {
self
}
} }
impl ActionAfterPrepareBody { 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::action::ActionPage;
use crate::response::page::Page; use crate::response::page::Page;
use crate::{use_handle, Handle}; use crate::{use_handle, Handle};
@ -25,10 +25,6 @@ impl ActionTrait for ActionBeforePrepareBody {
fn weight(&self) -> isize { fn weight(&self) -> isize {
self.weight self.weight
} }
fn as_ref_any(&self) -> &dyn AnyAction {
self
}
} }
impl ActionBeforePrepareBody { impl ActionBeforePrepareBody {