I wanted to know if the Rust compiler would optimize the following state machine pattern, as inspired by this blog post .
Please let me know if this is zero cost, because it could help me to enforce compile time constraints while keeping the same overall data structure
struct StateA {
foo: RefToSomeObject
}
struct StateB {
bar: RefToSomeObject
}
struct Request<S> {
data: String,
state: S
}
// noting that that StateA and StateB have same exact size
// would the following move be zero cost?
// Request<StateA> -> Request<StateB>
let a = A {
data: String::from("a"),
state: StateA { foo: CoolRef }
};
// Do some random function calls in the middle
let b = B {
data: a.data,
state: StateB { bar: a.state.foo }
};