Skip to content

Commit 013b5db

Browse files
committed
---
yaml --- r: 31443 b: refs/heads/dist-snap c: f1f9066 h: refs/heads/master i: 31441: 9b33413 31439: c8a6c75 v: v3
1 parent 6d36154 commit 013b5db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+562
-860
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
10-
refs/heads/dist-snap: afd9a75c9ebf56b0a387928cdbef7d086200f534
10+
refs/heads/dist-snap: f1f9066274114637510a48ab1087750262ae0699
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/dist-snap/doc/tutorial.md

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ high-level features include:
3434
* ***Higher-order functions.*** Rust functions may take closures as
3535
arguments or return closures as return values. Closures in Rust are
3636
very powerful and used pervasively.
37-
* ***Trait polymorphism.*** Rust's type system features a unique
38-
combination of Java-style interfaces and Haskell-style typeclasses
39-
called _traits_.
37+
* ***Interface polymorphism.*** Rust's type system features a unique
38+
combination of Java-style interfaces and Haskell-style typeclasses.
4039
* ***Parametric polymorphism (generics).*** Functions and types can be
4140
parameterized over type variables with optional type constraints.
4241
* ***Type inference.*** Type annotations on local variable
@@ -2090,9 +2089,9 @@ resource type. Rust has several kinds that can be used as type bounds:
20902089
mutable fields nor shared boxes.
20912090

20922091
> ***Note:*** Rust type kinds are syntactically very similar to
2093-
> [traits](#traits) when used as type bounds, and can be
2094-
> conveniently thought of as built-in traits. In the future type
2095-
> kinds will actually be traits that the compiler has special
2092+
> [interfaces](#interfaces) when used as type bounds, and can be
2093+
> conveniently thought of as built-in interfaces. In the future type
2094+
> kinds will actually be interfaces that the compiler has special
20962095
> knowledge about.
20972096
20982097
## Generic functions and argument-passing
@@ -2389,9 +2388,9 @@ This makes it possible to rebind a variable without actually mutating
23892388
it, which is mostly useful for destructuring (which can rebind, but
23902389
not assign).
23912390

2392-
# Traits
2391+
# Interfaces
23932392

2394-
Traits are Rust's take on value polymorphism—the thing that
2393+
Interfaces are Rust's take on value polymorphism—the thing that
23952394
object-oriented languages tend to solve with methods and inheritance.
23962395
For example, writing a function that can operate on multiple types of
23972396
collections.
@@ -2401,27 +2400,27 @@ collections.
24012400
24022401
## Declaration
24032402

2404-
A trait consists of a set of methods. A method is a function that
2403+
An interface consists of a set of methods. A method is a function that
24052404
can be applied to a `self` value and a number of arguments, using the
24062405
dot notation: `self.foo(arg1, arg2)`.
24072406

2408-
For example, we could declare the trait `to_str` for things that
2407+
For example, we could declare the interface `to_str` for things that
24092408
can be converted to a string, with a single method of the same name:
24102409

24112410
~~~~
2412-
trait to_str {
2411+
iface to_str {
24132412
fn to_str() -> ~str;
24142413
}
24152414
~~~~
24162415

24172416
## Implementation
24182417

2419-
To actually implement an trait for a given type, the `impl` form
2418+
To actually implement an interface for a given type, the `impl` form
24202419
is used. This defines implementations of `to_str` for the `int` and
24212420
`~str` types.
24222421

24232422
~~~~
2424-
# trait to_str { fn to_str() -> ~str; }
2423+
# iface to_str { fn to_str() -> ~str; }
24252424
impl of to_str for int {
24262425
fn to_str() -> ~str { int::to_str(self, 10u) }
24272426
}
@@ -2440,13 +2439,13 @@ method that matches the name, and simply calls that.
24402439

24412440
Implementations are not globally visible. Resolving a method to an
24422441
implementation requires that implementation to be in scope. You can
2443-
import and export implementations using the name of the trait they
2442+
import and export implementations using the name of the interface they
24442443
implement (multiple implementations with the same name can be in scope
24452444
without problems). Or you can give them an explicit name if you
24462445
prefer, using this syntax:
24472446

24482447
~~~~
2449-
# trait to_str { fn to_str() -> ~str; }
2448+
# iface to_str { fn to_str() -> ~str; }
24502449
impl nil_to_str of to_str for () {
24512450
fn to_str() -> ~str { ~"()" }
24522451
}
@@ -2462,7 +2461,7 @@ known at compile time, it is possible to specify 'bounds' for type
24622461
parameters.
24632462

24642463
~~~~
2465-
# trait to_str { fn to_str() -> ~str; }
2464+
# iface to_str { fn to_str() -> ~str; }
24662465
fn comma_sep<T: to_str>(elts: ~[T]) -> ~str {
24672466
let mut result = ~"", first = true;
24682467
for elts.each |elt| {
@@ -2477,18 +2476,18 @@ fn comma_sep<T: to_str>(elts: ~[T]) -> ~str {
24772476
The syntax for this is similar to the syntax for specifying that a
24782477
parameter type has to be copyable (which is, in principle, another
24792478
kind of bound). By declaring `T` as conforming to the `to_str`
2480-
trait, it becomes possible to call methods from that trait on
2479+
interface, it becomes possible to call methods from that interface on
24812480
values of that type inside the function. It will also cause a
24822481
compile-time error when anyone tries to call `comma_sep` on an array
24832482
whose element type does not have a `to_str` implementation in scope.
24842483

2485-
## Polymorphic traits
2484+
## Polymorphic interfaces
24862485

2487-
Traits may contain type parameters. This defines a trait for
2486+
Interfaces may contain type parameters. This defines an interface for
24882487
generalized sequence types:
24892488

24902489
~~~~
2491-
trait seq<T> {
2490+
iface seq<T> {
24922491
fn len() -> uint;
24932492
fn iter(fn(T));
24942493
}
@@ -2501,25 +2500,25 @@ impl <T> of seq<T> for ~[T] {
25012500
~~~~
25022501

25032502
Note that the implementation has to explicitly declare the its
2504-
parameter `T` before using it to specify its trait type. This is
2503+
parameter `T` before using it to specify its interface type. This is
25052504
needed because it could also, for example, specify an implementation
25062505
of `seq<int>`—the `of` clause *refers* to a type, rather than defining
25072506
one.
25082507

2509-
The type parameters bound by a trait are in scope in each of the
2508+
The type parameters bound by an iface are in scope in each of the
25102509
method declarations. So, re-declaring the type parameter
2511-
`T` as an explicit type parameter for `len` -- in either the trait or
2510+
`T` as an explicit type parameter for `len` -- in either the iface or
25122511
the impl -- would be a compile-time error.
25132512

2514-
## The `self` type in traits
2513+
## The `self` type in interfaces
25152514

2516-
In a trait, `self` is a special type that you can think of as a
2517-
type parameter. An implementation of the trait for any given type
2515+
In an interface, `self` is a special type that you can think of as a
2516+
type parameter. An implementation of the interface for any given type
25182517
`T` replaces the `self` type parameter with `T`. The following
2519-
trait describes types that support an equality operation:
2518+
interface describes types that support an equality operation:
25202519

25212520
~~~~
2522-
trait eq {
2521+
iface eq {
25232522
fn equals(&&other: self) -> bool;
25242523
}
25252524
@@ -2531,15 +2530,15 @@ impl of eq for int {
25312530
Notice that `equals` takes an `int` argument, rather than a `self` argument, in
25322531
an implementation for type `int`.
25332532

2534-
## Casting to an trait type
2533+
## Casting to an interface type
25352534

25362535
The above allows us to define functions that polymorphically act on
2537-
values of *an* unknown type that conforms to a given trait.
2536+
values of *an* unknown type that conforms to a given interface.
25382537
However, consider this function:
25392538

25402539
~~~~
25412540
# type circle = int; type rectangle = int;
2542-
# trait drawable { fn draw(); }
2541+
# iface drawable { fn draw(); }
25432542
# impl of drawable for int { fn draw() {} }
25442543
# fn new_circle() -> int { 1 }
25452544
fn draw_all<T: drawable>(shapes: ~[T]) {
@@ -2550,14 +2549,14 @@ fn draw_all<T: drawable>(shapes: ~[T]) {
25502549
~~~~
25512550

25522551
You can call that on an array of circles, or an array of squares
2553-
(assuming those have suitable `drawable` traits defined), but not
2552+
(assuming those have suitable `drawable` interfaces defined), but not
25542553
on an array containing both circles and squares.
25552554

2556-
When this is needed, a trait name can be used as a type, causing
2555+
When this is needed, an interface name can be used as a type, causing
25572556
the function to be written simply like this:
25582557

25592558
~~~~
2560-
# trait drawable { fn draw(); }
2559+
# iface drawable { fn draw(); }
25612560
fn draw_all(shapes: ~[drawable]) {
25622561
for shapes.each |shape| { shape.draw(); }
25632562
}
@@ -2572,11 +2571,11 @@ is very similar to the 'vtables' used in most object-oriented
25722571
languages.
25732572

25742573
To construct such a value, you use the `as` operator to cast a value
2575-
to a trait type:
2574+
to an interface type:
25762575

25772576
~~~~
25782577
# type circle = int; type rectangle = int;
2579-
# trait drawable { fn draw(); }
2578+
# iface drawable { fn draw(); }
25802579
# impl of drawable for int { fn draw() {} }
25812580
# fn new_circle() -> int { 1 }
25822581
# fn new_rectangle() -> int { 2 }
@@ -2595,10 +2594,10 @@ Note that the allocation of a box is somewhat more expensive than
25952594
simply using a type parameter and passing in the value as-is, and much
25962595
more expensive than statically resolved method calls.
25972596

2598-
## Trait-less implementations
2597+
## Interface-less implementations
25992598

26002599
If you only intend to use an implementation for static overloading,
2601-
and there is no trait available that it conforms to, you are free
2600+
and there is no interface available that it conforms to, you are free
26022601
to leave off the `of` clause. However, this is only possible when you
26032602
are defining an implementation in the same module as the receiver
26042603
type, and the receiver type is a named type (i.e., an enum or a

branches/dist-snap/mk/tests.mk

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ cleantestlibs:
9595
check: cleantestlibs cleantmptestlogs tidy all check-stage2
9696
$(Q)$(S)src/etc/check-summary.py tmp/*.log
9797

98-
check-notidy: cleantestlibs cleantmptestlogs all check-stage2
99-
$(Q)$(S)src/etc/check-summary.py tmp/*.log
100-
10198
check-full: cleantestlibs cleantmptestlogs tidy \
10299
all check-stage1 check-stage2 check-stage3
103100
$(Q)$(S)src/etc/check-summary.py tmp/*.log

branches/dist-snap/src/libcore/arc.rs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
* share immutable data between tasks.
44
*/
55

6+
import comm::{port, chan, methods};
67
import sys::methods;
78

8-
export arc, get, clone;
9+
export arc, get, clone, shared_arc, get_arc;
910

1011
export exclusive, methods;
1112

@@ -121,6 +122,49 @@ impl methods<T: send> for exclusive<T> {
121122
}
122123
}
123124

125+
// Convenience code for sharing arcs between tasks
126+
127+
type get_chan<T: const send> = chan<chan<arc<T>>>;
128+
129+
// (terminate, get)
130+
type shared_arc<T: const send> = (shared_arc_res, get_chan<T>);
131+
132+
class shared_arc_res {
133+
let c: comm::chan<()>;
134+
new(c: comm::chan<()>) { self.c = c; }
135+
drop { self.c.send(()); }
136+
}
137+
138+
fn shared_arc<T: send const>(-data: T) -> shared_arc<T> {
139+
let a = arc::arc(data);
140+
let p = port();
141+
let c = chan(p);
142+
do task::spawn() |move a| {
143+
let mut live = true;
144+
let terminate = port();
145+
let get = port();
146+
147+
c.send((chan(terminate), chan(get)));
148+
149+
while live {
150+
alt comm::select2(terminate, get) {
151+
either::left(()) { live = false; }
152+
either::right(cc) {
153+
comm::send(cc, arc::clone(&a));
154+
}
155+
}
156+
}
157+
};
158+
let (terminate, get) = p.recv();
159+
(shared_arc_res(terminate), get)
160+
}
161+
162+
fn get_arc<T: send const>(c: get_chan<T>) -> arc::arc<T> {
163+
let p = port();
164+
c.send(chan(p));
165+
p.recv()
166+
}
167+
124168
#[cfg(test)]
125169
mod tests {
126170
import comm::*;
@@ -152,6 +196,25 @@ mod tests {
152196
log(info, arc_v);
153197
}
154198

199+
#[test]
200+
fn auto_share_arc() {
201+
let v = ~[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
202+
let (_res, arc_c) = shared_arc(v);
203+
204+
let p = port();
205+
let c = chan(p);
206+
207+
do task::spawn() {
208+
let arc_v = get_arc(arc_c);
209+
let v = *get(&arc_v);
210+
assert v[2] == 3;
211+
212+
c.send(());
213+
};
214+
215+
assert p.recv() == ();
216+
}
217+
155218
#[test]
156219
#[ignore] // this can probably infinite loop too.
157220
fn exclusive_arc() {

branches/dist-snap/src/libcore/cmp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/// Interfaces used for comparison.
22
3-
trait ord {
4-
pure fn lt(&&other: self) -> bool;
3+
iface ord {
4+
fn lt(&&other: self) -> bool;
55
}
66

7-
trait eq {
8-
pure fn eq(&&other: self) -> bool;
7+
iface eq {
8+
fn eq(&&other: self) -> bool;
99
}
1010

branches/dist-snap/src/libcore/core.rc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ mod tuple;
161161

162162
// Ubiquitous-utility-type modules
163163

164-
mod ops;
165164
mod cmp;
166165
mod num;
167166
mod hash;

branches/dist-snap/src/libcore/core.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ import float::num;
3030
import f32::num;
3131
import f64::num;
3232
import num::num;
33-
import ops::{const, copy, send, owned};
34-
import ops::{add, sub, mul, div, modulo, neg, bitops, index};
3533

3634
export path, option, some, none, unreachable;
3735
export extensions;
@@ -44,9 +42,6 @@ export immutable_copyable_vector, iter_trait_extensions, vec_concat;
4442
export base_iter, copyable_iter, extended_iter;
4543
export tuple_ops, extended_tuple_ops;
4644
export ptr;
47-
// The following exports are the core operators and kinds
48-
export const, copy, send, owned;
49-
export add, sub, mul, div, modulo, neg, bitops, index;
5045

5146
// Export the log levels as global constants. Higher levels mean
5247
// more-verbosity. Error is the bottom level, default logging level is

0 commit comments

Comments
 (0)