· 6 years ago · Feb 19, 2019, 11:42 AM
1trait Spec {
2 fn key(&self) -> &str;
3 fn compute_big_number(&self) -> i32;
4}
5
6trait Resolver {
7 fn resolve<S: Spec>(&self, spec: S) -> i32;
8}
9
10fn display<R: Resolver>(r: &R) {
11 struct MySpec;
12 impl Spec for MySpec {
13 fn key(&self) -> &str {
14 "hmmm"
15 }
16 fn compute_big_number(&self) -> i32 {
17 for i in 1..10 {
18 // gee this sure is taking a long time
19 }
20 99
21 }
22 }
23 let spec: MySpec = MySpec {};
24 println!("{}", r.resolve(spec));
25}
26
27fn main() {
28 let secret_key: &str = "awhooga";
29 struct Z;
30 impl Resolver for Z {
31 fn resolve<S: Spec>(&self, spec: S) -> i32 {
32 if spec.key() != secret_key {
33 4
34 } else {
35 spec.compute_big_number()
36 }
37 }
38 }
39 display(&Z {});
40}