Skip to content
This repository was archived by the owner on Jan 11, 2026. It is now read-only.

Default implementations don't work #26

@jquesada2016

Description

@jquesada2016

Given the following:

    use typestate::typestate;

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

        #[state]
        pub struct State1(bool);

        pub trait State1: Sized {
            fn new() -> State1 {
                Automaton {
                    state: State1(true),
                }
            }

            fn to_state_2(mut self) -> State2 {
                Automaton {
                    state: State2(true),
                }
            }
        }

        #[state]
        pub struct State2(bool);

        pub trait State2: Sized {
            fn done(self) {}
        }
    }

    fn t() {
        use my_state::*;

        let s1 = Automaton::new();
    }

We get an error stating that State1State trait is in scope, but not implemented for Automaton<State1>.

If I instead implement the traits, all is well:

        #[automaton]
        pub struct Automaton;

        #[state]
        pub struct State1;

        pub trait State1 {
            fn new() -> State1;

            fn to_state_2(self) -> State2;
        }

        impl State1State for Automaton<State1> {
            fn new() -> Automaton<State1> {
                Automaton { state: State1 }
            }

            fn to_state_2(self) -> Automaton<State2> {
                Automaton { state: State2 }
            }
        }

        #[state]
        pub struct State2;

        pub trait State2 {
            fn done(self);
        }

        impl State2State for Automaton<State2> {
            fn done(self) {}
        }
    }

    fn t() {
        use my_state::*;

        let s1 = Automaton::new();
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions