]>
Commit | Line | Data |
---|---|---|
b7449926 | 1 | // run-pass |
0bf4aa26 | 2 | #![allow(dead_code)] |
b7449926 XL |
3 | #![allow(non_camel_case_types)] |
4 | #![allow(non_upper_case_globals)] | |
c34b1796 | 5 | |
1a4d82fc | 6 | static tau: f64 = 2.0*3.14159265358979323; |
223e47cc | 7 | |
1a4d82fc JJ |
8 | struct Point {x: f64, y: f64} |
9 | struct Size {w: f64, h: f64} | |
223e47cc | 10 | enum shape { |
1a4d82fc | 11 | circle(Point, f64), |
223e47cc LB |
12 | rectangle(Point, Size) |
13 | } | |
14 | ||
15 | ||
1a4d82fc | 16 | fn compute_area(shape: &shape) -> f64 { |
223e47cc | 17 | match *shape { |
1a4d82fc JJ |
18 | shape::circle(_, radius) => 0.5 * tau * radius * radius, |
19 | shape::rectangle(_, ref size) => size.w * size.h | |
223e47cc LB |
20 | } |
21 | } | |
22 | ||
970d7e83 | 23 | impl shape { |
223e47cc | 24 | // self is in the implicit self region |
1a4d82fc | 25 | pub fn select<'r, T>(&self, threshold: f64, a: &'r T, b: &'r T) |
970d7e83 | 26 | -> &'r T { |
223e47cc LB |
27 | if compute_area(self) > threshold {a} else {b} |
28 | } | |
29 | } | |
30 | ||
31 | fn select_based_on_unit_circle<'r, T>( | |
1a4d82fc | 32 | threshold: f64, a: &'r T, b: &'r T) -> &'r T { |
223e47cc | 33 | |
1a4d82fc | 34 | let shape = &shape::circle(Point{x: 0.0, y: 0.0}, 1.0); |
223e47cc LB |
35 | shape.select(threshold, a, b) |
36 | } | |
37 | ||
1a4d82fc | 38 | #[derive(Clone)] |
223e47cc LB |
39 | struct thing { |
40 | x: A | |
41 | } | |
42 | ||
1a4d82fc JJ |
43 | #[derive(Clone)] |
44 | struct A { | |
c34b1796 | 45 | a: isize |
1a4d82fc | 46 | } |
223e47cc LB |
47 | |
48 | fn thing(x: A) -> thing { | |
49 | thing { | |
1a4d82fc | 50 | x: x |
223e47cc LB |
51 | } |
52 | } | |
53 | ||
970d7e83 | 54 | impl thing { |
c34b1796 AL |
55 | pub fn bar(self: Box<thing>) -> isize { self.x.a } |
56 | pub fn quux(&self) -> isize { self.x.a } | |
970d7e83 | 57 | pub fn baz<'a>(&'a self) -> &'a A { &self.x } |
c34b1796 | 58 | pub fn spam(self) -> isize { self.x.a } |
223e47cc LB |
59 | } |
60 | ||
61 | trait Nus { fn f(&self); } | |
62 | impl Nus for thing { fn f(&self) {} } | |
63 | ||
64 | pub fn main() { | |
c295e0f8 | 65 | let y: Box<_> = Box::new(thing(A {a: 10})); |
1a4d82fc | 66 | assert_eq!(y.clone().bar(), 10); |
970d7e83 | 67 | assert_eq!(y.quux(), 10); |
223e47cc | 68 | |
1a4d82fc | 69 | let z = thing(A {a: 11}); |
970d7e83 | 70 | assert_eq!(z.spam(), 11); |
223e47cc | 71 | } |