]> git.lizzy.rs Git - rust.git/blob - src/test/ui/self/explicit-self.rs
Auto merge of #75936 - sdroege:chunks-exact-construction-bounds-check, r=nagisa
[rust.git] / src / test / ui / self / explicit-self.rs
1 // run-pass
2 #![allow(dead_code)]
3 #![allow(non_camel_case_types)]
4 #![allow(non_upper_case_globals)]
5
6 #![feature(box_syntax)]
7
8 static tau: f64 = 2.0*3.14159265358979323;
9
10 struct Point {x: f64, y: f64}
11 struct Size {w: f64, h: f64}
12 enum shape {
13     circle(Point, f64),
14     rectangle(Point, Size)
15 }
16
17
18 fn compute_area(shape: &shape) -> f64 {
19     match *shape {
20         shape::circle(_, radius) => 0.5 * tau * radius * radius,
21         shape::rectangle(_, ref size) => size.w * size.h
22     }
23 }
24
25 impl shape {
26     // self is in the implicit self region
27     pub fn select<'r, T>(&self, threshold: f64, a: &'r T, b: &'r T)
28                          -> &'r T {
29         if compute_area(self) > threshold {a} else {b}
30     }
31 }
32
33 fn select_based_on_unit_circle<'r, T>(
34     threshold: f64, a: &'r T, b: &'r T) -> &'r T {
35
36     let shape = &shape::circle(Point{x: 0.0, y: 0.0}, 1.0);
37     shape.select(threshold, a, b)
38 }
39
40 #[derive(Clone)]
41 struct thing {
42     x: A
43 }
44
45 #[derive(Clone)]
46 struct A {
47     a: isize
48 }
49
50 fn thing(x: A) -> thing {
51     thing {
52         x: x
53     }
54 }
55
56 impl thing {
57     pub fn bar(self: Box<thing>) -> isize { self.x.a }
58     pub fn quux(&self) -> isize { self.x.a }
59     pub fn baz<'a>(&'a self) -> &'a A { &self.x }
60     pub fn spam(self) -> isize { self.x.a }
61 }
62
63 trait Nus { fn f(&self); }
64 impl Nus for thing { fn f(&self) {} }
65
66 pub fn main() {
67     let y: Box<_> = box thing(A {a: 10});
68     assert_eq!(y.clone().bar(), 10);
69     assert_eq!(y.quux(), 10);
70
71     let z = thing(A {a: 11});
72     assert_eq!(z.spam(), 11);
73 }