Skip to content

Commit 76dd020

Browse files
Add missing examples for std::cell types
1 parent 4114b68 commit 76dd020

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/libcore/cell.rs

+49
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ impl<T:Copy> Cell<T> {
237237
///
238238
/// This call borrows `Cell` mutably (at compile-time) which guarantees
239239
/// that we possess the only reference.
240+
///
241+
/// # Examples
242+
///
243+
/// ```
244+
/// use std::cell::Cell;
245+
///
246+
/// let mut c = Cell::new(5);
247+
/// *c.get_mut() += 1;
248+
///
249+
/// assert_eq!(c.get(), 6);
250+
/// ```
240251
#[inline]
241252
#[stable(feature = "cell_get_mut", since = "1.11.0")]
242253
pub fn get_mut(&mut self) -> &mut T {
@@ -388,6 +399,22 @@ impl<T: ?Sized> RefCell<T> {
388399
///
389400
/// The returned value can be dispatched on to determine if a call to
390401
/// `borrow` or `borrow_mut` would succeed.
402+
///
403+
/// # Examples
404+
///
405+
/// ```
406+
/// #![feature(borrow_state)]
407+
///
408+
/// use std::cell::{BorrowState, RefCell};
409+
///
410+
/// let c = RefCell::new(5);
411+
///
412+
/// match c.borrow_state() {
413+
/// BorrowState::Writing => println!("Cannot be borrowed"),
414+
/// BorrowState::Reading => println!("Cannot be borrowed mutably"),
415+
/// BorrowState::Unused => println!("Can be borrowed (mutably as well)"),
416+
/// }
417+
/// ```
391418
#[unstable(feature = "borrow_state", issue = "27733")]
392419
#[inline]
393420
pub fn borrow_state(&self) -> BorrowState {
@@ -498,6 +525,17 @@ impl<T: ?Sized> RefCell<T> {
498525
/// This can be used to circumvent `RefCell`'s safety checks.
499526
///
500527
/// This function is `unsafe` because `UnsafeCell`'s field is public.
528+
///
529+
/// # Examples
530+
///
531+
/// ```
532+
/// #![feature(as_unsafe_cell)]
533+
///
534+
/// use std::cell::RefCell;
535+
///
536+
/// let c = RefCell::new(5);
537+
/// let c = unsafe { c.as_unsafe_cell() };
538+
/// ```
501539
#[inline]
502540
#[unstable(feature = "as_unsafe_cell", issue = "27708")]
503541
pub unsafe fn as_unsafe_cell(&self) -> &UnsafeCell<T> {
@@ -508,6 +546,17 @@ impl<T: ?Sized> RefCell<T> {
508546
///
509547
/// This call borrows `RefCell` mutably (at compile-time) so there is no
510548
/// need for dynamic checks.
549+
///
550+
/// # Examples
551+
///
552+
/// ```
553+
/// use std::cell::RefCell;
554+
///
555+
/// let mut c = RefCell::new(5);
556+
/// *c.get_mut() += 1;
557+
///
558+
/// assert_eq!(c, RefCell::new(6));
559+
/// ```
511560
#[inline]
512561
#[stable(feature = "cell_get_mut", since = "1.11.0")]
513562
pub fn get_mut(&mut self) -> &mut T {

0 commit comments

Comments
 (0)