@@ -1181,8 +1181,8 @@ Traits are implemented for specific types through separate [implementations](#im
11811181# type BoundingBox = int;
11821182
11831183trait Shape {
1184- fn draw(Surface);
1185- fn bounding_box() -> BoundingBox;
1184+ fn draw(&self, Surface);
1185+ fn bounding_box(&self ) -> BoundingBox;
11861186}
11871187~~~~
11881188
@@ -1195,9 +1195,9 @@ These appear after the trait name, using the same syntax used in [generic functi
11951195
11961196~~~~
11971197trait Seq<T> {
1198- fn len() -> uint;
1199- fn elt_at(n: uint) -> T;
1200- fn iter(&fn(T));
1198+ fn len(&self ) -> uint;
1199+ fn elt_at(&self, n: uint) -> T;
1200+ fn iter(&self, & fn(T));
12011201}
12021202~~~~
12031203
@@ -1209,7 +1209,7 @@ For example:
12091209
12101210~~~~
12111211# type Surface = int;
1212- # trait Shape { fn draw(Surface); }
1212+ # trait Shape { fn draw(&self, Surface); }
12131213
12141214fn draw_twice<T: Shape>(surface: Surface, sh: T) {
12151215 sh.draw(surface);
@@ -1271,8 +1271,8 @@ methods of the supertrait may be called on values of subtrait-bound type paramet
12711271Refering to the previous example of ` trait Circle : Shape ` :
12721272
12731273~~~
1274- # trait Shape { fn area() -> float; }
1275- # trait Circle : Shape { fn radius() -> float; }
1274+ # trait Shape { fn area(&self ) -> float; }
1275+ # trait Circle : Shape { fn radius(&self ) -> float; }
12761276fn radius_times_area<T: Circle>(c: T) -> float {
12771277 // `c` is both a Circle and a Shape
12781278 c.radius() * c.area()
@@ -1282,10 +1282,10 @@ fn radius_times_area<T: Circle>(c: T) -> float {
12821282Likewise, supertrait methods may also be called on trait objects.
12831283
12841284~~~ {.xfail-test}
1285- # trait Shape { fn area() -> float; }
1286- # trait Circle : Shape { fn radius() -> float; }
1287- # impl Shape for int { fn area() -> float { 0.0 } }
1288- # impl Circle for int { fn radius() -> float { 0.0 } }
1285+ # trait Shape { fn area(&self ) -> float; }
1286+ # trait Circle : Shape { fn radius(&self ) -> float; }
1287+ # impl Shape for int { fn area(&self ) -> float { 0.0 } }
1288+ # impl Circle for int { fn radius(&self ) -> float { 0.0 } }
12891289# let mycircle = 0;
12901290
12911291let mycircle: Circle = @mycircle as @Circle;
@@ -1302,7 +1302,7 @@ Implementations are defined with the keyword `impl`.
13021302# struct Point {x: float, y: float};
13031303# type Surface = int;
13041304# struct BoundingBox {x: float, y: float, width: float, height: float};
1305- # trait Shape { fn draw(Surface); fn bounding_box() -> BoundingBox; }
1305+ # trait Shape { fn draw(&self, Surface); fn bounding_box(&self ) -> BoundingBox; }
13061306# fn do_draw_circle(s: Surface, c: Circle) { }
13071307
13081308struct Circle {
@@ -1311,8 +1311,8 @@ struct Circle {
13111311}
13121312
13131313impl Shape for Circle {
1314- fn draw(s: Surface) { do_draw_circle(s, self); }
1315- fn bounding_box() -> BoundingBox {
1314+ fn draw(&self, s: Surface) { do_draw_circle(s, * self); }
1315+ fn bounding_box(&self ) -> BoundingBox {
13161316 let r = self.radius;
13171317 BoundingBox{x: self.center.x - r, y: self.center.y - r,
13181318 width: 2.0 * r, height: 2.0 * r}
@@ -2678,11 +2678,11 @@ An example of an object type:
26782678
26792679~~~~~~~~
26802680trait Printable {
2681- fn to_str() -> ~str;
2681+ fn to_str(&self ) -> ~str;
26822682}
26832683
26842684impl Printable for int {
2685- fn to_str() -> ~str { int::to_str(self) }
2685+ fn to_str(&self ) -> ~str { int::to_str(* self) }
26862686}
26872687
26882688fn print(a: @Printable) {
@@ -2721,11 +2721,11 @@ example, in:
27212721
27222722~~~~~~~~
27232723trait Printable {
2724- fn make_string() -> ~str;
2724+ fn make_string(&self ) -> ~str;
27252725}
27262726
27272727impl Printable for ~str {
2728- fn make_string() -> ~str { copy self }
2728+ fn make_string(&self ) -> ~str { copy * self }
27292729}
27302730~~~~~~~~
27312731
0 commit comments