Skip to content

Commit b4f4445

Browse files
---
yaml --- r: 29931 b: refs/heads/incoming c: 076dab9 h: refs/heads/master i: 29929: db9ee6c 29927: 53ae86a v: v3
1 parent 1e8e509 commit b4f4445

File tree

186 files changed

+1871
-1566
lines changed

Some content is hidden

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

186 files changed

+1871
-1566
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
9-
refs/heads/incoming: a14485b7fd1a8a268a456ee1b47d79b10ccac875
9+
refs/heads/incoming: 076dab92f73bb882e59e2f6a1ee6ba2b47bf6110
1010
refs/heads/dist-snap: 2f32a1581f522e524009138b33b1c7049ced668d
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/AUTHORS.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ Jeff Muizelaar <[email protected]>
4949
Jeff Olson <[email protected]>
5050
Jeffrey Yasskin <[email protected]>
5151
Jesse Ruderman <[email protected]>
52-
Jim Blandy <[email protected]>
5352
Joe Pletcher <[email protected]>
5453
Jon Morton <[email protected]>
5554
Jonathan Sternberg <[email protected]>

branches/incoming/doc/rust.md

Lines changed: 163 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ The keywords in [source files](#source-files) are the following strings:
211211
~~~~~~~~ {.keyword}
212212
again assert
213213
break
214-
check const copy
214+
check class const copy
215215
drop
216216
else enum export extern
217217
fail false fn for
@@ -220,7 +220,6 @@ let log loop
220220
match mod mut
221221
pure
222222
return
223-
struct
224223
true trait type
225224
unchecked unsafe
226225
while
@@ -1098,6 +1097,151 @@ enum list<T> {
10981097
let a: list<int> = cons(7, @cons(13, @nil));
10991098
~~~~
11001099

1100+
### Classes
1101+
1102+
A _class_ is a named record type that collects together fields and
1103+
methods. It must have a _constructor_ (a function called `new` that
1104+
returns a new instance of the class), and may have a destructor (a
1105+
nullary function called `drop` that executes before the memory manager
1106+
frees the memory for a given class). For historical reasons, we may
1107+
call a class with a destructor and a single field a "resource".
1108+
1109+
A _class item_ declares a class type:
1110+
1111+
~~~~
1112+
class file_descriptor {
1113+
let fd: libc::c_int;
1114+
new(fd: libc::c_int) { self.fd = fd; }
1115+
drop { libc::close(self.fd); }
1116+
}
1117+
~~~~
1118+
1119+
Calling the `file_descriptor` constructor function on an integer will
1120+
produce a value with the `file_descriptor` type.
1121+
1122+
_Fields_ are immutable by default, so instances of `file_descriptor`
1123+
can't have their `fd` fields reassigned. A mutable field declaration
1124+
looks like:
1125+
1126+
~~~~
1127+
let mut fd: libc::c_int;
1128+
~~~~
1129+
1130+
The only exception is that the body of the class constructor begins
1131+
with all the class's fields uninitialized, and is allowed to -- in
1132+
fact, must -- initialize all the fields. The compiler enforces this
1133+
invariant.
1134+
1135+
Usually, the class constructor stores its argument or arguments in the
1136+
class's named fields. In this case, the `file_descriptor`'s data field
1137+
would be accessed like `f.fd`, if `f` is a value of type
1138+
`file_descriptor`. By default, class fields are _public_: they can be
1139+
accessed both from methods inside the class, and code outside the
1140+
class. Classes can also have private fields:
1141+
1142+
~~~~
1143+
class file_descriptor {
1144+
let fd: *libc::FILE;
1145+
new(fd: *libc::FILE) {
1146+
self.fd = fd; self.name = none;
1147+
}
1148+
priv {
1149+
let mut name: option<~str>;
1150+
}
1151+
fn get_name() -> ~str {
1152+
match self.name {
1153+
none => fail ~"File has no name!",
1154+
some(n) => n
1155+
}
1156+
}
1157+
}
1158+
~~~~
1159+
1160+
Private fields are instance-private: methods in a class `C` can access
1161+
`self`'s private fields, but not private fields of other values of
1162+
type `C`. Code outside a class can't access any private fields.
1163+
1164+
A class item may contain _methods_, which take an implicit `self`
1165+
argument:
1166+
1167+
~~~~
1168+
class file_descriptor {
1169+
let fd: *libc::FILE;
1170+
new(fd: *libc::FILE) { self.fd = fd; }
1171+
fn flush() {
1172+
libc::fflush(self.fd);
1173+
}
1174+
}
1175+
~~~~
1176+
1177+
In this case, ```open``` is a nullary method that calls the
1178+
```fopen``` function, defined in another library, on the ```fd```
1179+
field. As in this example, methods must refer to their self's fields
1180+
as fields of ```self```; bare references to ```fd``` can't
1181+
occur. Methods can be public or private; just like fields, they are
1182+
public by default and private if enclosed in a `priv` section.
1183+
1184+
Classes may be polymorphic:
1185+
1186+
~~~~
1187+
class file<A: copy> {
1188+
let data: A;
1189+
let fd: *libc::FILE;
1190+
new(data: A, fd: *libc::FILE) { self.data = data; self.fd = fd; }
1191+
}
1192+
~~~~
1193+
1194+
Methods may also be polymorphic, and can have additional type
1195+
parameters other than those bound in the class:
1196+
1197+
~~~~
1198+
class file<A: copy> {
1199+
let data: A;
1200+
let fd: *libc::FILE;
1201+
new(fd: *libc::FILE, data: A) { self.fd = fd; self.data = data; }
1202+
fn map_data<B>(f: fn(A) -> B) -> B {
1203+
f(self.data)
1204+
}
1205+
}
1206+
~~~~
1207+
1208+
Classes do not support inheritance, except through traits. As a
1209+
result, all class method dispatch is static (non-virtual).
1210+
1211+
A class may implement a trait (see [traits](#traits)):
1212+
1213+
~~~~
1214+
trait to_str {
1215+
fn to_str() -> ~str;
1216+
}
1217+
1218+
class file : to_str {
1219+
let fd: *libc::FILE;
1220+
new(fd: *libc::FILE) { self.fd = fd; }
1221+
fn to_str() -> ~str { ~"a file" }
1222+
}
1223+
~~~~
1224+
1225+
The syntax `class file: to_str` is pronounced "class `file`
1226+
implements trait `to_str`".
1227+
1228+
Class instances may be allocated on the stack, in the exchange heap,
1229+
or on the task heap. A value with a class type ```C``` has a
1230+
noncopyable [type kind](#type-kinds) if ```C``` has a destructor, and
1231+
thus may not be copied. Class types that don't have destructors may be
1232+
copied if all their fields are copyable.
1233+
1234+
The semantics guarantee that for each constructed resource value, the
1235+
destructor will run once: when the value is disposed of (barring
1236+
drastic program termination that somehow prevents unwinding from
1237+
taking place). For stack-allocated values, disposal happens when the
1238+
value goes out of scope. For values in shared boxes, it happens when
1239+
the reference count of the box reaches zero.
1240+
1241+
The order of fields in a class instance is significant; its runtime
1242+
representation is the same as that of a record with identical fields
1243+
laid out in the same order.
1244+
11011245
### Traits
11021246

11031247
A _trait item_ describes a set of method types. [_implementation
@@ -1204,7 +1348,7 @@ trait. The methods in such an implementation can only be used
12041348
statically (as direct calls on the values of the type that the
12051349
implementation targets). In such an implementation, the `of` clause is
12061350
not given, and the name is mandatory. Such implementations are
1207-
limited to nominal types (enums, structs) and the implementation must
1351+
limited to nominal types (enums, classes) and the implementation must
12081352
appear in the same module or a sub-module as the receiver type.
12091353

12101354
_When_ a trait is specified, all methods declared as part of the
@@ -2600,9 +2744,9 @@ fn main() {
26002744
In this example, the trait `printable` occurs as a type in both the type signature of
26012745
`print`, and the cast expression in `main`.
26022746

2603-
### Struct types
2747+
### Class types
26042748

2605-
Every struct item defines a type.
2749+
Every class item defines a type. See [classes](#classes).
26062750

26072751
### Type parameters
26082752

@@ -2622,7 +2766,7 @@ type `~[B]`, a vector type with element type `B`.
26222766

26232767
### Self type
26242768

2625-
The special type `self` has a meaning within methods inside an
2769+
The special type `self` has a meaning within methods inside a class or
26262770
impl item. It refers to the type of the implicit `self` argument. For
26272771
example, in:
26282772

@@ -2637,7 +2781,19 @@ impl ~str: printable {
26372781
~~~~~~
26382782

26392783
`self` refers to the value of type `str` that is the receiver for a
2640-
call to the method `to_str`.
2784+
call to the method `to_str`. Similarly, in a class declaration:
2785+
2786+
~~~~~~
2787+
class cat {
2788+
let mut meows: uint;
2789+
new() { self.meows = 0; }
2790+
fn meow() { self.meows = self.meows + 1; }
2791+
}
2792+
~~~~~~
2793+
2794+
`self` refers to the class instance that is the receiver of the method
2795+
(except in the constructor `new`, where `self` is the class instance
2796+
that the constructor implicitly returns).
26412797

26422798
## Type kinds
26432799

branches/incoming/doc/tutorial.md

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ if favorite_crayon_name.len() > 5 {
15601560

15611561
Named functions, like those we've seen so far, may not refer to local
15621562
variables declared outside the function - they do not "close over
1563-
their environment". For example, you couldn't write the following:
1563+
their environment". For example you couldn't write the following:
15641564

15651565
~~~~ {.ignore}
15661566
let foo = 10;
@@ -1821,6 +1821,85 @@ fn contains(v: ~[int], elt: int) -> bool {
18211821

18221822
`for` syntax only works with stack closures.
18231823

1824+
# Classes
1825+
1826+
Rust lets users define new types with fields and methods, called 'classes', in
1827+
the style of object-oriented languages.
1828+
1829+
> ***Warning:*** Rust's classes are in the process of changing rapidly. Some more
1830+
> information about some of the potential changes is [here][classchanges].
1831+
1832+
[classchanges]: http://pcwalton.github.com/blog/2012/06/03/maximally-minimal-classes-for-rust/
1833+
1834+
An example of a class:
1835+
1836+
~~~~
1837+
class example {
1838+
let mut x: int;
1839+
let y: int;
1840+
1841+
priv {
1842+
let mut private_member: int;
1843+
fn private_method() {}
1844+
}
1845+
1846+
new(x: int) {
1847+
// Constructor
1848+
self.x = x;
1849+
self.y = 7;
1850+
self.private_member = 8;
1851+
}
1852+
1853+
fn a() {
1854+
io::println(~"a");
1855+
}
1856+
1857+
drop {
1858+
// Destructor
1859+
self.x = 0;
1860+
}
1861+
}
1862+
1863+
fn main() {
1864+
let x: example = example(1);
1865+
let y: @example = @example(2);
1866+
x.a();
1867+
x.x = 5;
1868+
}
1869+
~~~~
1870+
1871+
Fields and methods are declared just like functions and local variables, using
1872+
'fn' and 'let'. As usual, 'let mut' can be used to create mutable fields. At
1873+
minimum, Rust classes must have at least one field.
1874+
1875+
Rust classes must also have a constructor, and can optionally have a destructor
1876+
as well. The constructor and destructor are declared as shown in the example:
1877+
like methods named 'new' and 'drop', but without 'fn', and without arguments
1878+
for drop.
1879+
1880+
In the constructor, the compiler will enforce that all fields are initialized
1881+
before doing anything that might allow them to be accessed. This includes
1882+
returning from the constructor, calling any method on 'self', calling any
1883+
function with 'self' as an argument, or taking a reference to 'self'. Mutation
1884+
of immutable fields is possible only in the constructor, and only before doing
1885+
any of these things; afterwards it is an error.
1886+
1887+
Private fields and methods are declared as shown above, using a `priv { ... }`
1888+
block within the class. They are accessible only from within the same instance
1889+
of the same class. (For example, even from within class A, you cannot call
1890+
private methods, or access private fields, on other instances of class A; only
1891+
on `self`.) This accessibility restriction may change in the future.
1892+
1893+
As mentioned below, in the section on copying types, classes with destructors
1894+
are considered 'resource' types and are not copyable.
1895+
1896+
Declaring a class also declares its constructor as a function of the same name.
1897+
You can construct an instance of the class, as in the example, by calling that
1898+
function. The function and the type, though they have the same name, are
1899+
otherwise independent. As with other Rust types, you can use `@` or `~` to
1900+
construct a heap-allocated instance of a class, either shared or unique; just
1901+
call e.g. `@example(...)` as shown above.
1902+
18241903
# Argument passing
18251904

18261905
Rust datatypes are not trivial to copy (the way, for example,
@@ -1859,13 +1938,6 @@ match my_rec {
18591938
}
18601939
~~~~
18611940

1862-
It's unsafe to dereference `b` in the second `log` expression, because `b` is
1863-
a _pointer_ to the inside of `my_rec`, and the assignment statement has
1864-
allocated a new record and assigned `my_rec` to point to it. Thus, the old
1865-
contents of `my_rec` are no longer live, and `b` is dangling at this point.
1866-
The borrow-checking analysis inside the compiler recognizes this situation
1867-
and rejects the program.
1868-
18691941
## Argument passing styles
18701942

18711943
The fact that arguments are conceptually passed by safe reference does

0 commit comments

Comments
 (0)