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