ratatui_kit/element/
any_element.rs1use super::{Element, ElementKey, element_ext::ElementRepr};
2use crate::{
3 component::{Component, ComponentHelper, ComponentHelperExt},
4 props::AnyProps,
5};
6
7pub struct AnyElement<'a> {
8 key: ElementKey,
9 props: AnyProps<'a>,
10 helper: Box<dyn ComponentHelperExt>,
11}
12
13impl<'a, T> From<Element<'a, T>> for AnyElement<'a>
14where
15 T: Component,
16{
17 fn from(value: Element<'a, T>) -> Self {
18 Self {
19 key: value.key,
20 props: AnyProps::owned(value.props, ComponentHelper::<T>::props_type_id()),
21 helper: ComponentHelper::<T>::boxed(),
22 }
23 }
24}
25
26impl<'a, 'b: 'a, T> From<&'a mut Element<'b, T>> for AnyElement<'a>
27where
28 T: Component,
29{
30 fn from(value: &'a mut Element<'b, T>) -> Self {
31 Self {
32 key: value.key.clone(),
33 props: AnyProps::borrowed(&mut value.props, ComponentHelper::<T>::props_type_id()),
34 helper: ComponentHelper::<T>::boxed(),
35 }
36 }
37}
38
39impl<'a, 'b: 'a> From<&'a mut AnyElement<'b>> for AnyElement<'a> {
40 fn from(value: &'a mut AnyElement<'b>) -> Self {
41 Self {
42 key: value.key.clone(),
43 props: value.props.borrow(),
44 helper: value.helper.copy(),
45 }
46 }
47}
48
49impl<'a> ElementRepr for AnyElement<'a> {
50 fn key(&self) -> &ElementKey {
51 &self.key
52 }
53
54 fn helper(&self) -> Box<dyn ComponentHelperExt> {
55 self.helper.copy()
56 }
57
58 fn props_mut(&'_ mut self) -> AnyProps<'_> {
59 self.props.borrow()
60 }
61}