Created
April 21, 2019 11:28
-
-
Save satellitex/93b48d946d62a138fcafdd349cce032e to your computer and use it in GitHub Desktop.
異なる引数のトレイトを取る Verifier トレイト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::ops::{Add, Index}; | |
use std::cmp::Eq; | |
use std::marker::PhantomData; | |
pub trait Trait<T> { | |
fn verify(&self, a: T, b: T) -> bool; | |
} | |
pub struct VerifierX<T: Add + Eq> { | |
a: T, | |
} | |
impl<T: Add<Output=T> + Eq> Trait<T> for VerifierX<T> { | |
fn verify(&self, a: T, b: T) -> bool { | |
if self.a == a + b { return true; } | |
false | |
} | |
} | |
pub struct VerifierY<T: Index<usize, Output=U>, U: Eq>(PhantomData<(T,U)>); | |
impl<T, U> Trait<T> for VerifierY<T, U> where T: Index<usize, Output=U>, U: Eq { | |
fn verify(&self, a: T, b: T) -> bool { | |
if a[0] == b[0] { return true; } | |
false | |
} | |
} | |
fn main() { | |
let v = VerifierX{a: 10}; | |
println!("{}, {}",v.verify(1,9), v.verify(1,5)); | |
let v = VerifierY::<Vec<i32>,i32>(PhantomData); | |
println!("{}, {}", v.verify(vec!{1,2},vec!{1,3}), | |
v.verify(vec!{1,3}, vec!{0,3})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment