Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. struct FooBuilder;
  2. struct Foo;
  3.  
  4. impl FooBuilder {
  5. fn new() -> Self { FooBuilder }
  6. fn build(self) -> Foo { Foo }
  7. fn set_x(&mut self, _: u8) -> &mut Self {
  8. self
  9. }
  10. fn set_y(&mut self, _: u8) -> &mut Self {
  11. self
  12. }
  13. fn set_f(&mut self) -> &mut Self { self }
  14.  
  15. fn with<T>(mut self, f: T) -> Self where T: FnOnce(&mut Self) -> &mut Self {
  16. f(&mut self);
  17. self
  18. }
  19. fn with1<T, Q>(mut self, f: T, v: Q) -> Self where T: FnOnce(&mut Self, Q) -> &mut Self {
  20. f(&mut self, v);
  21. self
  22. }
  23. }
  24.  
  25. fn main() {
  26. let _ = FooBuilder::new()
  27. .with(FooBuilder::set_f)
  28. .with1(FooBuilder::set_x, 5)
  29. .with(|s| {s.set_y(3); s})
  30. .build();
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement