mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-10 21:56:42 +01:00
wip: refactored core
This commit is contained in:
parent
461c60d6b3
commit
c033a5618b
9 changed files with 75 additions and 67 deletions
41
crates/tek_core/src/collect.rs
Normal file
41
crates/tek_core/src/collect.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use crate::*;
|
||||
|
||||
pub enum Collected<'a, T, U> {
|
||||
Box(Box<dyn Render<T, U> + 'a>),
|
||||
Ref(&'a (dyn Render<T, U> + 'a)),
|
||||
}
|
||||
impl<'a, T, U> Render<T, U> for Collected<'a, T, U> {
|
||||
fn render (&self, to: &mut T) -> Perhaps<U> {
|
||||
match self {
|
||||
Self::Box(item) => (*item).render(to),
|
||||
Self::Ref(item) => (*item).render(to),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct Collection<'a, T, U>(
|
||||
pub Vec<Collected<'a, T, U>>
|
||||
);
|
||||
impl<'a, T, U> Collection<'a, T, U> {
|
||||
pub fn new () -> Self {
|
||||
Self(vec![])
|
||||
}
|
||||
}
|
||||
pub trait Collect<'a, T, U> {
|
||||
fn add_box (self, item: Box<dyn Render<T, U> + 'a>) -> Self;
|
||||
fn add_ref (self, item: &'a dyn Render<T, U>) -> Self;
|
||||
fn add <R: Render<T, U> + Sized + 'a> (self, item: R) -> Self
|
||||
where Self: Sized
|
||||
{
|
||||
self.add_box(Box::new(item))
|
||||
}
|
||||
}
|
||||
impl<'a, T, U> Collect<'a, T, U> for Collection<'a, T, U> {
|
||||
fn add_box (mut self, item: Box<dyn Render<T, U> + 'a>) -> Self {
|
||||
self.0.push(Collected::Box(item));
|
||||
self
|
||||
}
|
||||
fn add_ref (mut self, item: &'a dyn Render<T, U>) -> Self {
|
||||
self.0.push(Collected::Ref(item));
|
||||
self
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue