Compare commits

...

3 commits

Author SHA1 Message Date
446ec7a714 feat(output): impl PartialEq for Measure
Some checks are pending
/ build (push) Waiting to run
2025-08-23 23:19:49 +03:00
375b959e33 feat(input): add def_command 2025-08-23 23:19:39 +03:00
022bfa3e20 fix(proc): Command self type 2025-08-23 23:19:23 +03:00
3 changed files with 30 additions and 6 deletions

View file

@ -26,8 +26,8 @@ flex_trait_mut!(Handle <E: Input> {
});
pub trait Command<S>: Send + Sync + Sized {
fn execute (self, state: &mut S) -> Perhaps<Self>;
fn delegate <T> (self, state: &mut S, wrap: impl Fn(Self)->T) -> Perhaps<T>
fn execute (&self, state: &mut S) -> Perhaps<Self>;
fn delegate <T> (&self, state: &mut S, wrap: impl Fn(Self)->T) -> Perhaps<T>
where Self: Sized
{
Ok(self.execute(state)?.map(wrap))
@ -35,10 +35,10 @@ pub trait Command<S>: Send + Sync + Sized {
}
impl<S, T: Command<S>> Command<S> for Option<T> {
fn execute (self, _: &mut S) -> Perhaps<Self> {
fn execute (&self, _: &mut S) -> Perhaps<Self> {
Ok(None)
}
fn delegate <U> (self, _: &mut S, _: impl Fn(Self)->U) -> Perhaps<U>
fn delegate <U> (&self, _: &mut S, _: impl Fn(Self)->U) -> Perhaps<U>
where Self: Sized
{
Ok(None)
@ -49,13 +49,30 @@ impl<S, T: Command<S>> Command<S> for Option<T> {
#[macro_export] macro_rules! command {
($(<$($l:lifetime),+>)?|$self:ident:$Command:ty,$state:ident:$State:ty|$handler:expr) => {
impl$(<$($l),+>)? ::tengri::input::Command<$State> for $Command {
fn execute ($self, $state: &mut $State) -> Perhaps<Self> {
fn execute (&$self, $state: &mut $State) -> Perhaps<Self> {
Ok($handler)
}
}
};
}
#[macro_export] macro_rules! def_command (($Command:ident: |$state:ident: $State:ty| {
$($Variant:ident$({$($arg:ident:$Arg:ty),+ $(,)?})?=>$body:expr),* $(,)?
})=>{
#[derive(Debug)]
pub enum $Command {
$($Variant $({ $($arg: $Arg),* })?),*
}
impl Command<$State> for $Command {
fn execute (&self, $state: &mut $State) -> Perhaps<Self> {
match self {
$(Self::$Variant $({ $($arg),* })? => $body,)*
_ => unimplemented!("Command<{}>: {self:?}", stringify!($State)),
}
}
}
});
/// Implement [Handle] for given `State` and `handler`.
#[macro_export] macro_rules! handle {
(|$self:ident:$State:ty,$input:ident|$handler:expr) => {

View file

@ -208,6 +208,13 @@ pub struct Measure<E: Output> {
pub y: Arc<AtomicUsize>,
}
impl<E: Output> PartialEq for Measure<E> {
fn eq (&self, other: &Self) -> bool {
self.x.load(Relaxed) == other.x.load(Relaxed) &&
self.y.load(Relaxed) == other.y.load(Relaxed)
}
}
// TODO: 🡘 🡙 ←🡙→ indicator to expand window when too small
impl<E: Output> Content<E> for Measure<E> {
fn render (&self, to: &mut E) {

View file

@ -144,7 +144,7 @@ impl ToTokens for CommandDef {
/// mutable pointer to [#state], invoking predefined operations
/// and optionally returning undo history data.
impl ::tengri::input::Command<#state> for #command_enum {
fn execute (self, state: &mut #state) -> Perhaps<Self> {
fn execute (&self, state: &mut #state) -> Perhaps<Self> {
match self { #(#implementations)* }
}
}