2013-06-28 19:47:44 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
trait Speak {
|
2014-05-12 19:56:43 -05:00
|
|
|
fn say(&self, s:&str) -> StrBuf;
|
|
|
|
fn hi(&self) -> StrBuf { hello(self) }
|
2013-06-28 19:47:44 -05:00
|
|
|
}
|
|
|
|
|
2014-05-12 19:56:43 -05:00
|
|
|
fn hello<S:Speak>(s:&S) -> StrBuf{
|
2013-06-28 19:47:44 -05:00
|
|
|
s.say("hello")
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Speak for int {
|
2014-05-12 19:56:43 -05:00
|
|
|
fn say(&self, s:&str) -> StrBuf {
|
|
|
|
format_strbuf!("{}: {}", s, *self)
|
2013-06-28 19:47:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Speak> Speak for Option<T> {
|
2014-05-12 19:56:43 -05:00
|
|
|
fn say(&self, s:&str) -> StrBuf {
|
2013-06-28 19:47:44 -05:00
|
|
|
match *self {
|
2014-05-12 19:56:43 -05:00
|
|
|
None => format_strbuf!("{} - none", s),
|
|
|
|
Some(ref x) => { format_strbuf!("something!{}", x.say(s)) }
|
2013-06-28 19:47:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-25 02:43:37 -05:00
|
|
|
pub fn main() {
|
2014-05-12 19:56:43 -05:00
|
|
|
assert_eq!(3.hi(), "hello: 3".to_strbuf());
|
|
|
|
assert_eq!(Some(Some(3)).hi(),
|
|
|
|
"something!something!hello: 3".to_strbuf());
|
|
|
|
assert_eq!(None::<int>.hi(), "hello - none".to_strbuf());
|
2013-06-28 19:47:44 -05:00
|
|
|
|
2014-05-12 19:56:43 -05:00
|
|
|
assert_eq!(Some(None::<int>).hi(), "something!hello - none".to_strbuf());
|
|
|
|
assert_eq!(Some(3).hi(), "something!hello: 3".to_strbuf());
|
2013-06-28 19:47:44 -05:00
|
|
|
}
|