]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/explicit-self.rs
Rollup merge of #53545 - FelixMcFelix:fix-50865-beta, r=petrochenkov
[rust.git] / src / test / run-pass / explicit-self.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(box_syntax)]
12
13 static tau: f64 = 2.0*3.14159265358979323;
14
15 struct Point {x: f64, y: f64}
16 struct Size {w: f64, h: f64}
17 enum shape {
18     circle(Point, f64),
19     rectangle(Point, Size)
20 }
21
22
23 fn compute_area(shape: &shape) -> f64 {
24     match *shape {
25         shape::circle(_, radius) => 0.5 * tau * radius * radius,
26         shape::rectangle(_, ref size) => size.w * size.h
27     }
28 }
29
30 impl shape {
31     // self is in the implicit self region
32     pub fn select<'r, T>(&self, threshold: f64, a: &'r T, b: &'r T)
33                          -> &'r T {
34         if compute_area(self) > threshold {a} else {b}
35     }
36 }
37
38 fn select_based_on_unit_circle<'r, T>(
39     threshold: f64, a: &'r T, b: &'r T) -> &'r T {
40
41     let shape = &shape::circle(Point{x: 0.0, y: 0.0}, 1.0);
42     shape.select(threshold, a, b)
43 }
44
45 #[derive(Clone)]
46 struct thing {
47     x: A
48 }
49
50 #[derive(Clone)]
51 struct A {
52     a: isize
53 }
54
55 fn thing(x: A) -> thing {
56     thing {
57         x: x
58     }
59 }
60
61 impl thing {
62     pub fn bar(self: Box<thing>) -> isize { self.x.a }
63     pub fn quux(&self) -> isize { self.x.a }
64     pub fn baz<'a>(&'a self) -> &'a A { &self.x }
65     pub fn spam(self) -> isize { self.x.a }
66 }
67
68 trait Nus { fn f(&self); }
69 impl Nus for thing { fn f(&self) {} }
70
71 pub fn main() {
72     let y: Box<_> = box thing(A {a: 10});
73     assert_eq!(y.clone().bar(), 10);
74     assert_eq!(y.quux(), 10);
75
76     let z = thing(A {a: 11});
77     assert_eq!(z.spam(), 11);
78 }