typestate-rs icon indicating copy to clipboard operation
typestate-rs copied to clipboard

Generics Support

Open jquesada2016 opened this issue 3 years ago • 10 comments

Attempting to do the following results in an error:

#[typestate]
mod my_state {
    #[automaton]
    pub struct MyState;

    #[state]
    pub struct State1<T> {
        data: T,
    }

    trait State1 {
        fn new<T>() -> State1<T>;

        fn done(self);
    }
}

The above produces the following 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.rustc

This seems to tell me generics are not yet supported? If not, are there any plans for supporting generics?

jquesada2016 avatar Nov 17 '21 21:11 jquesada2016

You're correct, generics are not yet supported, I've ran into this issue the day before your comment. There are plans yes, I'm researching how to properly implement them and maybe review part of the crate.

jmg-duarte avatar Nov 18 '21 08:11 jmg-duarte

Awesome! I'm working on a new Rust WASM front-end framework that uses typestates a lot, and this crate took out a massive amount of boilerplate. Those generics are the main thing I need!

jquesada2016 avatar Nov 19 '21 02:11 jquesada2016

@jquesada2016 I've just pushed a change to the dev branch which should add support for generics! You example expands to the following:

mod my_state {
    pub struct MyState<State: MyStateState> {
        pub state: State,
    }
    pub struct State1<T> {
        data: T,
    }
    trait State1State {
        fn new<T>() -> State1<T>;
        fn done(self);
    }
    #[doc(hidden)]
    mod __private {
        pub trait MyStateState {}
    }
    pub trait MyStateState: __private::MyStateState {}
    impl<__T: ?::core::marker::Sized> MyStateState for __T where __T: __private::MyStateState {}
    impl<T> __private::MyStateState for State1<T> {}
}

I'd be very grateful if you would help me test it by using the dev branch for development before I release the code!

jmg-duarte avatar Dec 08 '21 18:12 jmg-duarte

You got it good sir! I'm on it now.

P.S. Sorry for not getting a chance to create a pull request on the docs, I thought I was going to have a chance this passed weekend. That didn't happen. :/

jquesada2016 avatar Dec 09 '21 02:12 jquesada2016

@jmg-duarte do you need me to write tests or just give you feedback?

jquesada2016 avatar Dec 09 '21 18:12 jquesada2016

@jmg-duarte do you need me to write tests or just give you feedback?

I won't say no to more tests, however, what I really need is feedback

jmg-duarte avatar Dec 09 '21 18:12 jmg-duarte

@jmg-duarte generics have been working very good for me thus far! I tested it with constraints and some lifetime bounds and have had no issues thus far! Thank you again for your work!

jquesada2016 avatar Dec 13 '21 02:12 jquesada2016

@jmg-duarte generics have been working very good for me thus far! I tested it with constraints and some lifetime bounds and have had no issues thus far! Thank you again for your work!

That's great to know! I'll be merging the branch in the near future (hopefully today!) and adding some other features (namely the export). I'll let you know when the rc is released!

jmg-duarte avatar Dec 13 '21 08:12 jmg-duarte

@jmg-duarte I added an issue for a problem I discovered with generics as currently implemented. I added a new issue instead of posting it here because it was a bit longer. The issue number is #27.

jquesada2016 avatar Dec 14 '21 22:12 jquesada2016

@jmg-duarte I added an issue for a problem I discovered with generics as currently implemented. I added a new issue instead of posting it here because it was a bit longer. The issue number is #27.

Hey @jquesada2016, I saw it already and had a nice draft and answer lined up but the browser had other plans 🤷‍♂️

I'll try to handle the issues you raised ASAP.

Thank you for the time spent reporting issues!

jmg-duarte avatar Dec 14 '21 23:12 jmg-duarte