Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. trait Validate {
  2. fn validate(&self) -> bool;
  3. }
  4.  
  5. trait ValidateWith {
  6. type Validator;
  7.  
  8. fn validate_with(&self, param: Self::Validator) -> bool;
  9. }
  10.  
  11. impl<T> Validate for T
  12. where
  13. T: ValidateWith<Validator=()> {
  14. fn validate(&self) -> bool {
  15. self.validate_with(V::default())
  16. }
  17. }
  18.  
  19. #[derive(Copy, Clone, Debug)]
  20. struct Tester {
  21. pub x: i32,
  22. pub y: i32
  23. }
  24.  
  25. /*impl Validate for Tester {
  26. fn validate(&self) -> bool {
  27. self.x == self.y
  28. }
  29. }*/
  30.  
  31. impl ValidateWith for Tester {
  32. type Validator = i32;
  33.  
  34. fn validate_with(&self, param: i32) -> bool {
  35. self.x + param == self.y
  36. }
  37. }
  38.  
  39. fn testing<T>(a: i32, b: T) -> bool {
  40. a % 2 == 0
  41. }
  42.  
  43. fn main() {
  44. let test = Tester{x: 3, y: 3};
  45.  
  46. println!("Test 1: {}", test.validate());
  47. println!("Test 2: {}", test.validate_with(3));
  48. println!("Test 3: {}", testing(3, 4));
  49. println!("Test 4: {}", testing(5, ()));
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement