Guest User

Untitled

a guest
Feb 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #[derive(Debug)]
  2. struct AType(i32);
  3.  
  4. #[derive(Debug)]
  5. struct BType(i32);
  6.  
  7. #[derive(Debug)]
  8. struct CType(i32);
  9.  
  10. struct Incomplete;
  11.  
  12. #[derive(Debug)]
  13. struct Obj {
  14. a: AType,
  15. b: BType,
  16. c: CType,
  17. }
  18.  
  19. struct Builder<A=Incomplete, B=Incomplete, C=Incomplete> {
  20. a: A,
  21. b: B,
  22. c: C,
  23. }
  24. impl Builder<Incomplete, Incomplete, Incomplete> {
  25. pub fn new() -> Self {
  26. Self {
  27. a: Incomplete,
  28. b: Incomplete,
  29. c: Incomplete,
  30. }
  31. }
  32. }
  33. impl<B, C> Builder<Incomplete, B, C> {
  34. pub fn with_a(self, a: AType) -> Builder<AType, B, C> {
  35. Builder::<AType, B, C> {
  36. a: a,
  37. b: self.b,
  38. c: self.c,
  39. }
  40. }
  41. }
  42. impl<A, C> Builder<A, Incomplete, C> {
  43. pub fn with_b(self, b: BType) -> Builder<A, BType, C> {
  44. Builder::<A, BType, C> {
  45. a: self.a,
  46. b: b,
  47. c: self.c,
  48. }
  49. }
  50. }
  51. impl<A, B> Builder<A, B, Incomplete> {
  52. pub fn with_c(self, c: CType) -> Builder<A, B, CType> {
  53. Builder::<A, B, CType> {
  54. a: self.a,
  55. b: self.b,
  56. c: c,
  57. }
  58. }
  59. }
  60. impl Builder<AType, BType, CType> {
  61. pub fn build(self) -> Obj {
  62. Obj {
  63. a: self.a,
  64. b: self.b,
  65. c: self.c,
  66. }
  67. }
  68. }
  69.  
  70. pub fn main() {
  71. let a = AType(1);
  72. let b = BType(2);
  73. let c = CType(3);
  74.  
  75. let value = Builder::new()
  76. .with_a(a)
  77. .with_b(b)
  78. .with_c(c)
  79. .build();
  80.  
  81. dbg!(value);
  82. }
Add Comment
Please, Sign In to add comment