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