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

@ -1265,17 +1265,17 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
draw: &impl Fn(&mut Self)->T
) -> T;
/// Determine used area without drawing
fn size (
fn size <'a> (
&mut self,
area: impl Into<Option<XYWH<Self::Unit>>>,
draw: impl Draw<Self>
) -> Perhaps<XYWH<Self::Unit>>;
draw: impl Draw<'a, Self>
) -> Drawn<'a, Self::Unit>;
/// Draw
fn draw (
fn draw <'a> (
&mut self,
area: impl Into<Option<XYWH<Self::Unit>>>,
draw: impl Draw<Self>
) -> Perhaps<XYWH<Self::Unit>>;
draw: impl Draw<'a, Self>
) -> Drawn<'a, Self::Unit>;
}
/// Implement the [Draw] trait for a particular drawable and [Screen].
@ -1290,7 +1290,7 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
#[macro_export] macro_rules! impl_draw (
($(<$($T:ident: $Trait:path,)+>)?|
$self:ident:$Self:path, $to:ident:$To:ty
|$draw:block)=>{ impl$(<$($T:$Trait),+>)? Draw<$To> for $Self {
|$draw:block)=>{ impl$(<$($T:$Trait),+>)? Draw<'_, $To> for $Self {
fn draw (&$self, $to: &mut $To) -> Perhaps<XYWH<<$To as Screen>::Unit>> $draw
} };
($(<$($T:ident: $Trait:path,)+>)?|
@ -1321,8 +1321,8 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
/// }
/// }
/// ```
pub trait Draw<S: Screen> {
fn draw (&self, to: &mut S) -> Drawn<S::Unit>;
pub trait Draw<'a, S: Screen> {
fn draw (&self, to: &mut S) -> Drawn<'a, S::Unit>;
}
/// Emit a [Draw]able.
@ -1345,14 +1345,14 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
/// let _ = view::<Tui, _, _>(||"drawable");
/// let _ = view::<Tui, _, _>(||Some("drawable"));
/// ```
pub const fn view <S: Screen, T: Draw<S>, F: Fn()->T> (view: F) -> impl View<S> {
pub const fn view <S: Screen, T: for<'a> Draw<'a, S>, F: Fn()->T> (view: F) -> impl View<S> {
ViewThunk(view, PhantomData)
}
/// Because we can't implement [Draw] for `F: FnOnce...` without conflicts.
pub struct ViewThunk<S: Screen, F>(pub F, std::marker::PhantomData<S>);
impl<S: Screen, T: Draw<S>, F: Fn()->T> View<S> for ViewThunk<S, F> {
impl<S: Screen, T: for<'a> Draw<'a, S>, F: Fn()->T> View<S> for ViewThunk<S, F> {
fn view (&self) -> impl Draw<S> {
self.0()
}
@ -1361,8 +1361,8 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
/// Because we can't implement [Draw] for `F: FnOnce...` without conflicts.
pub struct DrawThunk<S: Screen, F>(pub F, std::marker::PhantomData<S>);
impl<T: Screen, F: Fn(&mut T)->Perhaps<XYWH<T::Unit>>> Draw<T> for DrawThunk<T, F> {
fn draw (&self, to: &mut T) -> Perhaps<XYWH<T::Unit>> {
impl<'a, T: Screen, F: Fn(&mut T)->PerhapsRef<'a, XYWH<T::Unit>>> Draw<'a, T> for DrawThunk<T, F> {
fn draw (&self, to: &mut T) -> PerhapsRef<'a, XYWH<T::Unit>> {
to.clip(None, &self.0)
}
}
@ -1373,23 +1373,25 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
/// # use tengri::*;
/// let _ = draw(|to: &mut Tui|Ok(Some(to.area()))); // draws nothing
/// ```
pub const fn draw <T: Screen, F: Fn(&mut T)->Perhaps<XYWH<T::Unit>>> (
pub const fn draw <'a, T: Screen, F: Fn(&mut T)->PerhapsRef<'a, XYWH<T::Unit>>> (
item: F
) -> DrawThunk<T, F> {
DrawThunk(item, std::marker::PhantomData)
}
pub type Drawn<U> = Perhaps<XYWH<U>>;
pub type Drawn<'a, U> = PerhapsRef<'a, XYWH<U>>;
impl<S: Screen> Draw<S> for () {
fn draw (&self, _: &mut S) -> Drawn<S::Unit> {
impl<'a, S: Screen> Draw<'a, S> for () {
fn draw (&self, _: &mut S) -> Drawn<'a, S::Unit> {
Ok(None)
}
}
impl_draw!(<S: Screen, D: Draw<S>,>|self: Option<D>, to: S|{
self.as_ref().map(|it|it.draw(to)).transpose().map(Option::unwrap_or_default)
});
impl<'a, S: Screen, D: Draw<'a, S>> Draw<'a, S> for Option<D> {
fn draw (&self, to: &mut S) -> Drawn<'a, S::Unit> {
self.as_ref().map(|it|it.draw(to)).transpose().map(Option::unwrap_or_default)
}
}
//impl<S: Screen, D: Draw<S>> Draw<S> for RwLock<D> {
//fn draw (&self, __: &mut S) -> Drawn<S::Unit> {
@ -1403,8 +1405,8 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
//}
//}
impl<T: Screen, V: Draw<T>> Draw<T> for &V {
fn draw (&self, to: &mut T) -> Perhaps<XYWH<T::Unit>> {
impl<'a, T: Screen, V: Draw<'a, T>> Draw<'a, T> for &V {
fn draw (&self, to: &mut T) -> Drawn<'a, T::Unit> {
(*self).draw(to)
}
}
@ -1717,7 +1719,7 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
impl ItemTheme {
#[cfg(feature = "term")] pub const G: [Self;256] = {
let mut builder = dizzle::konst::array::ArrayBuilder::new();
let mut builder = ::konst::array::ArrayBuilder::new();
while !builder.is_full() {
let index = builder.len() as u8;
let light = (index as f64 * 1.15) as u8;
@ -1788,23 +1790,26 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
}
}
pub trait ColorDsl<T>: Sized {
fn new_g (expr: T, try_to_u8: impl Fn(Perhaps<&str>)->Perhaps<u8>) -> Usually<Self>;
fn new_rgb (expr: T, try_to_u8: impl Fn(Perhaps<&str>)->Perhaps<u8>) -> Usually<Self>;
pub trait ColorDsl<'a, T>: Sized {
fn new_g (expr: &'a T, try_to_u8: impl Fn(PerhapsRef<'a, &'a str>)->PerhapsRef<'a, u8>)
-> UsuallyRef<'a, Self>;
fn new_rgb (expr: &'a T, try_to_u8: impl Fn(PerhapsRef<'a, &str>)->PerhapsRef<'a, u8>)
-> UsuallyRef<'a, Self>;
}
impl<T: Expression> ColorDsl<T> for Color {
fn new_g (expr: T, try_to_u8: impl Fn(Perhaps<&str>)->Perhaps<u8>) -> Usually<Self> {
impl<'a, T: Language<'a> + 'a> ColorDsl<'a, T> for Color {
fn new_g (expr: &'a T, try_to_u8: impl Fn(PerhapsRef<'a, &'a str>)->PerhapsRef<'a, u8>)
-> Result<Self, Box<dyn Error + 'a>>
{
let n = try_to_u8(expr.tail().map_err(Into::into))?.ok_or(Domain("not gray"))?;
Ok(Self::Rgb(n, n, n))
}
fn new_rgb (expr: T, try_to_u8: impl Fn(Perhaps<&str>)->Perhaps<u8>) -> Usually<Self> {
let r = try_to_u8(expr.tail().map_err(Into::into))?
.ok_or(Domain("not red"))?;
let g = try_to_u8(expr.tail().tail().head().map_err(Into::into))?
.ok_or(Domain("not green"))?;
let b = try_to_u8(expr.tail().tail().tail().head().map_err(Into::into))?
.ok_or(Domain("not blue"))?;
fn new_rgb (expr: &'a T, try_to_u8: impl Fn(PerhapsRef<'a, &str>)->PerhapsRef<'a, u8>)
-> Result<Self, Box<dyn Error + 'a>>
{
let r = try_to_u8(expr.nth(1).map_err(Into::into))?.ok_or(Domain("not red"))?;
let g = try_to_u8(expr.nth(2).map_err(Into::into))?.ok_or(Domain("not green"))?;
let b = try_to_u8(expr.nth(3).map_err(Into::into))?.ok_or(Domain("not blue"))?;
Ok(Color::Rgb(r, g, b))
}
}
@ -1902,7 +1907,7 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
/// }
///
/// impl Interpret<Tui, Option<XYWH<u16>>> for App {
/// fn interpret_expr <'a> (&'a self, to: &mut Tui, lang: &'a impl Expression)
/// fn interpret_expr <'a> (&'a self, to: &mut Tui, lang: &'a impl Language)
/// -> Usually<Option<XYWH<u16>>>
/// {
/// self.keyword(to, lang)
@ -1931,7 +1936,7 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
/// ```
pub trait Keywords <U, V>: 'static {
fn keywords () -> impl Iterator<Item = fn(&Self, &mut U, &str) -> Perhaps<V>>;
fn keyword (&self, to: &mut U, expr: &impl Expression) -> Perhaps<V> {
fn keyword <'a> (&self, to: &mut U, expr: &'a impl Language<'a>) -> PerhapsRef<'a, V> {
if let Some(expr) = expr.src()? {
for keyword in Self::keywords() {
if let Some(result) = keyword(self, to, &expr)? {
@ -1954,17 +1959,5 @@ pub trait AsMutOpt<T> { fn as_mut_opt (&mut self) -> Option<&mut T>; }
}
}
pub fn invalid_variant <T> (
name: &str,
frag: impl Language,
expr: impl Language,
head: impl Language,
) -> Usually<T> {
unimplemented!(
"{name}/{frag:?} ({expr:?}) ({head:?}) ({:?})",
head.src()?.unwrap_or_default().split("/").next()
)
}
#[cfg(feature = "term")] pub use self::term::*;
#[cfg(feature = "term")] mod term;