Comment by estebank
1 hour ago
Yes, that would be one way of doing it. You can model off of the Typed Builder pattern:
struct Builder<const A: bool, const B: bool> {
a: Option<u32>,
b: Option<u32>,
}
struct Val {
a: u32,
b: u32,
}
impl<const B: bool> Builder<false, B> {
fn set_a(self, a: u32) -> Builder<true, B> {
Builder {
a: Some(a),
b: self.b,
}
}
}
impl<const A: bool> Builder<A, false> {
fn set_b(self, b: u32) -> Builder<A, true> {
Builder {
a: self.a,
b: Some(b),
}
}
}
impl Builder<true, true> {
fn build(self) -> Val {
Val {
a: self.a.unwrap(),
b: self.b.unwrap(),
}
}
}
This won't work for everything, but it is a pattern that I find useful to ensure that things can't happen out of order.
No comments yet
Contribute on Hacker News ↗