Skip to content

Commit 4dff9cb

Browse files
committed
auto merge of #14304 : brson/rust/moredocs, r=kballard
Includes module docs for `cell`.
2 parents f9bd6b4 + c9ab33a commit 4dff9cb

File tree

4 files changed

+182
-20
lines changed

4 files changed

+182
-20
lines changed

src/libcore/cell.rs

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,157 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! Types that provide interior mutability.
11+
//! Sharable mutable containers.
12+
//!
13+
//! Values of the `Cell` and `RefCell` types may be mutated through
14+
//! shared references (i.e. the common `&T` type), whereas most Rust
15+
//! types can only be mutated through unique (`&mut T`) references. We
16+
//! say that `Cell` and `RefCell` provide *interior mutability*, in
17+
//! contrast with typical Rust types that exhibit *inherited
18+
//! mutability*.
19+
//!
20+
//! Cell types come in two flavors: `Cell` and `RefCell`. `Cell`
21+
//! provides `get` and `set` methods that change the
22+
//! interior value with a single method call. `Cell` though is only
23+
//! compatible with types that implement `Copy`. For other types,
24+
//! one must use the `RefCell` type, acquiring a write lock before
25+
//! mutating.
26+
//!
27+
//! `RefCell` uses Rust's lifetimes to implement *dynamic borrowing*,
28+
//! a process whereby one can claim temporary, exclusive, mutable
29+
//! access to the inner value. Borrows for `RefCell`s are tracked *at
30+
//! runtime*, unlike Rust's native reference types which are entirely
31+
//! tracked statically, at compile time. Because `RefCell` borrows are
32+
//! dynamic it is possible to attempt to borrow a value that is
33+
//! already mutably borrowed; when this happens it results in task
34+
//! failure.
35+
//!
36+
//! # When to choose interior mutability
37+
//!
38+
//! The more common inherited mutability, where one must have unique
39+
//! access to mutate a value, is one of the key language elements that
40+
//! enables Rust to reason strongly about pointer aliasing, statically
41+
//! preventing crash bugs. Because of that, inherited mutability is
42+
//! preferred, and interior mutability is something of a last
43+
//! resort. Since cell types enable mutation where it would otherwise
44+
//! be disallowed though, there are occassions when interior
45+
//! mutability might be appropriate, or even *must* be used, e.g.
46+
//!
47+
//! * Introducing inherited mutability roots to shared types.
48+
//! * Implementation details of logically-immutable methods.
49+
//! * Mutating implementations of `clone`.
50+
//!
51+
//! ## Introducing inherited mutability roots to shared types
52+
//!
53+
//! Shared smart pointer types, including `Rc` and `Arc`, provide
54+
//! containers that can be cloned and shared between multiple parties.
55+
//! Because the contained values may be multiply-aliased, they can
56+
//! only be borrowed as shared references, not mutable references.
57+
//! Without cells it would be impossible to mutate data inside of
58+
//! shared boxes at all!
59+
//!
60+
//! It's very common then to put a `RefCell` inside shared pointer
61+
//! types to reintroduce mutability:
62+
//!
63+
//! ```
64+
//! extern crate collections;
65+
//!
66+
//! use collections::HashMap;
67+
//! use std::cell::RefCell;
68+
//! use std::rc::Rc;
69+
//!
70+
//! fn main() {
71+
//! let shared_map: Rc<RefCell<_>> = Rc::new(RefCell::new(HashMap::new()));
72+
//! shared_map.borrow_mut().insert("africa", 92388);
73+
//! shared_map.borrow_mut().insert("kyoto", 11837);
74+
//! shared_map.borrow_mut().insert("piccadilly", 11826);
75+
//! shared_map.borrow_mut().insert("marbles", 38);
76+
//! }
77+
//! ```
78+
//!
79+
//! ## Implementation details of logically-immutable methods
80+
//!
81+
//! Occasionally it may be desirable not to expose in an API that
82+
//! there is mutation happening "under the hood". This may be because
83+
//! logically the operation is immutable, but e.g. caching forces the
84+
//! implementation to perform mutation; or because you must employ
85+
//! mutation to implement a trait method that was originally defined
86+
//! to take `&self`.
87+
//!
88+
//! ```
89+
//! extern crate collections;
90+
//!
91+
//! use collections::HashMap;
92+
//! use std::cell::RefCell;
93+
//!
94+
//! struct Graph {
95+
//! edges: HashMap<uint, uint>,
96+
//! span_tree_cache: RefCell<Option<Vec<(uint, uint)>>>
97+
//! }
98+
//!
99+
//! impl Graph {
100+
//! fn minimum_spanning_tree(&self) -> Vec<(uint, uint)> {
101+
//! // Create a new scope to contain the lifetime of the
102+
//! // dynamic borrow
103+
//! {
104+
//! // Take a reference to the inside of cache cell
105+
//! let mut cache = self.span_tree_cache.borrow_mut();
106+
//! if cache.is_some() {
107+
//! return cache.get_ref().clone();
108+
//! }
109+
//!
110+
//! let span_tree = self.calc_span_tree();
111+
//! *cache = Some(span_tree);
112+
//! }
113+
//!
114+
//! // Recursive call to return the just-cached value.
115+
//! // Note that if we had not let the previous borrow
116+
//! // of the cache fall out of scope then the subsequent
117+
//! // recursive borrow would cause a dynamic task failure.
118+
//! // This is the major hazard of using `RefCell`.
119+
//! self.minimum_spanning_tree()
120+
//! }
121+
//! # fn calc_span_tree(&self) -> Vec<(uint, uint)> { vec![] }
122+
//! }
123+
//! # fn main() { }
124+
//! ```
125+
//!
126+
//! ## Mutating implementations of `clone`
127+
//!
128+
//! This is simply a special - but common - case of the previous:
129+
//! hiding mutability for operations that appear to be immutable.
130+
//! The `clone` method is expected to not change the source value, and
131+
//! is declared to take `&self`, not `&mut self`. Therefore any
132+
//! mutation that happens in the `clone` method must use cell
133+
//! types. For example, `Rc` maintains its reference counts within a
134+
//! `Cell`.
135+
//!
136+
//! ```
137+
//! use std::cell::Cell;
138+
//!
139+
//! struct Rc<T> {
140+
//! ptr: *mut RcBox<T>
141+
//! }
142+
//!
143+
//! struct RcBox<T> {
144+
//! value: T,
145+
//! refcount: Cell<uint>
146+
//! }
147+
//!
148+
//! impl<T> Clone for Rc<T> {
149+
//! fn clone(&self) -> Rc<T> {
150+
//! unsafe {
151+
//! (*self.ptr).refcount.set((*self.ptr).refcount.get() + 1);
152+
//! Rc { ptr: self.ptr }
153+
//! }
154+
//! }
155+
//! }
156+
//! ```
157+
//!
158+
// FIXME: Explain difference between Cell and RefCell
159+
// FIXME: Downsides to interior mutability
160+
// FIXME: Can't be shared between threads. Dynamic borrows
161+
// FIXME: Relationship to Atomic types and RWLock
12162

13163
use clone::Clone;
14164
use cmp::Eq;

src/libcore/lib.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,28 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! The Rust core library
11+
//! The Rust Core Library
1212
//!
13-
//! This library is meant to represent the core functionality of rust that is
14-
//! maximally portable to other platforms. To that extent, this library has no
15-
//! knowledge of things like allocation, threads, I/O, etc. This library is
16-
//! built on the assumption of a few existing symbols:
13+
//! The Rust Core Library is the dependency-free foundation of [The
14+
//! Rust Standard Library](../std/index.html). It is the portable glue
15+
//! between the language and its libraries, defining the intrinsic and
16+
//! primitive building blocks of all Rust code. It links to no
17+
//! upstream libraries, no system libraries, and no libc.
18+
//!
19+
//! The core library is *minimal*: it isn't even aware of heap allocation,
20+
//! nor does it provide concurrency or I/O. These things require
21+
//! platform integration, and this library is platform-agnostic.
22+
//!
23+
//! *It is not recommended to use the core library*. The stable
24+
//! functionality of libcore is reexported from the
25+
//! [standard library](../std/index.html). The composition of this library is
26+
//! subject to change over time; only the interface exposed through libstd is
27+
//! intended to be stable.
28+
//!
29+
//! # How to use the core library
30+
//!
31+
// FIXME: Fill me in with more detail when the interface settles
32+
//! This library is built on the assumption of a few existing symbols:
1733
//!
1834
//! * `memcpy`, `memcmp`, `memset` - These are core memory routines which are
1935
//! often generated by LLVM. Additionally, this library can make explicit
@@ -23,16 +39,11 @@
2339
//! distribution.
2440
//!
2541
//! * `rust_begin_unwind` - This function takes three arguments, a
26-
//! `&fmt::Arguments`, a `&str`, and a `uint. These three arguments dictate
42+
//! `&fmt::Arguments`, a `&str`, and a `uint`. These three arguments dictate
2743
//! the failure message, the file at which failure was invoked, and the line.
2844
//! It is up to consumers of this core library to define this failure
2945
//! function; it is only required to never return.
3046
//!
31-
//! Currently, it is *not* recommended to use the core library. The stable
32-
//! functionality of libcore is exported directly into the
33-
//! [standard library](../std/index.html). The composition of this library is
34-
//! subject to change over time, only the interface exposed through libstd is
35-
//! intended to be stable.
3647
3748
#![crate_id = "core#0.11.0-pre"]
3849
#![license = "MIT/ASL2"]

src/libcore/ops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
/*!
1212
*
13-
* Traits representing built-in operators, useful for overloading
13+
* Overloadable operators
1414
*
1515
* Implementing these traits allows you to get an effect similar to
1616
* overloading operators.

src/libstd/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
//!
1717
//! ## Intrinsic types and operations
1818
//!
19-
//! The [`ptr`](../core/ptr/index.html), [`mem`](../core/mem/index.html),
20-
//! and [`cast`](../core/cast/index.html) modules deal with unsafe pointers,
21-
//! memory manipulation, and coercion.
19+
//! The [`ptr`](../core/ptr/index.html) and [`mem`](../core/mem/index.html)
20+
//! modules deal with unsafe pointers and memory manipulation.
2221
//! [`kinds`](../core/kinds/index.html) defines the special built-in traits,
2322
//! and [`raw`](../core/raw/index.html) the runtime representation of Rust types.
2423
//! These are some of the lowest-level building blocks of Rust
@@ -135,24 +134,26 @@ extern crate libc;
135134
#[cfg(test)] pub use realstd::cmp;
136135
#[cfg(test)] pub use realstd::ty;
137136

138-
#[cfg(not(test))] pub use core::cmp;
139-
#[cfg(not(test))] pub use core::kinds;
140-
#[cfg(not(test))] pub use core::ops;
141-
#[cfg(not(test))] pub use core::ty;
137+
138+
// NB: These reexports are in the order they should be listed in rustdoc
142139

143140
pub use core::any;
144141
pub use core::bool;
145142
pub use core::cell;
146143
pub use core::char;
147144
pub use core::clone;
145+
#[cfg(not(test))] pub use core::cmp;
148146
pub use core::container;
149147
pub use core::default;
150148
pub use core::intrinsics;
151149
pub use core::iter;
150+
#[cfg(not(test))] pub use core::kinds;
152151
pub use core::mem;
152+
#[cfg(not(test))] pub use core::ops;
153153
pub use core::ptr;
154154
pub use core::raw;
155155
pub use core::tuple;
156+
#[cfg(not(test))] pub use core::ty;
156157
pub use core::result;
157158

158159
pub use alloc::owned;

0 commit comments

Comments
 (0)