Skip to content

Commit

Permalink
Fix BorrowState reborrow
Browse files Browse the repository at this point in the history
A SharedBorrow was dropped immediately instead of being returned
  • Loading branch information
leudz committed Dec 17, 2024
1 parent 82e6e30 commit 81e9209
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/atomic_refcell/borrow_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const MAX_FAILED_BORROWS: usize = HIGH_BIT + (HIGH_BIT >> 1);
pub(super) struct BorrowState(AtomicUsize);

/// Unlocks a shared borrow on drop.
#[must_use]
pub struct SharedBorrow<'a>(&'a BorrowState);

impl Drop for SharedBorrow<'_> {
Expand All @@ -24,13 +25,12 @@ impl Clone for SharedBorrow<'_> {
}

/// Unlocks an exclusive borrow on drop.
#[must_use]
pub struct ExclusiveBorrow<'a>(&'a BorrowState);

impl ExclusiveBorrow<'_> {
pub(crate) fn shared_reborrow(&self) -> SharedBorrow<'_> {
self.0.read_reborrow();

SharedBorrow(self.0)
self.0.read_reborrow()
}
}

Expand Down Expand Up @@ -130,3 +130,18 @@ impl BorrowState {
self.check_overflow(new)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn reborrow() {
let borrow = BorrowState::new();
let write = borrow.write().unwrap();

let read = write.shared_reborrow();

assert_eq!(HIGH_BIT + 1, (read.0).0.load(Ordering::Relaxed));
}
}

0 comments on commit 81e9209

Please sign in to comment.