Skip to content

Commit c7928f5

Browse files
committed
Expand comment on BorrowRef::new
1 parent f0b3c02 commit c7928f5

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Diff for: src/libcore/cell.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1103,10 +1103,20 @@ impl<'b> BorrowRef<'b> {
11031103
fn new(borrow: &'b Cell<BorrowFlag>) -> Option<BorrowRef<'b>> {
11041104
let b = borrow.get().wrapping_add(1);
11051105
if !is_reading(b) {
1106-
// If there's currently a writing borrow, or if incrementing the
1107-
// refcount would overflow into a writing borrow.
1106+
// Incrementing borrow can result in a non-reading value (<= 0) in these cases:
1107+
// 1. It was < 0, i.e. there are writing borrows, so we can't allow a read borrow
1108+
// due to Rust's reference aliasing rules
1109+
// 2. It was isize::max_value() (the max amount of reading borrows) and it overflowed
1110+
// into isize::min_value() (the max amount of writing borrows) so we can't allow
1111+
// an additional read borrow because isize can't represent so many read borrows
1112+
// (this can only happen if you mem::forget more than a small constant amount of
1113+
// `Ref`s, which is not good practice)
11081114
None
11091115
} else {
1116+
// Incrementing borrow can result in a reading value (< 0) in these cases:
1117+
// 1. It was = 0, i.e. it wasn't borrowed, and we are taking the first read borrow
1118+
// 2. It was > 0 and < isize::max_value(), i.e. there were read borrows, and isize
1119+
// is large enough to represent having one more read borrow
11101120
borrow.set(b);
11111121
Some(BorrowRef { borrow })
11121122
}

0 commit comments

Comments
 (0)