add lifetime to Draw trait
Some checks failed
/ build (push) Has been cancelled

This commit is contained in:
facile pop culture reference 2026-08-18 10:46:20 +03:00
parent 20517f09b4
commit a9f2fa2cdd
17 changed files with 655 additions and 491 deletions

View file

@ -1,11 +1,10 @@
use crate::*;
fn_kw_layout!(kw_when |state, output, expr| {
Ok(matches!(expr.head()?, Some("when")).then(||{
when(state.namespace(expr.tail().head())?.unwrap(),
draw(move|output: &mut O|{state.interpret(output, &expr.tail().tail().head())})
).draw(output)
}).transpose()?.flatten())
let thunk = draw(move|screen|ok_flat(expr.nth(2)?.map(|x: &'a str|state.interpret(screen, x))));
ok_flat(matches!(expr.head()?, Some("when")).then(||{
when(state.namespace(expr.nth(1)?)?.unwrap(), thunk) .draw(output)
}))
});
/// Only render when condition is true.
@ -16,21 +15,16 @@ fn_kw_layout!(kw_when |state, output, expr| {
/// when(true, "Yes")
/// # }
/// ```
pub const fn when <T: Screen> (condition: bool, item: impl Draw<T>) -> impl Draw<T> {
pub const fn when <'a, T: Screen> (condition: bool, item: impl Draw<'a, T>) -> impl Draw<'a, T> {
draw(move|to: &mut T|if condition { item.draw(to) } else { Ok(Default::default()) })
}
fn_kw_layout!(kw_either |state, output, expr| {
Ok(matches!(expr.head()?, Some("either")).then(||{
either(state.namespace(expr.tail().head()?)?.unwrap(),
draw(move|output: &mut O|{
state.interpret(output, &expr.tail().tail().head()?)
}),
draw(move|output: &mut O|{
state.interpret(output, &expr.tail().tail().tail().head()?)
}),
).draw(output)
}).transpose()?.flatten())
let thunk_a = draw(move|screen|ok_flat(expr.nth(2)?.map(|x: &'a str|state.interpret(screen, x))));
let thunk_b = draw(move|screen|ok_flat(expr.nth(3)?.map(|x: &'a str|state.interpret(screen, x))));
ok_flat(matches!(expr.head()?, Some("either")).then(||{
either(state.namespace(expr.nth(1)?)?.unwrap(), thunk_a, thunk_b).draw(output)
}))
});
/// Render one thing if a condition is true and another false.
@ -41,6 +35,8 @@ fn_kw_layout!(kw_either |state, output, expr| {
/// either(true, "Yes", "No")
/// # }
/// ```
pub const fn either <T: Screen> (condition: bool, a: impl Draw<T>, b: impl Draw<T>) -> impl Draw<T> {
pub const fn either <'a, T: Screen> (
condition: bool, a: impl Draw<'a, T>, b: impl Draw<'a, T>
) -> impl Draw<'a, T> {
draw(move|to: &mut T|if condition { a.draw(to) } else { b.draw(to) })
}