]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/bug-7183-generics.rs
test: Remove all uses of `~str` from the test suite.
[rust.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 {
12     fn say(&self, s:&str) -> StrBuf;
13     fn hi(&self) -> StrBuf { hello(self) }
14 }
15
16 fn hello<S:Speak>(s:&S) -> StrBuf{
17     s.say("hello")
18 }
19
20 impl Speak for int {
21     fn say(&self, s:&str) -> StrBuf {
22         format_strbuf!("{}: {}", s, *self)
23     }
24 }
25
26 impl<T: Speak> Speak for Option<T> {
27     fn say(&self, s:&str) -> StrBuf {
28         match *self {
29             None => format_strbuf!("{} - none", s),
30             Some(ref x) => { format_strbuf!("something!{}", x.say(s)) }
31         }
32     }
33 }
34
35
36 pub fn main() {
37     assert_eq!(3.hi(), "hello: 3".to_strbuf());
38     assert_eq!(Some(Some(3)).hi(),
39                "something!something!hello: 3".to_strbuf());
40     assert_eq!(None::<int>.hi(), "hello - none".to_strbuf());
41
42     assert_eq!(Some(None::<int>).hi(), "something!hello - none".to_strbuf());
43     assert_eq!(Some(3).hi(), "something!hello: 3".to_strbuf());
44 }