constify and deinline some methods

This commit is contained in:
🪞👃🪞 2025-04-13 21:11:07 +03:00
parent 4279503681
commit 18a01b8355
10 changed files with 124 additions and 92 deletions

View file

@ -2,28 +2,39 @@ use crate::*;
use std::marker::PhantomData;
/// Lazily-evaluated [Render]able.
pub struct Thunk<E: Output, T: Render<E>, F: Fn()->T + Send + Sync>(PhantomData<E>, F);
pub struct Thunk<E: Output, T: Render<E>, F: Fn()->T + Send + Sync>(
PhantomData<E>,
F
);
impl<E: Output, T: Render<E>, F: Fn()->T + Send + Sync> Thunk<E, T, F> {
pub fn new (thunk: F) -> Self {
Self(Default::default(), thunk)
pub const fn new (thunk: F) -> Self {
Self(PhantomData, thunk)
}
}
impl<E: Output, T: Render<E>, F: Fn()->T + Send + Sync> Content<E> for Thunk<E, T, F> {
fn content (&self) -> impl Render<E> { (self.1)() }
}
pub struct ThunkBox<'a, E: Output>(PhantomData<E>, Box<dyn Fn()->RenderBox<'a, E> + Send + Sync + 'a>);
pub struct ThunkBox<'a, E: Output>(
PhantomData<E>,
Box<dyn Fn()->RenderBox<'a, E> + Send + Sync + 'a>
);
impl<'a, E: Output> ThunkBox<'a, E> {
pub fn new (thunk: Box<dyn Fn()->RenderBox<'a, E> + Send + Sync + 'a>) -> Self {
Self(Default::default(), thunk)
pub const fn new (thunk: Box<dyn Fn()->RenderBox<'a, E> + Send + Sync + 'a>) -> Self {
Self(PhantomData, thunk)
}
}
impl<'a, E: Output> Content<E> for ThunkBox<'a, E> {
fn content (&self) -> impl Render<E> { (self.1)() }
}
impl<'a, E: Output, F: Fn()->T + Send + Sync + 'a, T: Render<E> + Send + Sync + 'a> From<F> for ThunkBox<'a, E> {
impl<'a, E, F, T> From<F> for ThunkBox<'a, E>
where
E: Output,
F: Fn()->T + Send + Sync + 'a,
T: Render<E> + Send + Sync + 'a
{
fn from (f: F) -> Self {
Self(Default::default(), Box::new(move||f().boxed()))
Self(PhantomData, Box::new(move||f().boxed()))
}
}
//impl<'a, E: Output, F: Fn()->Box<dyn Render<E> + 'a> + Send + Sync + 'a> From<F> for ThunkBox<'a, E> {
@ -34,17 +45,30 @@ impl<'a, E: Output, F: Fn()->T + Send + Sync + 'a, T: Render<E> + Send + Sync +
pub struct ThunkRender<E: Output, F: Fn(&mut E) + Send + Sync>(PhantomData<E>, F);
impl<E: Output, F: Fn(&mut E) + Send + Sync> ThunkRender<E, F> {
pub fn new (render: F) -> Self { Self(Default::default(), render) }
pub fn new (render: F) -> Self { Self(PhantomData, render) }
}
impl<E: Output, F: Fn(&mut E) + Send + Sync> Content<E> for ThunkRender<E, F> {
fn render (&self, to: &mut E) { (self.1)(to) }
}
pub struct ThunkLayout<E: Output, F1: Fn(E::Area)->E::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync>(PhantomData<E>, F1, F2);
pub struct ThunkLayout<
E: Output,
F1: Fn(E::Area)->E::Area + Send + Sync,
F2: Fn(&mut E) + Send + Sync
>(
PhantomData<E>,
F1,
F2
);
impl<E: Output, F1: Fn(E::Area)->E::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync> ThunkLayout<E, F1, F2> {
pub fn new (layout: F1, render: F2) -> Self { Self(Default::default(), layout, render) }
pub fn new (layout: F1, render: F2) -> Self { Self(PhantomData, layout, render) }
}
impl<E: Output, F1: Fn(E::Area)->E::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync> Content<E> for ThunkLayout<E, F1, F2> {
impl<E, F1, F2> Content<E> for ThunkLayout<E, F1, F2>
where
E: Output,
F1: Fn(E::Area)->E::Area + Send + Sync,
F2: Fn(&mut E) + Send + Sync
{
fn layout (&self, to: E::Area) -> E::Area { (self.1)(to) }
fn render (&self, to: &mut E) { (self.2)(to) }
}