Skip to content

Commit 8d0be73

Browse files
committed
doc: Update the tutorial about bounds for traits
1 parent bb9172d commit 8d0be73

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

src/doc/tutorial.md

+17-15
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,7 @@ a `&T` pointer. `MutexArc` is an example of a *sharable* type with internal muta
21032103
These are types that do not contain any data whose lifetime is bound to
21042104
a particular stack frame. These are types that do not contain any
21052105
references, or types where the only contained references
2106-
have the `'static` lifetime. (For more on named lifetimes and their uses,
2106+
have the `'static` lifetime. (For more on named lifetimes and their uses,
21072107
see the [references and lifetimes guide][lifetimes].)
21082108
21092109
> ***Note:*** These two traits were referred to as 'kinds' in earlier
@@ -2430,23 +2430,25 @@ select the method to call at runtime.
24302430
24312431
This usage of traits is similar to Java interfaces.
24322432
2433-
By default, each of the three storage classes for traits enforce a
2434-
particular set of built-in kinds that their contents must fulfill in
2435-
order to be packaged up in a trait object of that storage class.
2433+
There are some built-in bounds, such as `Send` and `Share`, which are properties
2434+
of the components of types. By design, trait objects don't know the exact type
2435+
of their contents and so the compiler cannot reason about those properties.
24362436
2437-
* The contents of owned traits (`~Trait`) must fulfill the `Send` bound.
2438-
* The contents of reference traits (`&Trait`) are not constrained by any bound.
2437+
You can instruct the compiler, however, that the contents of a trait object must
2438+
acribe to a particular bound with a trailing colon (`:`). These are examples of
2439+
valid types:
24392440
2440-
Consequently, the trait objects themselves automatically fulfill their
2441-
respective kind bounds. However, this default behavior can be overridden by
2442-
specifying a list of bounds on the trait type, for example, by writing `~Trait:`
2443-
(which indicates that the contents of the owned trait need not fulfill any
2444-
bounds), or by writing `~Trait:Send+Share`, which indicates that in addition
2445-
to fulfilling `Send`, contents must also fulfill `Share`, and as a consequence,
2446-
the trait itself fulfills `Share`.
2441+
~~~rust
2442+
trait Foo {}
2443+
trait Bar<T> {}
24472444
2448-
* `~Trait:Send` is equivalent to `~Trait`.
2449-
* `&Trait:` is equivalent to `&Trait`.
2445+
fn sendable_foo(f: ~Foo:Send) { /* ... */ }
2446+
fn shareable_bar<T: Share>(b: &Bar<T>: Share) { /* ... */ }
2447+
~~~
2448+
2449+
When no colon is specified (such as the type `~Foo`), it is inferred that the
2450+
value ascribes to no bounds. They must be added manually if any bounds are
2451+
necessary for usage.
24502452

24512453
Builtin kind bounds can also be specified on closure types in the same way (for
24522454
example, by writing `fn:Send()`), and the default behaviours are the same as

src/libnative/io/file_win32.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ impl rtio::RtioPipe for FileDesc {
206206
fn write(&mut self, buf: &[u8]) -> Result<(), IoError> {
207207
self.inner_write(buf)
208208
}
209-
fn clone(&self) -> ~rtio::RtioPipe {
210-
~FileDesc { inner: self.inner.clone() } as ~rtio::RtioPipe
209+
fn clone(&self) -> ~rtio::RtioPipe:Send {
210+
~FileDesc { inner: self.inner.clone() } as ~rtio::RtioPipe:Send
211211
}
212212
}
213213

src/libnative/io/pipe_win32.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,12 @@ impl rtio::RtioPipe for UnixStream {
335335
Ok(())
336336
}
337337

338-
fn clone(&self) -> ~rtio::RtioPipe {
338+
fn clone(&self) -> ~rtio::RtioPipe:Send {
339339
~UnixStream {
340340
inner: self.inner.clone(),
341341
read: None,
342342
write: None,
343-
} as ~rtio::RtioPipe
343+
} as ~rtio::RtioPipe:Send
344344
}
345345
}
346346

@@ -383,8 +383,8 @@ impl Drop for UnixListener {
383383
}
384384

385385
impl rtio::RtioUnixListener for UnixListener {
386-
fn listen(~self) -> IoResult<~rtio::RtioUnixAcceptor> {
387-
self.native_listen().map(|a| ~a as ~rtio::RtioUnixAcceptor)
386+
fn listen(~self) -> IoResult<~rtio::RtioUnixAcceptor:Send> {
387+
self.native_listen().map(|a| ~a as ~rtio::RtioUnixAcceptor:Send)
388388
}
389389
}
390390

@@ -485,8 +485,8 @@ impl UnixAcceptor {
485485
}
486486

487487
impl rtio::RtioUnixAcceptor for UnixAcceptor {
488-
fn accept(&mut self) -> IoResult<~rtio::RtioPipe> {
489-
self.native_accept().map(|s| ~s as ~rtio::RtioPipe)
488+
fn accept(&mut self) -> IoResult<~rtio::RtioPipe:Send> {
489+
self.native_accept().map(|s| ~s as ~rtio::RtioPipe:Send)
490490
}
491491
}
492492

0 commit comments

Comments
 (0)