#![warn(missing_docs)]
use crate::api::ComponentHandle;
use crate::item_tree::{ItemTreeRc, ItemTreeVTable, ItemTreeWeak};
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
use alloc::rc::Rc;
use core::fmt::Debug;
pub struct FactoryContext {
pub parent_item_tree: ItemTreeWeak,
pub parent_item_tree_index: u32,
}
#[derive(Clone)]
struct ComponentFactoryInner(Rc<dyn Fn(FactoryContext) -> Option<ItemTreeRc> + 'static>);
impl PartialEq for ComponentFactoryInner {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
}
impl Debug for ComponentFactoryInner {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("ComponentFactoryData").finish()
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ComponentFactory(Option<ComponentFactoryInner>);
impl ComponentFactory {
pub fn new<T: ComponentHandle + 'static>(
factory: impl Fn(FactoryContext) -> Option<T> + 'static,
) -> Self
where
T::Inner: vtable::HasStaticVTable<ItemTreeVTable> + 'static,
{
let factory = Box::new(factory) as Box<dyn Fn(FactoryContext) -> Option<T> + 'static>;
Self(Some(ComponentFactoryInner(Rc::new(move |ctx| -> Option<ItemTreeRc> {
let product = (factory)(ctx);
product.map(|p| vtable::VRc::into_dyn(p.as_weak().inner().upgrade().unwrap()))
}))))
}
pub(crate) fn build(&self, ctx: FactoryContext) -> Option<ItemTreeRc> {
self.0.as_ref().and_then(move |b| (b.0)(ctx))
}
}