korken89 / smlang-rs

A State Machine Language DSL procedual macro for Rust
Apache License 2.0
202 stars 28 forks source link

Allow states to contain data with lifetimes just like events can #26

Closed dzimmanck closed 2 years ago

dzimmanck commented 2 years ago

While events can contain data with lifetimes, states cannot and generate a "use of undeclared lifetime" error inside the procedural macro if you try.

Simple example:

//! An example of generating events with actions

#![deny(missing_docs)]

use smlang::statemachine;

/// State data
#[derive(PartialEq)]
pub struct Data<'a>(Option<&'a Events>);

statemachine! {
    transitions: {
        *D0 + ToD1 / to_d1  = D1,
        D1(Data<'a>) + ToD0 = D0,
    }
}

/// Context
pub struct Context;

impl StateMachineContext for Context {
    fn to_d1(&mut self) -> Data {
        Data(Some(&Events::ToD0))
    }
}

image

dzimmanck commented 2 years ago

I am working on this. It involves a pretty substantial re-factor of the parser code in order to re-use the data type parsing for both states and events.

dzimmanck commented 2 years ago

I have a branch ready for review at https://github.com/dzimmanck/smlang-rs/tree/feature/support_lifetimes_in_state_data.

I haven't issued a pull request yet because:

  1. I have not verified that it works, only that it does not break existing tests
  2. It needs testing
  3. It needs at least 1 example
  4. It needs documentation

This involved quite a bit of re-factor that I think went pretty well. To aide in review, I broke it into 8 concise commits with limited scope for each change and every one of which still passes all tests. @korken89, would love a review of these changes before I proceed when you get a chance.

dzimmanck commented 2 years ago

I debugged the code and added a working example, so I submitted a pull request. Pull request #28 .

dzimmanck commented 2 years ago

Closed by pull request #28