about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-03-06 18:18:51 +0800
committerkennytm <kennytm@gmail.com>2018-03-06 20:52:35 +0800
commitee9425214bf6b71692c5d79b92ca4ec20fbdcd4a (patch)
treeb9e3ed2e4c247b2c06e0b55b5f58947330467804 /src/libcore
parentfa374f91a1f3ca7950e32a83ea61fae48338feb4 (diff)
parent9091584def3b566f24f8fbeac495e6dc87178be8 (diff)
downloadrust-ee9425214bf6b71692c5d79b92ca4ec20fbdcd4a.tar.gz
rust-ee9425214bf6b71692c5d79b92ca4ec20fbdcd4a.zip
Rollup merge of #48474 - christianpoveda:new_cell_docs, r=steveklabnik
New Cell docs

This fixes https://github.com/rust-lang/rust/issues/44061
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/cell.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs
index 419ae96b94b..1372151b753 100644
--- a/src/libcore/cell.rs
+++ b/src/libcore/cell.rs
@@ -10,6 +10,24 @@
 
 //! Shareable mutable containers.
 //!
+//! Rust memory safety is based on this rule: Given an object `T`, it is only possible to
+//! have one of the following:
+//!
+//! - Having several immutable references (`&T`) to the object (also known as **aliasing**).
+//! - Having one mutable reference (`&mut T`) to the object (also known as **mutability**).
+//!
+//! This is enforced by the Rust compiler. However, there are situations where this rule is not
+//! flexible enough. Sometimes it is required to have multiple references to an object and yet
+//! mutate it.
+//!
+//! Shareable mutable containers exist to permit mutability in a controlled manner, even in the
+//! presence of aliasing. Both `Cell<T>` and `RefCell<T>` allows to do this in a single threaded
+//! way. However, neither `Cell<T>` nor `RefCell<T>` are thread safe (they do not implement
+//! `Sync`). If you need to do aliasing and mutation between multiple threads it is possible to
+//! use [`Mutex`](../../std/sync/struct.Mutex.html),
+//! [`RwLock`](../../std/sync/struct.RwLock.html) or
+//! [`atomic`](../../core/sync/atomic/index.html) types.
+//!
 //! Values of the `Cell<T>` and `RefCell<T>` types may be mutated through shared references (i.e.
 //! the common `&T` type), whereas most Rust types can only be mutated through unique (`&mut T`)
 //! references. We say that `Cell<T>` and `RefCell<T>` provide 'interior mutability', in contrast