Skip to content

Commit 3ab4b01

Browse files
committed
Remove the class keyword
1 parent 6d7b143 commit 3ab4b01

File tree

143 files changed

+185
-421
lines changed

Some content is hidden

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

143 files changed

+185
-421
lines changed

doc/rust.md

Lines changed: 7 additions & 163 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 class const copy
214+
check const copy
215215
drop
216216
else enum export extern
217217
fail false fn for
@@ -220,6 +220,7 @@ let log loop
220220
match mod mut
221221
pure
222222
return
223+
struct
223224
true trait type
224225
unchecked unsafe
225226
while
@@ -1097,151 +1098,6 @@ enum list<T> {
10971098
let a: list<int> = cons(7, @cons(13, @nil));
10981099
~~~~
10991100

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-
12451101
### Traits
12461102

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

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

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

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

27512607
### Type parameters
27522608

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

27672623
### Self type
27682624

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

@@ -2781,19 +2637,7 @@ impl ~str: printable {
27812637
~~~~~~
27822638

27832639
`self` refers to the value of type `str` that is the receiver for a
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).
2640+
call to the method `to_str`.
27972641

27982642
## Type kinds
27992643

doc/tutorial.md

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,85 +1821,6 @@ 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-
19031824
# Argument passing
19041825

19051826
Rust datatypes are not trivial to copy (the way, for example,

src/libcore/comm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ fn listen<T: send, U>(f: fn(Chan<T>) -> U) -> U {
9797
f(po.chan())
9898
}
9999

100-
class PortPtr<T:send> {
100+
struct PortPtr<T:send> {
101101
let po: *rust_port;
102102
new(po: *rust_port) { self.po = po; }
103103
drop unsafe {
@@ -132,7 +132,7 @@ class PortPtr<T:send> {
132132
*/
133133
fn as_raw_port<T: send, U>(ch: comm::Chan<T>, f: fn(*rust_port) -> U) -> U {
134134

135-
class PortRef {
135+
struct PortRef {
136136
let p: *rust_port;
137137
new(p: *rust_port) { self.p = p; }
138138
drop {

src/libcore/io.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl<T: Reader, C> {base: T, cleanup: C}: Reader {
237237
fn tell() -> uint { self.base.tell() }
238238
}
239239

240-
class FILERes {
240+
struct FILERes {
241241
let f: *libc::FILE;
242242
new(f: *libc::FILE) { self.f = f; }
243243
drop { libc::fclose(self.f); }
@@ -415,7 +415,7 @@ impl fd_t: Writer {
415415
}
416416
}
417417

418-
class FdRes {
418+
struct FdRes {
419419
let fd: fd_t;
420420
new(fd: fd_t) { self.fd = fd; }
421421
drop { libc::close(self.fd); }
@@ -764,7 +764,7 @@ mod fsync {
764764

765765

766766
// Artifacts that need to fsync on destruction
767-
class Res<t> {
767+
struct Res<t> {
768768
let arg: Arg<t>;
769769
new(-arg: Arg<t>) { self.arg <- arg; }
770770
drop {

src/libcore/option.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ fn test_unwrap_str() {
264264

265265
#[test]
266266
fn test_unwrap_resource() {
267-
class r {
267+
struct r {
268268
let i: @mut int;
269269
new(i: @mut int) { self.i = i; }
270270
drop { *(self.i) += 1; }

src/libcore/priv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ unsafe fn weaken_task(f: fn(comm::Port<()>)) {
194194
let _unweaken = Unweaken(ch);
195195
f(po);
196196

197-
class Unweaken {
197+
struct Unweaken {
198198
let ch: comm::Chan<()>;
199199
new(ch: comm::Chan<()>) { self.ch = ch; }
200200
drop unsafe {

src/libcore/rand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl Rng {
243243

244244
}
245245

246-
class RandRes {
246+
struct RandRes {
247247
let c: *rctx;
248248
new(c: *rctx) { self.c = c; }
249249
drop { rustrt::rand_free(self.c); }

src/libcore/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn start_program(prog: &str, args: &[~str]) -> Program {
225225
libc::fclose(r.out_file);
226226
libc::fclose(r.err_file);
227227
}
228-
class ProgRes {
228+
struct ProgRes {
229229
let r: ProgRepr;
230230
new(+r: ProgRepr) { self.r = r; }
231231
drop { destroy_repr(&self.r); }

src/libcore/stackwalk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import sys::size_of;
66

77
type Word = uint;
88

9-
class Frame {
9+
struct Frame {
1010
let fp: *Word;
1111

1212
new(fp: *Word) {

0 commit comments

Comments
 (0)