@@ -570,20 +570,19 @@ impl Display for BorrowMutError {
570
570
}
571
571
}
572
572
573
- // Values [1, MIN_WRITING-1] represent the number of `Ref` active. Values in
574
- // [MIN_WRITING, MAX-1] represent the number of `RefMut` active. Multiple
575
- // `RefMut`s can only be active at a time if they refer to distinct,
576
- // nonoverlapping components of a `RefCell` (e.g., different ranges of a slice).
573
+ // Positive values represent the number of `Ref` active. Negative values
574
+ // represent the number of `RefMut` active. Multiple `RefMut`s can only be
575
+ // active at a time if they refer to distinct, nonoverlapping components of a
576
+ // `RefCell` (e.g., different ranges of a slice).
577
577
//
578
578
// `Ref` and `RefMut` are both two words in size, and so there will likely never
579
579
// be enough `Ref`s or `RefMut`s in existence to overflow half of the `usize`
580
- // range. Thus, a `BorrowFlag` will probably never overflow. However, this is
581
- // not a guarantee, as a pathological program could repeatedly create and then
582
- // mem::forget `Ref`s or `RefMut`s. Thus, all code must explicitly check for
583
- // overflow in order to avoid unsafety.
584
- type BorrowFlag = usize ;
580
+ // range. Thus, a `BorrowFlag` will probably never overflow or underflow.
581
+ // However, this is not a guarantee, as a pathological program could repeatedly
582
+ // create and then mem::forget `Ref`s or `RefMut`s. Thus, all code must
583
+ // explicitly check for overflow and underflow in order to avoid unsafety.
584
+ type BorrowFlag = isize ;
585
585
const UNUSED : BorrowFlag = 0 ;
586
- const MIN_WRITING : BorrowFlag = ( !0 ) /2 + 1 ; // 0b1000...
587
586
588
587
impl < T > RefCell < T > {
589
588
/// Creates a new `RefCell` containing `value`.
@@ -1022,12 +1021,12 @@ impl<'b> BorrowRef<'b> {
1022
1021
#[ inline]
1023
1022
fn new ( borrow : & ' b Cell < BorrowFlag > ) -> Option < BorrowRef < ' b > > {
1024
1023
let b = borrow. get ( ) ;
1025
- if b >= MIN_WRITING {
1024
+ if b < UNUSED {
1026
1025
None
1027
1026
} else {
1028
1027
// Prevent the borrow counter from overflowing into
1029
1028
// a writing borrow.
1030
- assert ! ( b < MIN_WRITING - 1 ) ;
1029
+ assert ! ( b != isize :: max_value ( ) ) ;
1031
1030
borrow. set ( b + 1 ) ;
1032
1031
Some ( BorrowRef { borrow } )
1033
1032
}
@@ -1038,7 +1037,7 @@ impl<'b> Drop for BorrowRef<'b> {
1038
1037
#[ inline]
1039
1038
fn drop ( & mut self ) {
1040
1039
let borrow = self . borrow . get ( ) ;
1041
- debug_assert ! ( borrow < MIN_WRITING && borrow != UNUSED ) ;
1040
+ debug_assert ! ( borrow > UNUSED ) ;
1042
1041
self . borrow . set ( borrow - 1 ) ;
1043
1042
}
1044
1043
}
@@ -1052,7 +1051,7 @@ impl<'b> Clone for BorrowRef<'b> {
1052
1051
debug_assert ! ( borrow != UNUSED ) ;
1053
1052
// Prevent the borrow counter from overflowing into
1054
1053
// a writing borrow.
1055
- assert ! ( borrow < MIN_WRITING - 1 ) ;
1054
+ assert ! ( borrow != isize :: max_value ( ) ) ;
1056
1055
self . borrow . set ( borrow + 1 ) ;
1057
1056
BorrowRef { borrow : self . borrow }
1058
1057
}
@@ -1251,12 +1250,8 @@ impl<'b> Drop for BorrowRefMut<'b> {
1251
1250
#[ inline]
1252
1251
fn drop ( & mut self ) {
1253
1252
let borrow = self . borrow . get ( ) ;
1254
- debug_assert ! ( borrow >= MIN_WRITING ) ;
1255
- self . borrow . set ( if borrow == MIN_WRITING {
1256
- UNUSED
1257
- } else {
1258
- borrow - 1
1259
- } ) ;
1253
+ debug_assert ! ( borrow < UNUSED ) ;
1254
+ self . borrow . set ( borrow + 1 ) ;
1260
1255
}
1261
1256
}
1262
1257
@@ -1266,10 +1261,10 @@ impl<'b> BorrowRefMut<'b> {
1266
1261
// NOTE: Unlike BorrowRefMut::clone, new is called to create the initial
1267
1262
// mutable reference, and so there must currently be no existing
1268
1263
// references. Thus, while clone increments the mutable refcount, here
1269
- // we simply go directly from UNUSED to MIN_WRITING .
1264
+ // we explicitly only allow going from UNUSED to UNUSED - 1 .
1270
1265
match borrow. get ( ) {
1271
1266
UNUSED => {
1272
- borrow. set ( MIN_WRITING ) ;
1267
+ borrow. set ( UNUSED - 1 ) ;
1273
1268
Some ( BorrowRefMut { borrow : borrow } )
1274
1269
} ,
1275
1270
_ => None ,
@@ -1284,10 +1279,10 @@ impl<'b> BorrowRefMut<'b> {
1284
1279
#[ inline]
1285
1280
fn clone ( & self ) -> BorrowRefMut < ' b > {
1286
1281
let borrow = self . borrow . get ( ) ;
1287
- debug_assert ! ( borrow >= MIN_WRITING ) ;
1288
- // Prevent the borrow counter from overflowing .
1289
- assert ! ( borrow != ! 0 ) ;
1290
- self . borrow . set ( borrow + 1 ) ;
1282
+ debug_assert ! ( borrow < UNUSED ) ;
1283
+ // Prevent the borrow counter from underflowing .
1284
+ assert ! ( borrow != isize :: min_value ( ) ) ;
1285
+ self . borrow . set ( borrow - 1 ) ;
1291
1286
BorrowRefMut { borrow : self . borrow }
1292
1287
}
1293
1288
}
0 commit comments