mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-01-12 02:56:42 +01:00
This commit is contained in:
parent
194f2f9874
commit
277f96d5cc
63 changed files with 1389 additions and 909 deletions
64
output/src/thunk.rs
Normal file
64
output/src/thunk.rs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
use crate::*;
|
||||
|
||||
pub struct Group<T>(T);
|
||||
|
||||
impl<T> Group<T> {
|
||||
pub const fn new () -> Group<()> {
|
||||
Group(())
|
||||
}
|
||||
pub const fn add <U> (self, value: U) -> Group<(T, U)> {
|
||||
Group((self.0, value))
|
||||
}
|
||||
}
|
||||
|
||||
/// Lazily-evaluated [Draw]able.
|
||||
pub struct Thunk<E: Out, T: Draw<E>, F: Fn()->T>(
|
||||
PhantomData<E>,
|
||||
F
|
||||
);
|
||||
impl<E: Out, T: Draw<E>, F: Fn()->T> Thunk<E, T, F> {
|
||||
pub const fn new (thunk: F) -> Self {
|
||||
Self(PhantomData, thunk)
|
||||
}
|
||||
}
|
||||
impl<E: Out, T: Draw<E> + Layout<E>, F: Fn()->T> Content<E> for Thunk<E, T, F> {
|
||||
fn content (&self) -> impl Draw<E> + Layout<E> + '_ {
|
||||
(self.1)()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ThunkDraw<E: Out, F: Fn(&mut E)>(PhantomData<E>, F);
|
||||
impl<E: Out, F: Fn(&mut E)> ThunkDraw<E, F> {
|
||||
pub fn new (render: F) -> Self { Self(PhantomData, render) }
|
||||
}
|
||||
impl<E: Out, F: Fn(&mut E)> Draw<E> for ThunkDraw<E, F> {
|
||||
fn draw (&self, to: &mut E) { (self.1)(to) }
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)] pub struct Memo<T, U> {
|
||||
pub value: T,
|
||||
pub view: Arc<RwLock<U>>
|
||||
}
|
||||
|
||||
impl<T: PartialEq, U> Memo<T, U> {
|
||||
pub fn new (value: T, view: U) -> Self {
|
||||
Self { value, view: Arc::new(view.into()) }
|
||||
}
|
||||
pub fn update <R> (
|
||||
&mut self,
|
||||
newval: T,
|
||||
render: impl Fn(&mut U, &T, &T)->R
|
||||
) -> Option<R> {
|
||||
if newval != self.value {
|
||||
let result = render(&mut*self.view.write().unwrap(), &newval, &self.value);
|
||||
self.value = newval;
|
||||
return Some(result);
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear a pre-allocated buffer, then write into it.
|
||||
#[macro_export] macro_rules! rewrite {
|
||||
($buf:ident, $($rest:tt)*) => { |$buf,_,_|{ $buf.clear(); write!($buf, $($rest)*) } }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue