@@ -569,6 +569,20 @@ pub struct RefCell<T: ?Sized> {
569569 value : UnsafeCell < T > ,
570570}
571571
572+ /// An enumeration of values returned from the `state` method on a `RefCell<T>`.
573+ #[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
574+ #[ unstable( feature = "borrow_state" , issue = "27733" ) ]
575+ #[ rustc_deprecated( since = "1.15.0" , reason = "use `try_borrow` instead" ) ]
576+ #[ allow( deprecated) ]
577+ pub enum BorrowState {
578+ /// The cell is currently being read, there is at least one active `borrow`.
579+ Reading ,
580+ /// The cell is currently being written to, there is an active `borrow_mut`.
581+ Writing ,
582+ /// There are no outstanding borrows on this cell.
583+ Unused ,
584+ }
585+
572586/// An error returned by [`RefCell::try_borrow`](struct.RefCell.html#method.try_borrow).
573587#[ stable( feature = "try_borrow" , since = "1.13.0" ) ]
574588pub struct BorrowError {
@@ -753,6 +767,41 @@ impl<T> RefCell<T> {
753767}
754768
755769impl < T : ?Sized > RefCell < T > {
770+ /// Query the current state of this `RefCell`
771+ ///
772+ /// The returned value can be dispatched on to determine if a call to
773+ /// `borrow` or `borrow_mut` would succeed.
774+ ///
775+ /// # Examples
776+ ///
777+ /// ```
778+ /// #![feature(borrow_state)]
779+ ///
780+ /// use std::cell::{BorrowState, RefCell};
781+ ///
782+ /// let c = RefCell::new(5);
783+ ///
784+ /// match c.borrow_state() {
785+ /// BorrowState::Writing => println!("Cannot be borrowed"),
786+ /// BorrowState::Reading => println!("Cannot be borrowed mutably"),
787+ /// BorrowState::Unused => println!("Can be borrowed (mutably as well)"),
788+ /// }
789+ /// ```
790+ #[ unstable( feature = "borrow_state" , issue = "27733" ) ]
791+ #[ rustc_deprecated( since = "1.15.0" , reason = "use `try_borrow` instead" ) ]
792+ #[ allow( deprecated) ]
793+ #[ inline]
794+ pub fn borrow_state ( & self ) -> BorrowState {
795+ let borrow = self . borrow . get ( ) ;
796+ if is_writing ( borrow) {
797+ BorrowState :: Writing
798+ } else if is_reading ( borrow) {
799+ BorrowState :: Reading
800+ } else {
801+ BorrowState :: Unused
802+ }
803+ }
804+
756805 /// Immutably borrows the wrapped value.
757806 ///
758807 /// The borrow lasts until the returned `Ref` exits scope. Multiple
0 commit comments