ferroui_base/
i_support_initialize.rs1use crate::styling::DuplicateSetterError;
2use crate::{ObjectType, Ref, StyledElement, Upcast};
3use std::rc::Rc;
4
5#[derive(Clone, Debug, PartialEq, Eq)]
9pub enum InitializationError {
10 DuplicateSetter(DuplicateSetterError),
13}
14
15impl std::fmt::Display for InitializationError {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 match self {
18 Self::DuplicateSetter(error) => error.fmt(f),
19 }
20 }
21}
22
23impl std::error::Error for InitializationError {}
24
25impl From<DuplicateSetterError> for InitializationError {
26 fn from(error: DuplicateSetterError) -> Self {
27 Self::DuplicateSetter(error)
28 }
29}
30
31pub trait ISupportInitialize {
36 fn begin_init(&self);
38
39 fn end_init(&self);
41
42 fn try_end_init(&self) -> Result<(), InitializationError> {
47 self.end_init();
48 Ok(())
49 }
50
51 #[doc(hidden)]
53 fn reference_id(&self) -> *const () {
54 self as *const Self as *const ()
55 }
56}
57
58impl PartialEq for dyn ISupportInitialize {
62 #[inline]
63 fn eq(&self, other: &Self) -> bool {
64 self.reference_id() == other.reference_id()
65 }
66}
67
68struct StyledElementInitialize(Ref<StyledElement>);
70
71impl ISupportInitialize for StyledElementInitialize {
72 fn begin_init(&self) {
73 self.0.begin_init()
74 }
75
76 fn end_init(&self) {
77 self.0.end_init()
78 }
79
80 fn try_end_init(&self) -> Result<(), InitializationError> {
81 self.0.try_end_init()
82 }
83
84 fn reference_id(&self) -> *const () {
85 &*self.0 as *const StyledElement as *const ()
86 }
87}
88
89impl<T: ObjectType + Upcast<StyledElement>> From<Ref<T>> for Rc<dyn ISupportInitialize> {
90 fn from(value: Ref<T>) -> Self {
91 Rc::new(StyledElementInitialize(value.upcast()))
92 }
93}
94
95impl<T: ObjectType + Upcast<StyledElement>> From<&Ref<T>> for Rc<dyn ISupportInitialize> {
96 fn from(value: &Ref<T>) -> Self {
97 Rc::new(StyledElementInitialize(value.clone().upcast()))
98 }
99}