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.
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.
11 #![feature(box_syntax)]
13 static tau: f64 = 2.0*3.14159265358979323;
15 struct Point {x: f64, y: f64}
16 struct Size {w: f64, h: f64}
19 rectangle(Point, Size)
23 fn compute_area(shape: &shape) -> f64 {
25 shape::circle(_, radius) => 0.5 * tau * radius * radius,
26 shape::rectangle(_, ref size) => size.w * size.h
31 // self is in the implicit self region
32 pub fn select<'r, T>(&self, threshold: f64, a: &'r T, b: &'r T)
34 if compute_area(self) > threshold {a} else {b}
38 fn select_based_on_unit_circle<'r, T>(
39 threshold: f64, a: &'r T, b: &'r T) -> &'r T {
41 let shape = &shape::circle(Point{x: 0.0, y: 0.0}, 1.0);
42 shape.select(threshold, a, b)
55 fn thing(x: A) -> 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 }
68 trait Nus { fn f(&self); }
69 impl Nus for thing { fn f(&self) {} }
72 let y: Box<_> = box thing(A {a: 10});
73 assert_eq!(y.clone().bar(), 10);
74 assert_eq!(y.quux(), 10);
76 let z = thing(A {a: 11});
77 assert_eq!(z.spam(), 11);