mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
wip: dsl, input, output, proc: more precise lifetimes
Some checks are pending
/ build (push) Waiting to run
Some checks are pending
/ build (push) Waiting to run
This commit is contained in:
parent
22d63eed9c
commit
5e09f5a4bb
11 changed files with 140 additions and 122 deletions
|
|
@ -2,9 +2,9 @@ use crate::*;
|
|||
use std::marker::PhantomData;
|
||||
|
||||
/// A [Command] that can be constructed from a [Token].
|
||||
pub trait DslCommand<'a, C>: TryFromDsl<'a, C> + Command<C> {}
|
||||
pub trait DslCommand<'state, C>: TryFromDsl<'state, C> + Command<C> {}
|
||||
|
||||
impl<'a, C, T: TryFromDsl<'a, C> + Command<C>> DslCommand<'a, C> for T {}
|
||||
impl<'state, C, T: TryFromDsl<'state, C> + Command<C>> DslCommand<'state, C> for T {}
|
||||
|
||||
/// [Input] state that can be matched against a [Value].
|
||||
pub trait DslInput: Input {
|
||||
|
|
@ -12,14 +12,14 @@ pub trait DslInput: Input {
|
|||
}
|
||||
|
||||
/// A pre-configured mapping of input events to commands.
|
||||
pub trait KeyMap<'a, S, C: DslCommand<'a, S>, I: DslInput> {
|
||||
pub trait KeyMap<'state, S, C: DslCommand<'state, S>, I: DslInput> {
|
||||
/// Try to find a command that matches the current input event.
|
||||
fn command (&'a self, state: &'a S, input: &'a I) -> Option<C>;
|
||||
fn command (&'state self, state: &'state S, input: &'state I) -> Option<C>;
|
||||
}
|
||||
|
||||
/// A [SourceIter] can be a [KeyMap].
|
||||
impl<'a, S, C: DslCommand<'a, S>, I: DslInput> KeyMap<'a, S, C, I> for SourceIter<'a> {
|
||||
fn command (&'a self, state: &'a S, input: &'a I) -> Option<C> {
|
||||
impl<'state, S, C: DslCommand<'state, S>, I: DslInput> KeyMap<'state, S, C, I> for SourceIter<'state> {
|
||||
fn command (&'state self, state: &'state S, input: &'state I) -> Option<C> {
|
||||
let mut iter = self.clone();
|
||||
while let Some((token, rest)) = iter.next() {
|
||||
iter = rest;
|
||||
|
|
@ -45,8 +45,8 @@ impl<'a, S, C: DslCommand<'a, S>, I: DslInput> KeyMap<'a, S, C, I> for SourceIte
|
|||
}
|
||||
|
||||
/// A [TokenIter] can be a [KeyMap].
|
||||
impl<'a, S, C: DslCommand<'a, S>, I: DslInput> KeyMap<'a, S, C, I> for TokenIter<'a> {
|
||||
fn command (&'a self, state: &'a S, input: &'a I) -> Option<C> {
|
||||
impl<'state, S, C: DslCommand<'state, S>, I: DslInput> KeyMap<'state, S, C, I> for TokenIter<'state> {
|
||||
fn command (&'state self, state: &'state S, input: &'state I) -> Option<C> {
|
||||
let mut iter = self.clone();
|
||||
while let Some(next) = iter.next() {
|
||||
match next {
|
||||
|
|
@ -70,25 +70,25 @@ impl<'a, S, C: DslCommand<'a, S>, I: DslInput> KeyMap<'a, S, C, I> for TokenIter
|
|||
}
|
||||
}
|
||||
|
||||
pub type InputLayerCond<'a, S> = Box<dyn Fn(&S)->bool + Send + Sync + 'a>;
|
||||
pub type InputLayerCond<'state, S> = Box<dyn Fn(&S)->bool + Send + Sync + 'state>;
|
||||
|
||||
/// A collection of pre-configured mappings of input events to commands,
|
||||
/// which may be made available subject to given conditions.
|
||||
pub struct InputMap<'a, S, C, I, M>
|
||||
pub struct InputMap<'state, S, C, I, M>
|
||||
where
|
||||
C: DslCommand<'a, S>,
|
||||
C: DslCommand<'state, S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'a, S, C, I> + Send + Sync
|
||||
M: KeyMap<'state, S, C, I> + Send + Sync
|
||||
{
|
||||
__: &'a PhantomData<(S, C, I)>,
|
||||
pub layers: Vec<(InputLayerCond<'a, S>, M)>,
|
||||
__: &'state PhantomData<(S, C, I)>,
|
||||
pub layers: Vec<(InputLayerCond<'state, S>, M)>,
|
||||
}
|
||||
|
||||
impl<'a, S, C, I, M> Default for InputMap<'a, S, C, I, M>
|
||||
impl<'state, S, C, I, M> Default for InputMap<'state, S, C, I, M>
|
||||
where
|
||||
C: DslCommand<'a, S>,
|
||||
C: DslCommand<'state, S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'a, S, C, I> + Send + Sync
|
||||
M: KeyMap<'state, S, C, I> + Send + Sync
|
||||
{
|
||||
fn default () -> Self {
|
||||
Self {
|
||||
|
|
@ -98,11 +98,11 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, S, C, I, M> InputMap<'a, S, C, I, M>
|
||||
impl<'state, S, C, I, M> InputMap<'state, S, C, I, M>
|
||||
where
|
||||
C: DslCommand<'a, S>,
|
||||
C: DslCommand<'state, S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'a, S, C, I> + Send + Sync
|
||||
M: KeyMap<'state, S, C, I> + Send + Sync
|
||||
{
|
||||
pub fn new (keymap: M) -> Self {
|
||||
Self::default().layer(keymap)
|
||||
|
|
@ -115,21 +115,21 @@ where
|
|||
self.add_layer_if(Box::new(|_|true), keymap);
|
||||
self
|
||||
}
|
||||
pub fn layer_if (mut self, condition: InputLayerCond<'a, S>, keymap: M) -> Self {
|
||||
pub fn layer_if (mut self, condition: InputLayerCond<'state, S>, keymap: M) -> Self {
|
||||
self.add_layer_if(condition, keymap);
|
||||
self
|
||||
}
|
||||
pub fn add_layer_if (&mut self, condition: InputLayerCond<'a, S>, keymap: M) -> &mut Self {
|
||||
pub fn add_layer_if (&mut self, condition: InputLayerCond<'state, S>, keymap: M) -> &mut Self {
|
||||
self.layers.push((Box::new(condition), keymap));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S, C, I, M> std::fmt::Debug for InputMap<'a, S, C, I, M>
|
||||
impl<'state, S, C, I, M> std::fmt::Debug for InputMap<'state, S, C, I, M>
|
||||
where
|
||||
C: DslCommand<'a, S>,
|
||||
C: DslCommand<'state, S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'a, S, C, I> + Send + Sync
|
||||
M: KeyMap<'state, S, C, I> + Send + Sync
|
||||
{
|
||||
fn fmt (&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
||||
write!(f, "[InputMap: {} layer(s)]", self.layers.len());
|
||||
|
|
@ -138,13 +138,13 @@ where
|
|||
}
|
||||
|
||||
/// An [InputMap] can be a [KeyMap].
|
||||
impl<'a, S, C, I, M> KeyMap<'a, S, C, I> for InputMap<'a, S, C, I, M>
|
||||
impl<'state, S, C, I, M> KeyMap<'state, S, C, I> for InputMap<'state, S, C, I, M>
|
||||
where
|
||||
C: DslCommand<'a, S>,
|
||||
C: DslCommand<'state, S>,
|
||||
I: DslInput,
|
||||
M: KeyMap<'a, S, C, I> + Send + Sync
|
||||
M: KeyMap<'state, S, C, I> + Send + Sync
|
||||
{
|
||||
fn command (&'a self, state: &'a S, input: &'a I) -> Option<C> {
|
||||
fn command (&'state self, state: &'state S, input: &'state I) -> Option<C> {
|
||||
for (condition, keymap) in self.layers.iter() {
|
||||
if !condition(state) {
|
||||
continue
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue