Skip to content

Commit a74447f

Browse files
committed
---
yaml --- r: 31222 b: refs/heads/dist-snap c: 7eae204 h: refs/heads/master v: v3
1 parent 38bdb4a commit a74447f

File tree

2 files changed

+131
-21
lines changed

2 files changed

+131
-21
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: 6081eb78f973119967fa659d5348c041783825c5
10+
refs/heads/dist-snap: 7eae2044b0f4bbb586cd48993d9a4d1853028cc7
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/dist-snap/doc/rust.md

Lines changed: 130 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,11 +1106,12 @@ let a: list<int> = cons(7, @cons(13, @nil));
11061106

11071107
### Classes
11081108

1109-
TODO: more about classes
1110-
1111-
_Classes_ are named record types that may have a destructor associated
1112-
with them, as well as fields and methods. For historical reasons, we
1113-
may call a class with a destructor and a single field a "resource".
1109+
A _class_ is a named record type that collects together fields and
1110+
methods. It must have a _constructor_ (a function called `new` that
1111+
returns a new instance of the class), and may have a destructor (a
1112+
nullary function called `drop` that executes before the memory manager
1113+
frees the memory for a given class). For historical reasons, we may
1114+
call a class with a destructor and a single field a "resource".
11141115

11151116
A _class item_ declares a class type:
11161117

@@ -1123,21 +1124,130 @@ class file_descriptor {
11231124
~~~~
11241125

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

11421252
### Interfaces
11431253

0 commit comments

Comments
 (0)