]>
Commit | Line | Data |
---|---|---|
970d7e83 LB |
1 | // Copyright 2013 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 | ||
c34b1796 AL |
11 | // pretty-expanded FIXME #23616 |
12 | ||
1a4d82fc JJ |
13 | trait Speak : Sized { |
14 | fn say(&self, s:&str) -> String; | |
15 | fn hi(&self) -> String { hello(self) } | |
970d7e83 LB |
16 | } |
17 | ||
1a4d82fc | 18 | fn hello<S:Speak>(s:&S) -> String{ |
970d7e83 LB |
19 | s.say("hello") |
20 | } | |
21 | ||
c34b1796 | 22 | impl Speak for isize { |
1a4d82fc JJ |
23 | fn say(&self, s:&str) -> String { |
24 | format!("{}: {}", s, *self) | |
970d7e83 LB |
25 | } |
26 | } | |
27 | ||
28 | impl<T: Speak> Speak for Option<T> { | |
1a4d82fc | 29 | fn say(&self, s:&str) -> String { |
970d7e83 | 30 | match *self { |
1a4d82fc JJ |
31 | None => format!("{} - none", s), |
32 | Some(ref x) => { format!("something!{}", x.say(s)) } | |
970d7e83 LB |
33 | } |
34 | } | |
35 | } | |
36 | ||
37 | ||
1a4d82fc | 38 | pub fn main() { |
85aaf69f SL |
39 | assert_eq!(3.hi(), "hello: 3".to_string()); |
40 | assert_eq!(Some(Some(3)).hi(), | |
1a4d82fc | 41 | "something!something!hello: 3".to_string()); |
c34b1796 | 42 | assert_eq!(None::<isize>.hi(), "hello - none".to_string()); |
970d7e83 | 43 | |
c34b1796 | 44 | assert_eq!(Some(None::<isize>).hi(), "something!hello - none".to_string()); |
85aaf69f | 45 | assert_eq!(Some(3).hi(), "something!hello: 3".to_string()); |
970d7e83 | 46 | } |