From 96a09371273590d987ba917d8f89d2e38d9b3849 Mon Sep 17 00:00:00 2001 From: Steven Normore Date: Wed, 24 Apr 2024 13:52:31 -0400 Subject: [PATCH] chore: clean up naming --- benches/bench.rs | 36 ++++++++++++---------- src/lib.rs | 78 ++++++++++++++++++++++++------------------------ 2 files changed, 59 insertions(+), 55 deletions(-) diff --git a/benches/bench.rs b/benches/bench.rs index 765d0e5..9eb9250 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -8,9 +8,8 @@ use test::Bencher; fn bench_borrow_mut(b: &mut Bencher) { let cell = OwnedRefCell::new(42); b.iter(|| { - // Perform the mutable borrow inside the testing loop for _ in 0..1000 { - test::black_box(cell.try_borrow_mut()); + test::black_box(cell.borrow_mut()); } }); } @@ -19,45 +18,50 @@ fn bench_borrow_mut(b: &mut Bencher) { fn bench_borrow(b: &mut Bencher) { let cell = OwnedRefCell::new(42); b.iter(|| { - // Perform the immutable borrow inside the testing loop for _ in 0..1000 { - test::black_box(cell.try_borrow()); + test::black_box(cell.borrow()); } }); } #[bench] -fn bench_borrow_mut_borrow(b: &mut Bencher) { +fn bench_borrow_after_borrow(b: &mut Bencher) { let cell = OwnedRefCell::new(OwnedRefCell::new(42)); b.iter(|| { - let outer_borrow = cell.borrow_mut(); + let _ref = cell.borrow(); + for _ in 0..1000 { - // Perform an inner borrow operation within an outer borrow - let _inner_borrow = outer_borrow.borrow(); + test::black_box(cell.borrow()); } }); } #[bench] -fn bench_borrow_mut_borrow_mut(b: &mut Bencher) { +fn bench_borrow_after_borrow_mut_fails(b: &mut Bencher) { let cell = OwnedRefCell::new(OwnedRefCell::new(42)); b.iter(|| { - let outer_borrow = cell.borrow_mut(); + let _ref = cell.borrow_mut(); + for _ in 0..1000 { - // Perform an inner borrow operation within an outer borrow - let _inner_borrow = outer_borrow.try_borrow_mut(); + assert!( + cell.try_borrow().is_none(), + "Expected failure, but succeeded" + ); } }); } #[bench] -fn bench_borrow_borrow_mut(b: &mut Bencher) { +fn bench_borrow_mut_after_borrow_mut_fails(b: &mut Bencher) { let cell = OwnedRefCell::new(OwnedRefCell::new(42)); b.iter(|| { - let outer_borrow = cell.borrow(); + let _ref = cell.borrow_mut(); + for _ in 0..1000 { - // Perform an inner borrow operation within an outer borrow - let _inner_borrow = outer_borrow.try_borrow_mut(); + assert!( + cell.try_borrow_mut().is_none(), + "Expected failure, but succeeded" + ); } }); } diff --git a/src/lib.rs b/src/lib.rs index 7b5f06a..fcf53df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,8 +6,8 @@ //! is more nuanced. //! //! The main class in this module, `OwnedRefCell`, provides an interface similar to `RefCell`, -//! allowing both mutable and immutable borrows, tracked at runtime to ensure that there are no data races. -//! `OwnedRefCell` should be used when you need temporary mutable access to data inside a data +//! allowing both mutable and immutable borrows, tracked at runtime to ensure that there are no value races. +//! `OwnedRefCell` should be used when you need temporary mutable access to value inside a value //! structure that does not itself provide intrinsic mutable access. //! //! # Differences from `RefCell` @@ -24,7 +24,7 @@ //! //! Unlike `RefCell`, `OwnedRefCell` uses `Rc` to track the borrowing state, and thus it is not //! thread-safe. It is meant for use only in single-threaded scenarios. Attempting to use `OwnedRefCell` -//! in a multithreaded context may lead to data races and is not supported. +//! in a multithreaded context may lead to value races and is not supported. //! //! # Examples //! @@ -54,45 +54,45 @@ //! //! This module also provides: //! -//! - `OwnedRef`: an owned, immutable reference to the data inside an `OwnedRefCell`. -//! - `OwnedRefMut`: an owned, mutable reference to the data inside an `OwnedRefCell`. +//! - `OwnedRef`: an owned, immutable reference to the value inside an `OwnedRefCell`. +//! - `OwnedRefMut`: an owned, mutable reference to the value inside an `OwnedRefCell`. use std::cell::{RefCell, UnsafeCell}; use std::ops::{Deref, DerefMut}; use std::rc::Rc; -/// Provides mutable or immutable access to encapsulated data with owned references. +/// Provides mutable or immutable access to encapsulated value with owned references. pub struct OwnedRefCell { - data: UnsafeCell, - borrow_state: Rc>, + value: UnsafeCell, + state: Rc>, } /// Internal state to keep track of the borrowing status. struct BorrowState { - mutable_borrowed: bool, - immutable_borrow_count: usize, + is_writing: bool, + reading_count: usize, } -/// An immutable reference to the data within `OwnedRefCell`. +/// An immutable reference to the value within `OwnedRefCell`. pub struct OwnedRef { - data: *const T, - borrow_state: Rc>, + value: *const T, + state: Rc>, } -/// A mutable reference to the data within `OwnedRefCell`. +/// A mutable reference to the value within `OwnedRefCell`. pub struct OwnedRefMut { - data: *mut T, - borrow_state: Rc>, + value: *mut T, + state: Rc>, } impl OwnedRefCell { - /// Constructs a new `OwnedRefCell` with the specified data. - pub fn new(data: T) -> Self { + /// Constructs a new `OwnedRefCell` with the specified value. + pub fn new(value: T) -> Self { OwnedRefCell { - data: UnsafeCell::new(data), - borrow_state: Rc::new(RefCell::new(BorrowState { - mutable_borrowed: false, - immutable_borrow_count: 0, + value: UnsafeCell::new(value), + state: Rc::new(RefCell::new(BorrowState { + is_writing: false, + reading_count: 0, })), } } @@ -114,14 +114,14 @@ impl OwnedRefCell { /// Tries to immutably borrow the cell. /// Returns `None` if the cell is already borrowed mutably. pub fn try_borrow(&self) -> Option> { - let mut borrow_state = self.borrow_state.borrow_mut(); - if borrow_state.mutable_borrowed { + let mut state = self.state.borrow_mut(); + if state.is_writing { None } else { - borrow_state.immutable_borrow_count += 1; + state.reading_count += 1; Some(OwnedRef { - data: self.data.get(), - borrow_state: Rc::clone(&self.borrow_state), + value: self.value.get(), + state: Rc::clone(&self.state), }) } } @@ -129,14 +129,14 @@ impl OwnedRefCell { /// Tries to mutably borrow the cell. /// Returns `None` if the cell is already borrowed immutably or mutably. pub fn try_borrow_mut(&self) -> Option> { - let mut borrow_state = self.borrow_state.borrow_mut(); - if borrow_state.mutable_borrowed || borrow_state.immutable_borrow_count > 0 { + let mut state = self.state.borrow_mut(); + if state.is_writing || state.reading_count > 0 { None } else { - borrow_state.mutable_borrowed = true; + state.is_writing = true; Some(OwnedRefMut { - data: self.data.get(), - borrow_state: Rc::clone(&self.borrow_state), + value: self.value.get(), + state: Rc::clone(&self.state), }) } } @@ -147,7 +147,7 @@ impl Deref for OwnedRef { type Target = T; fn deref(&self) -> &Self::Target { - unsafe { &*self.data } + unsafe { &*self.value } } } @@ -156,14 +156,14 @@ impl Deref for OwnedRefMut { type Target = T; fn deref(&self) -> &Self::Target { - unsafe { &*self.data } + unsafe { &*self.value } } } /// Implements `DerefMut` for `OwnedRefMut` to allow dereferencing the owned mutable reference. impl DerefMut for OwnedRefMut { fn deref_mut(&mut self) -> &mut Self::Target { - unsafe { &mut *self.data } + unsafe { &mut *self.value } } } @@ -171,16 +171,16 @@ impl DerefMut for OwnedRefMut { /// references are dropped. impl Drop for OwnedRef { fn drop(&mut self) { - let mut borrow_state = self.borrow_state.borrow_mut(); - borrow_state.immutable_borrow_count -= 1; + let mut state = self.state.borrow_mut(); + state.reading_count -= 1; } } /// Implements `Drop` for `OwnedRefMut` to update the borrowing state when the reference is dropped. impl Drop for OwnedRefMut { fn drop(&mut self) { - let mut borrow_state = self.borrow_state.borrow_mut(); - borrow_state.mutable_borrowed = false; + let mut state = self.state.borrow_mut(); + state.is_writing = false; } }