2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-10-23 03:49:18 -05:00
|
|
|
#[feature(managed_boxes)];
|
2012-08-16 18:44:22 -05:00
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
static tau: f64 = 2.0*3.14159265358979323;
|
2012-08-16 18:44:22 -05:00
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
struct Point {x: f64, y: f64}
|
|
|
|
struct Size {w: f64, h: f64}
|
2012-08-16 18:44:22 -05:00
|
|
|
enum shape {
|
2013-09-26 01:26:09 -05:00
|
|
|
circle(Point, f64),
|
2013-01-26 00:46:32 -06:00
|
|
|
rectangle(Point, Size)
|
2012-08-16 18:44:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
fn compute_area(shape: &shape) -> f64 {
|
2012-08-16 18:44:22 -05:00
|
|
|
match *shape {
|
|
|
|
circle(_, radius) => 0.5 * tau * radius * radius,
|
|
|
|
rectangle(_, ref size) => size.w * size.h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl shape {
|
2012-08-16 18:44:22 -05:00
|
|
|
// self is in the implicit self region
|
2013-09-26 01:26:09 -05:00
|
|
|
pub fn select<'r, T>(&self, threshold: f64, a: &'r T, b: &'r T)
|
2013-05-31 17:17:22 -05:00
|
|
|
-> &'r T {
|
2012-08-16 18:44:22 -05:00
|
|
|
if compute_area(self) > threshold {a} else {b}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-25 15:21:04 -05:00
|
|
|
fn select_based_on_unit_circle<'r, T>(
|
2013-09-26 01:26:09 -05:00
|
|
|
threshold: f64, a: &'r T, b: &'r T) -> &'r T {
|
2012-08-16 18:44:22 -05:00
|
|
|
|
2013-01-26 00:46:32 -06:00
|
|
|
let shape = &circle(Point{x: 0.0, y: 0.0}, 1.0);
|
2012-08-16 18:44:22 -05:00
|
|
|
shape.select(threshold, a, b)
|
|
|
|
}
|
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone)]
|
2012-08-16 18:44:22 -05:00
|
|
|
struct thing {
|
2013-01-26 00:46:32 -06:00
|
|
|
x: A
|
2012-08-16 18:44:22 -05:00
|
|
|
}
|
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone)]
|
|
|
|
struct A {
|
|
|
|
a: @int
|
|
|
|
}
|
2013-01-26 00:46:32 -06:00
|
|
|
|
|
|
|
fn thing(x: A) -> thing {
|
2012-09-05 17:58:43 -05:00
|
|
|
thing {
|
2013-06-27 19:41:35 -05:00
|
|
|
x: x
|
2012-09-05 17:58:43 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-16 18:44:22 -05:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl thing {
|
|
|
|
pub fn foo(@self) -> int { *self.x.a }
|
|
|
|
pub fn bar(~self) -> int { *self.x.a }
|
|
|
|
pub fn quux(&self) -> int { *self.x.a }
|
|
|
|
pub fn baz<'a>(&'a self) -> &'a A { &self.x }
|
|
|
|
pub fn spam(self) -> int { *self.x.a }
|
2012-08-16 18:44:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
trait Nus { fn f(&self); }
|
2013-02-14 13:47:00 -06:00
|
|
|
impl Nus for thing { fn f(&self) {} }
|
2012-08-16 18:44:22 -05:00
|
|
|
|
2013-02-01 21:43:17 -06:00
|
|
|
pub fn main() {
|
2012-08-16 18:44:22 -05:00
|
|
|
|
2013-02-22 18:08:16 -06:00
|
|
|
let x = @thing(A {a: @10});
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(x.foo(), 10);
|
|
|
|
assert_eq!(x.quux(), 10);
|
2012-08-16 18:44:22 -05:00
|
|
|
|
2013-02-22 18:08:16 -06:00
|
|
|
let y = ~thing(A {a: @10});
|
2013-07-02 14:47:32 -05:00
|
|
|
assert_eq!(y.clone().bar(), 10);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(y.quux(), 10);
|
2012-08-16 18:44:22 -05:00
|
|
|
|
2013-02-22 18:08:16 -06:00
|
|
|
let z = thing(A {a: @11});
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(z.spam(), 11);
|
2012-08-16 18:44:22 -05:00
|
|
|
}
|