Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Validate {
- fn validate(&self) -> bool;
- }
- trait ValidateWith {
- type Validator;
- fn validate_with(&self, param: Self::Validator) -> bool;
- }
- impl<T> Validate for T
- where
- T: ValidateWith<Validator=()> {
- fn validate(&self) -> bool {
- self.validate_with(V::default())
- }
- }
- #[derive(Copy, Clone, Debug)]
- struct Tester {
- pub x: i32,
- pub y: i32
- }
- /*impl Validate for Tester {
- fn validate(&self) -> bool {
- self.x == self.y
- }
- }*/
- impl ValidateWith for Tester {
- type Validator = i32;
- fn validate_with(&self, param: i32) -> bool {
- self.x + param == self.y
- }
- }
- fn testing<T>(a: i32, b: T) -> bool {
- a % 2 == 0
- }
- fn main() {
- let test = Tester{x: 3, y: 3};
- println!("Test 1: {}", test.validate());
- println!("Test 2: {}", test.validate_with(3));
- println!("Test 3: {}", testing(3, 4));
- println!("Test 4: {}", testing(5, ()));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement