]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/bug-7183-generics.rs
a3bb02d1d0037835f8cb96ed1d03c7047b74e810
[rustc.git] / src / test / run-pass / bug-7183-generics.rs
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
11 trait Speak : Sized {
12 fn say(&self, s:&str) -> String;
13 fn hi(&self) -> String { hello(self) }
14 }
15
16 fn hello<S:Speak>(s:&S) -> String{
17 s.say("hello")
18 }
19
20 impl Speak for int {
21 fn say(&self, s:&str) -> String {
22 format!("{}: {}", s, *self)
23 }
24 }
25
26 impl<T: Speak> Speak for Option<T> {
27 fn say(&self, s:&str) -> String {
28 match *self {
29 None => format!("{} - none", s),
30 Some(ref x) => { format!("something!{}", x.say(s)) }
31 }
32 }
33 }
34
35
36 pub fn main() {
37 assert_eq!(3.hi(), "hello: 3".to_string());
38 assert_eq!(Some(Some(3)).hi(),
39 "something!something!hello: 3".to_string());
40 assert_eq!(None::<int>.hi(), "hello - none".to_string());
41
42 assert_eq!(Some(None::<int>).hi(), "something!hello - none".to_string());
43 assert_eq!(Some(3).hi(), "something!hello: 3".to_string());
44 }