When const generics are fully implemented (they are partially usable on nightly rust now) this will enable an even stronger version of this pattern, allowing you the full power of enums to represent the state.
enum SenderState {
ReadyToSendHello,
HasSentHello,
HasSentNumber,
HasReceivedNumber
}
Sender<const S: SenderState> {
...
}
impl Sender<{SenderState::ReadyToSendHello}>{
...
}
One can then give the individual states extra parameters:
HasSentNumber {
number: u32
}
(Note that in this case that doesn't make much sense, the number that is sent is more associated data then an actual type parameter. There is no real difference, at the type level, between HasSentNumber { number: 3 } and HasSentNumber { number: 6 }, and the compiler generating two types for this would be unnecessary. It is only an example of the syntax.)