Code review request for my first crate

I've just published my first crate and would appreciate any suggestions: GitHub - imbolc/step-machine

My first feedback: include a link to the docs in your README, or at least a link to the crates.io page.

1 Like

I'm a little confused by

pub trait State<M: State<M>> {
    type Error: Debug;
    fn next(self) -> Result<Option<M>, Self::Error>;
}

This send very confusing, it looks like you could create type A that is a State<B>, and that seems like it would be very confusing.

Why not simply use

pub trait State : DeserializeOwned + Serialize {
    type Error: Debug;
    fn next(self) -> Result<Option<Self>, Self::Error>;
}

This would simplify the bounds in various places in your code and make it obvious that the State isn't going to be changing type.

it looks like you could create type A that is a State<B>

That's right, B here is an enum wrapping all the states including A. We need enum wrapper to unambiguously serialize / deserialize states.

fn next(self) -> Result<Option<Self>, Self::Error>

It meant to return the enum wrapper around any of possible states, rather then the state itself eg: Ok(Some(if foo { FirstState.into() } else { SecondState.into() }))

And why not just implement State on the even [correction: enum] itself?

I don't understand, could you show the code you mean

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.