Can we still write safe state machines?

I'm the author of the backblaze-b2 crate, and now that async await will be stabilized, I'm taking up work on it again.

In my old implementation, I had written a custom state machine to wrap the ResponseFuture in a named future. This state machine can be seen here.

Now, because I like being able to provide a nameable type, I tried to convert this state machine to the nightly async await ecosystem. I came up with this. I can't put it on the playground as it has the wrong versions of various dependencies.

Unfortunately this requires unsafe code to pin the futures stored in the various states of the enum. Are there any techniques to avoid this?

If there is any unsoundness in the linked code, I'd like to hear about it too.

1 Like

Oh damn, I just realized ResponseFuture is Unpin. But this question remains for non-unpin futures.

No, this sort of pin projection (through and enum) can't be done safely just yet with just std. I think pin-project may be able to handle enum but I haven't checked yet.


Yes, pin-project can be used on enums. (e.g., this commit in hyper)