refactor: extract mod exit, task; Thread -> Task

This commit is contained in:
same mf who else 2026-03-28 14:11:23 +02:00
parent bea88ac58d
commit dae4d5a140
8 changed files with 114 additions and 74 deletions

View file

@ -1,6 +1,12 @@
use crate::{*, lang::*, color::*, space::*};
/// Drawable that supports dynamic dispatch.
///
/// Drawables are composable, e.g. the [when] and [either] conditionals
/// or the layout constraints.
///
/// Drawables are consumable, i.e. the [Draw::draw] method receives an
/// owned `self` and does not return it, consuming the drawable.
///
/// ```
/// use tengri::draw::*;
@ -20,25 +26,29 @@ use crate::{*, lang::*, color::*, space::*};
pub trait Draw<T: Screen> {
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>>;
}
impl<T: Screen> Draw<T> for () {
fn draw (self, __: &mut T) -> Usually<XYWH<T::Unit>> {
Ok(Default::default())
}
}
impl<T: Screen, D: Draw<T>> Draw<T> for Option<D> {
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> {
Ok(self.map(|draw|draw.draw(to)).transpose()?.unwrap_or_default())
}
}
pub const fn thunk <T: Screen, F: FnOnce(&mut T)->Usually<XYWH<T::Unit>>> (draw: F) -> Thunk<T, F> {
Thunk(draw, std::marker::PhantomData)
}
/// Because we can't implement [Draw] for `F: FnOnce...` without conflicts.
pub struct Thunk<T: Screen, F: FnOnce(&mut T)->Usually<XYWH<T::Unit>>>(
pub F,
std::marker::PhantomData<T>
);
pub const fn thunk <T: Screen, F: FnOnce(&mut T)->Usually<XYWH<T::Unit>>> (draw: F) -> Thunk<T, F> {
Thunk(draw, std::marker::PhantomData)
}
impl<T: Screen, F: FnOnce(&mut T)->Usually<XYWH<T::Unit>>> Draw<T> for Thunk<T, F> {
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> {
(self.0)(to)
@ -76,8 +86,8 @@ pub const fn either <T: Screen> (condition: bool, a: impl Draw<T>, b: impl Draw<
///
/// impl Screen for TestOut {
/// type Unit = u16;
/// fn place <T: Draw<Self> + ?Sized> (&mut self, area: impl TwoD<u16>, _: &T) {
/// println!("place: {area:?}");
/// fn place_at <T: Draw<Self> + ?Sized> (&mut self, area: impl TwoD<u16>, _: &T) {
/// println!("placed: {area:?}");
/// ()
/// }
/// }
@ -101,3 +111,14 @@ pub trait Screen: Space<Self::Unit> + Send + Sync + Sized {
unimplemented!()
}
}
/// Emit a [Draw]able.
pub trait View<T: Screen> {
fn view (&self) -> impl Draw<T>;
}
impl<T: Screen, V: View<T>> Draw<T> for &V {
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> {
self.view().draw(to)
}
}