rustype / typestate-rs

Proc-macro typestate DSL for Rust
https://rustype.github.io/typestate-rs/
Apache License 2.0
141 stars 11 forks source link

Feature request: "Missing initial state" error when using `Self` #32

Open mx00s opened 2 years ago

mx00s commented 2 years ago

When I had this trait for transitions relating to my Opened state everything worked as expected with the new function making Opened the start state.

pub trait Opened {
    fn new() -> Opened;
    // ...
}

However, changing the return value for new from Opened to Self causes the macro to abort with several errors, beginning with:

error: Missing initial state. To declare an initial state you can use a function with signature like fn f() -> T where T is a declared state.

It would be nicer, I think, if I could use -> Self because it helps make the initial state stand out.

jmg-duarte commented 2 years ago

Hello! Thank you for submitting this issue!

The fix should be simple enough. However, the part of the code that handles that needs a bit of refactoring.

The following function and friends should have more context on the current state: https://github.com/rustype/typestate-rs/blob/ad6c9022f5dc001424ec59f2cea585328d38a740/typestate-proc-macro/src/visitors/transition.rs#L295-L312

If the function knows which state is it part of, it can return the correct ident