Skip to content

Commit 9340fd5

Browse files
committed
Add tests for some default method things.
1 parent c05165b commit 9340fd5

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
pub trait Clone2 {
12+
fn clone(&self) -> Self;
13+
}
14+
15+
16+
trait Getter<T: Clone2> {
17+
fn get(&self) -> T;
18+
}
19+
20+
impl Getter<int> for int { //~ ERROR failed to find an implementation of trait Clone2 for int
21+
fn get(&self) -> int { *self }
22+
}
23+
24+
fn main() { }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
#[allow(default_methods)]
12+
trait Speak {
13+
fn say(&self, s:&str) -> ~str;
14+
fn hi(&self) -> ~str { hello(self) }
15+
}
16+
17+
fn hello<S:Speak>(s:&S) -> ~str{
18+
s.say("hello")
19+
}
20+
21+
impl Speak for int {
22+
fn say(&self, s:&str) -> ~str {
23+
fmt!("%s: %d", s, *self)
24+
}
25+
}
26+
27+
impl<T: Speak> Speak for Option<T> {
28+
fn say(&self, s:&str) -> ~str {
29+
match *self {
30+
None => fmt!("%s - none", s),
31+
Some(ref x) => { ~"something!" + x.say(s) }
32+
}
33+
}
34+
}
35+
36+
37+
fn main() {
38+
assert_eq!(3.hi(), ~"hello: 3");
39+
assert_eq!(Some(Some(3)).hi(), ~"something!something!hello: 3");
40+
assert_eq!(None::<int>.hi(), ~"hello - none");
41+
42+
// These fail because of a bug in monomorphization's ID generation.
43+
//assert_eq!(Some(None::<int>).hi(), ~"something!hello - none");
44+
//assert_eq!(Some(3).hi(), ~"something!hello: 3");
45+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
pub trait Clone2 {
12+
/// Returns a copy of the value. The contents of owned pointers
13+
/// are copied to maintain uniqueness, while the contents of
14+
/// managed pointers are not copied.
15+
fn clone(&self) -> Self;
16+
}
17+
18+
#[allow(default_methods)]
19+
trait Getter<T: Clone> {
20+
fn do_get(&self) -> T;
21+
22+
fn do_get2(&self) -> (T, T) {
23+
let x = self.do_get();
24+
(x.clone(), x.clone())
25+
}
26+
27+
}
28+
29+
impl Getter<int> for int {
30+
fn do_get(&self) -> int { *self }
31+
}
32+
33+
impl<T: Clone> Getter<T> for Option<T> {
34+
fn do_get(&self) -> T { self.get_ref().clone() }
35+
}
36+
37+
38+
fn main() {
39+
assert_eq!(3.do_get2(), (3, 3));
40+
assert_eq!(Some(~"hi").do_get2(), (~"hi", ~"hi"));
41+
}

0 commit comments

Comments
 (0)