However, the compiler doesn't allow me to use the ..self syntax because Post<PostPublished> is not the same type as Post<PostDeleted>. Fair, considering that typestates were specifically created to combat conversions like these, but what is the nicest way to achieve what I want without doing something like:
You'll have to do it at least once. For code duplication, you could create e. g. something like a map-style helper function. Of the problem is that you have too many similar types like this, you could even generate it from a macro, I suppose.
Besides the implementation style you showed, one can also write this with a few less usages of "self." using something like
impl Post<PostPublished> {
fn delete(self) -> Post<PostDeleted> {
let Post { id, user_id, title, body, .. } = self;
Post {
state: PostDeleted,
id,
user_id,
title,
body,
}
}
}
Eventually the plan is to make struct update syntax compatible with changing the type of its generic parameters. Here's the tracking issue for that feature if you're curious