]> git.proxmox.com Git - rustc.git/blob - src/test/ui/self/explicit-self.rs
New upstream version 1.67.1+dfsg1
[rustc.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 static tau: f64 = 2.0*3.14159265358979323;
7
8 struct Point {x: f64, y: f64}
9 struct Size {w: f64, h: f64}
10 enum shape {
11 circle(Point, f64),
12 rectangle(Point, Size)
13 }
14
15
16 fn compute_area(shape: &shape) -> f64 {
17 match *shape {
18 shape::circle(_, radius) => 0.5 * tau * radius * radius,
19 shape::rectangle(_, ref size) => size.w * size.h
20 }
21 }
22
23 impl shape {
24 // self is in the implicit self region
25 pub fn select<'r, T>(&self, threshold: f64, a: &'r T, b: &'r T)
26 -> &'r T {
27 if compute_area(self) > threshold {a} else {b}
28 }
29 }
30
31 fn select_based_on_unit_circle<'r, T>(
32 threshold: f64, a: &'r T, b: &'r T) -> &'r T {
33
34 let shape = &shape::circle(Point{x: 0.0, y: 0.0}, 1.0);
35 shape.select(threshold, a, b)
36 }
37
38 #[derive(Clone)]
39 struct thing {
40 x: A
41 }
42
43 #[derive(Clone)]
44 struct A {
45 a: isize
46 }
47
48 fn thing(x: A) -> thing {
49 thing {
50 x: x
51 }
52 }
53
54 impl thing {
55 pub fn bar(self: Box<thing>) -> isize { self.x.a }
56 pub fn quux(&self) -> isize { self.x.a }
57 pub fn baz<'a>(&'a self) -> &'a A { &self.x }
58 pub fn spam(self) -> isize { self.x.a }
59 }
60
61 trait Nus { fn f(&self); }
62 impl Nus for thing { fn f(&self) {} }
63
64 pub fn main() {
65 let y: Box<_> = Box::new(thing(A {a: 10}));
66 assert_eq!(y.clone().bar(), 10);
67 assert_eq!(y.quux(), 10);
68
69 let z = thing(A {a: 11});
70 assert_eq!(z.spam(), 11);
71 }