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 #![allow(unknown_features)]
12 #![feature(box_syntax)]
14 static tau: f64 = 2.0*3.14159265358979323;
16 struct Point {x: f64, y: f64}
17 struct Size {w: f64, h: f64}
20 rectangle(Point, Size)
24 fn compute_area(shape: &shape) -> f64 {
26 shape::circle(_, radius) => 0.5 * tau * radius * radius,
27 shape::rectangle(_, ref size) => size.w * size.h
32 // self is in the implicit self region
33 pub fn select<'r, T>(&self, threshold: f64, a: &'r T, b: &'r T)
35 if compute_area(self) > threshold {a} else {b}
39 fn select_based_on_unit_circle<'r, T>(
40 threshold: f64, a: &'r T, b: &'r T) -> &'r T {
42 let shape = &shape::circle(Point{x: 0.0, y: 0.0}, 1.0);
43 shape.select(threshold, a, b)
56 fn thing(x: A) -> thing {
63 pub fn bar(self: Box<thing>) -> int { self.x.a }
64 pub fn quux(&self) -> int { self.x.a }
65 pub fn baz<'a>(&'a self) -> &'a A { &self.x }
66 pub fn spam(self) -> int { self.x.a }
69 trait Nus { fn f(&self); }
70 impl Nus for thing { fn f(&self) {} }
73 let y = box thing(A {a: 10});
74 assert_eq!(y.clone().bar(), 10);
75 assert_eq!(y.quux(), 10);
77 let z = thing(A {a: 11});
78 assert_eq!(z.spam(), 11);