Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct AType(i32);
- #[derive(Debug)]
- struct BType(i32);
- #[derive(Debug)]
- struct CType(i32);
- struct Incomplete;
- #[derive(Debug)]
- struct Obj {
- a: AType,
- b: BType,
- c: CType,
- }
- struct Builder<A=Incomplete, B=Incomplete, C=Incomplete> {
- a: A,
- b: B,
- c: C,
- }
- impl Builder<Incomplete, Incomplete, Incomplete> {
- pub fn new() -> Self {
- Self {
- a: Incomplete,
- b: Incomplete,
- c: Incomplete,
- }
- }
- }
- impl<B, C> Builder<Incomplete, B, C> {
- pub fn with_a(self, a: AType) -> Builder<AType, B, C> {
- Builder::<AType, B, C> {
- a: a,
- b: self.b,
- c: self.c,
- }
- }
- }
- impl<A, C> Builder<A, Incomplete, C> {
- pub fn with_b(self, b: BType) -> Builder<A, BType, C> {
- Builder::<A, BType, C> {
- a: self.a,
- b: b,
- c: self.c,
- }
- }
- }
- impl<A, B> Builder<A, B, Incomplete> {
- pub fn with_c(self, c: CType) -> Builder<A, B, CType> {
- Builder::<A, B, CType> {
- a: self.a,
- b: self.b,
- c: c,
- }
- }
- }
- impl Builder<AType, BType, CType> {
- pub fn build(self) -> Obj {
- Obj {
- a: self.a,
- b: self.b,
- c: self.c,
- }
- }
- }
- pub fn main() {
- let a = AType(1);
- let b = BType(2);
- let c = CType(3);
- let value = Builder::new()
- .with_a(a)
- .with_b(b)
- .with_c(c)
- .build();
- dbg!(value);
- }
Add Comment
Please, Sign In to add comment