2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
2018-09-25 16:51:35 -05:00
|
|
|
#![allow(dead_code)]
|
2018-08-31 08:02:01 -05:00
|
|
|
#![allow(non_camel_case_types)]
|
|
|
|
#![allow(non_upper_case_globals)]
|
|
|
|
|
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 {
|
2014-11-06 02:05:53 -06:00
|
|
|
shape::circle(_, radius) => 0.5 * tau * radius * radius,
|
|
|
|
shape::rectangle(_, ref size) => size.w * size.h
|
2012-08-16 18:44:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-11-06 02:05:53 -06:00
|
|
|
let shape = &shape::circle(Point{x: 0.0, y: 0.0}, 1.0);
|
2012-08-16 18:44:22 -05:00
|
|
|
shape.select(threshold, a, b)
|
|
|
|
}
|
|
|
|
|
2014-12-30 22:32:49 -06:00
|
|
|
#[derive(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
|
|
|
}
|
|
|
|
|
2014-12-30 22:32:49 -06:00
|
|
|
#[derive(Clone)]
|
2013-07-02 14:47:32 -05:00
|
|
|
struct A {
|
2015-03-25 19:06:52 -05:00
|
|
|
a: isize
|
2013-07-02 14:47:32 -05:00
|
|
|
}
|
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 {
|
2015-03-25 19:06:52 -05:00
|
|
|
pub fn bar(self: Box<thing>) -> isize { self.x.a }
|
|
|
|
pub fn quux(&self) -> isize { self.x.a }
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn baz<'a>(&'a self) -> &'a A { &self.x }
|
2015-03-25 19:06:52 -05:00
|
|
|
pub fn spam(self) -> isize { 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() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let y: Box<_> = Box::new(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
|
|
|
|
2014-02-06 16:38:33 -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
|
|
|
}
|