@@ -569,18 +569,6 @@ 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- pub enum BorrowState {
576- /// The cell is currently being read, there is at least one active `borrow`.
577- Reading ,
578- /// The cell is currently being written to, there is an active `borrow_mut`.
579- Writing ,
580- /// There are no outstanding borrows on this cell.
581- Unused ,
582- }
583-
584572/// An error returned by [`RefCell::try_borrow`](struct.RefCell.html#method.try_borrow).
585573#[ stable( feature = "try_borrow" , since = "1.13.0" ) ]
586574pub struct BorrowError {
@@ -765,43 +753,6 @@ impl<T> RefCell<T> {
765753}
766754
767755impl < T : ?Sized > RefCell < T > {
768- /// Queries the current state of this `RefCell`.
769- ///
770- /// A return value of `BorrowState::Writing` signals that this `RefCell`
771- /// is currently mutably borrowed, while `BorrowState::Reading` signals
772- /// that it is immutably borrowed.
773- ///
774- /// This is mostly useful in rare use cases with `RefCell::as_ptr` to
775- /// access the data without changing its borrow state, use with care.
776- ///
777- /// # Examples
778- ///
779- /// ```
780- /// #![feature(borrow_state)]
781- ///
782- /// use std::cell::{BorrowState, RefCell};
783- ///
784- /// let c = RefCell::new(5);
785- ///
786- /// match c.borrow_state() {
787- /// BorrowState::Writing => println!("currently borrowed mutably"),
788- /// BorrowState::Reading => println!("currently borrowed immutably"),
789- /// BorrowState::Unused => println!("not borrowed"),
790- /// }
791- /// ```
792- #[ unstable( feature = "borrow_state" , issue = "27733" ) ]
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-
805756 /// Immutably borrows the wrapped value.
806757 ///
807758 /// The borrow lasts until the returned `Ref` exits scope. Multiple
@@ -1007,6 +958,44 @@ impl<T: ?Sized> RefCell<T> {
1007958 & mut * self . value . get ( )
1008959 }
1009960 }
961+
962+ /// Immutably borrows the wrapped value, returning an error if the value is
963+ /// currently mutably borrowed.
964+ ///
965+ /// # Safety
966+ ///
967+ /// Unlike `RefCell::borrow`, this method is unsafe because it does not
968+ /// return a `Ref`, thus leaving the borrow flag untouched. Mutably
969+ /// borrowing the `RefCell` while the reference returned by this method
970+ /// is alive is undefined behaviour.
971+ ///
972+ /// # Examples
973+ ///
974+ /// ```
975+ /// #![feature(borrow_state)]
976+ /// use std::cell::RefCell;
977+ ///
978+ /// let c = RefCell::new(5);
979+ ///
980+ /// {
981+ /// let m = c.borrow_mut();
982+ /// assert!(unsafe { c.try_borrow_unguarded() }.is_err());
983+ /// }
984+ ///
985+ /// {
986+ /// let m = c.borrow();
987+ /// assert!(unsafe { c.try_borrow_unguarded() }.is_ok());
988+ /// }
989+ /// ```
990+ #[ unstable( feature = "borrow_state" , issue = "27733" ) ]
991+ #[ inline]
992+ pub unsafe fn try_borrow_unguarded ( & self ) -> Result < & T , BorrowError > {
993+ if !is_writing ( self . borrow . get ( ) ) {
994+ Ok ( & * self . value . get ( ) )
995+ } else {
996+ Err ( BorrowError { _private : ( ) } )
997+ }
998+ }
1010999}
10111000
10121001#[ stable( feature = "rust1" , since = "1.0.0" ) ]
0 commit comments