diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2016-07-12 12:08:22 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-07-12 12:08:22 +0200 |
| commit | 9f4d2b6cd91b32ead690d7e02e1e64fb8896fc15 (patch) | |
| tree | f75794233982e03e8cf5fe3719461b7fb68efa25 /src/libcore | |
| parent | 2539c15b49530e882a3e8803b3f53a7914d367be (diff) | |
| parent | 76dd02025ce0082c468eff79f015267843ebbec5 (diff) | |
| download | rust-9f4d2b6cd91b32ead690d7e02e1e64fb8896fc15.tar.gz rust-9f4d2b6cd91b32ead690d7e02e1e64fb8896fc15.zip | |
Rollup merge of #34736 - GuillaumeGomez:cells_doc, r=steveklabnik
Add missing examples for std::cell types Fixes #29344. r? @steveklabnik
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/cell.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 9435be3b012..65cb1aaaff6 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -237,6 +237,17 @@ impl<T:Copy> Cell<T> { /// /// This call borrows `Cell` mutably (at compile-time) which guarantees /// that we possess the only reference. + /// + /// # Examples + /// + /// ``` + /// use std::cell::Cell; + /// + /// let mut c = Cell::new(5); + /// *c.get_mut() += 1; + /// + /// assert_eq!(c.get(), 6); + /// ``` #[inline] #[stable(feature = "cell_get_mut", since = "1.11.0")] pub fn get_mut(&mut self) -> &mut T { @@ -388,6 +399,22 @@ impl<T: ?Sized> RefCell<T> { /// /// The returned value can be dispatched on to determine if a call to /// `borrow` or `borrow_mut` would succeed. + /// + /// # Examples + /// + /// ``` + /// #![feature(borrow_state)] + /// + /// use std::cell::{BorrowState, RefCell}; + /// + /// let c = RefCell::new(5); + /// + /// match c.borrow_state() { + /// BorrowState::Writing => println!("Cannot be borrowed"), + /// BorrowState::Reading => println!("Cannot be borrowed mutably"), + /// BorrowState::Unused => println!("Can be borrowed (mutably as well)"), + /// } + /// ``` #[unstable(feature = "borrow_state", issue = "27733")] #[inline] pub fn borrow_state(&self) -> BorrowState { @@ -498,6 +525,17 @@ impl<T: ?Sized> RefCell<T> { /// This can be used to circumvent `RefCell`'s safety checks. /// /// This function is `unsafe` because `UnsafeCell`'s field is public. + /// + /// # Examples + /// + /// ``` + /// #![feature(as_unsafe_cell)] + /// + /// use std::cell::RefCell; + /// + /// let c = RefCell::new(5); + /// let c = unsafe { c.as_unsafe_cell() }; + /// ``` #[inline] #[unstable(feature = "as_unsafe_cell", issue = "27708")] pub unsafe fn as_unsafe_cell(&self) -> &UnsafeCell<T> { @@ -508,6 +546,17 @@ impl<T: ?Sized> RefCell<T> { /// /// This call borrows `RefCell` mutably (at compile-time) so there is no /// need for dynamic checks. + /// + /// # Examples + /// + /// ``` + /// use std::cell::RefCell; + /// + /// let mut c = RefCell::new(5); + /// *c.get_mut() += 1; + /// + /// assert_eq!(c, RefCell::new(6)); + /// ``` #[inline] #[stable(feature = "cell_get_mut", since = "1.11.0")] pub fn get_mut(&mut self) -> &mut T { |
