|
10 | 10 |
|
11 | 11 | //! Thread-local reference-counted boxes (the `Rc<T>` type).
|
12 | 12 | //!
|
13 |
| -//! The `Rc<T>` type provides shared ownership of an immutable value. Destruction is deterministic, |
14 |
| -//! and will occur as soon as the last owner is gone. It is marked as non-sendable because it |
15 |
| -//! avoids the overhead of atomic reference counting. |
| 13 | +//! The `Rc<T>` type provides shared ownership of an immutable value. |
| 14 | +//! Destruction is deterministic, and will occur as soon as the last owner is |
| 15 | +//! gone. It is marked as non-sendable because it avoids the overhead of atomic |
| 16 | +//! reference counting. |
16 | 17 | //!
|
17 |
| -//! The `downgrade` method can be used to create a non-owning `Weak<T>` pointer to the box. A |
18 |
| -//! `Weak<T>` pointer can be upgraded to an `Rc<T>` pointer, but will return `None` if the value |
19 |
| -//! has already been dropped. |
| 18 | +//! The `downgrade` method can be used to create a non-owning `Weak<T>` pointer |
| 19 | +//! to the box. A `Weak<T>` pointer can be upgraded to an `Rc<T>` pointer, but |
| 20 | +//! will return `None` if the value has already been dropped. |
20 | 21 | //!
|
21 |
| -//! For example, a tree with parent pointers can be represented by putting the nodes behind strong |
22 |
| -//! `Rc<T>` pointers, and then storing the parent pointers as `Weak<T>` pointers. |
| 22 | +//! For example, a tree with parent pointers can be represented by putting the |
| 23 | +//! nodes behind strong `Rc<T>` pointers, and then storing the parent pointers |
| 24 | +//! as `Weak<T>` pointers. |
23 | 25 | //!
|
24 | 26 | //! # Examples
|
25 | 27 | //!
|
26 |
| -//! Consider a scenario where a set of `Gadget`s are owned by a given `Owner`. We want to have our |
27 |
| -//! `Gadget`s point to their `Owner`. We can't do this with unique ownership, because more than one |
28 |
| -//! gadget may belong to the same `Owner`. `Rc<T>` allows us to share an `Owner` between multiple |
29 |
| -//! `Gadget`s, and have the `Owner` remain allocated as long as any `Gadget` points at it. |
| 28 | +//! Consider a scenario where a set of `Gadget`s are owned by a given `Owner`. |
| 29 | +//! We want to have our `Gadget`s point to their `Owner`. We can't do this with |
| 30 | +//! unique ownership, because more than one gadget may belong to the same |
| 31 | +//! `Owner`. `Rc<T>` allows us to share an `Owner` between multiple `Gadget`s, |
| 32 | +//! and have the `Owner` remain allocated as long as any `Gadget` points at it. |
30 | 33 | //!
|
31 | 34 | //! ```rust
|
32 | 35 | //! use std::rc::Rc;
|
@@ -597,12 +600,20 @@ impl<T: Ord> Ord for Rc<T> {
|
597 | 600 | }
|
598 | 601 |
|
599 | 602 | // FIXME (#18248) Make `T` `Sized?`
|
| 603 | +#[cfg(stage0)] |
600 | 604 | impl<S: hash::Writer, T: Hash<S>> Hash<S> for Rc<T> {
|
601 | 605 | #[inline]
|
602 | 606 | fn hash(&self, state: &mut S) {
|
603 | 607 | (**self).hash(state);
|
604 | 608 | }
|
605 | 609 | }
|
| 610 | +#[cfg(not(stage0))] |
| 611 | +impl<S: hash::Hasher, T: Hash<S>> Hash<S> for Rc<T> { |
| 612 | + #[inline] |
| 613 | + fn hash(&self, state: &mut S) { |
| 614 | + (**self).hash(state); |
| 615 | + } |
| 616 | +} |
606 | 617 |
|
607 | 618 | #[experimental = "Show is experimental."]
|
608 | 619 | impl<T: fmt::Show> fmt::Show for Rc<T> {
|
|
0 commit comments