mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
42 lines
1 KiB
Rust
42 lines
1 KiB
Rust
|
|
pub enum Collected<'a> {
|
|
Box(Box<dyn Render + 'a>),
|
|
Ref(&'a (dyn Render + 'a)),
|
|
None
|
|
}
|
|
|
|
impl<'a> Render for Collected<'a> {
|
|
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
|
|
match self {
|
|
Self::Box(item) => (*item).render(buf, area),
|
|
Self::Ref(item) => (*item).render(buf, area),
|
|
Self::None => Ok(area),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct Collector<'a>(pub Vec<Collected<'a>>);
|
|
|
|
impl<'a, R: Render + 'a> FnOnce<(R)> for Collector<'a> {
|
|
type Output = ();
|
|
extern "rust-call" fn call_once (self, (device, ): (R,)) -> Self::Output {
|
|
self.add(widget.into_collected());
|
|
}
|
|
}
|
|
|
|
impl<'a> Collector<'a> {
|
|
pub fn collect (collect: impl Fn(&mut Collector<'a>)) -> Self {
|
|
let mut items = Self(vec![]);
|
|
collect(&mut items);
|
|
items
|
|
}
|
|
fn add (mut self, widget: Collected<'a>) -> Self {
|
|
self.0.push(widget);
|
|
self
|
|
}
|
|
}
|
|
|
|
pub trait Collection<'a, T, U> {
|
|
fn add (self, widget: impl Render + 'a) -> Self;
|
|
}
|
|
|