Skip to content

Commit

Permalink
docs: example consistent with refcell example
Browse files Browse the repository at this point in the history
  • Loading branch information
snormore committed Apr 27, 2024
1 parent cbe789b commit c413863
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
31 changes: 13 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,25 @@ Here is a simple example of how to use `OwnedRefCell`:

```rust
use owned_ref_cell::OwnedRefCell;
use std::collections::HashMap;

fn main() {
let cell = OwnedRefCell::new(42);
let shared_map = OwnedRefCell::new(HashMap::new());

// Create a new block to limit the scope of the dynamic borrow
{
let value = cell.borrow();
assert_eq!(*value, 42);

// Cannot borrow mutably when already borrowed immutably
assert!(cell.try_borrow_mut().is_none());
let mut map = shared_map.borrow_mut();
map.insert("green", 92388);
map.insert("blue", 11837);
map.insert("red", 11826);
map.insert("yellow", 38);
}

{
let mut value = cell.borrow_mut();
*value = 45;

// Cannot borrow when already borrowed mutably
assert!(cell.try_borrow().is_none());
}

{
let value = cell.borrow();
assert_eq!(*value, 45);
}
// Note that if we had not let the previous borrow of the cache fall out
// of scope then the subsequent borrow would cause a dynamic thread panic.
// This is the major hazard of using `OwnedRefCell`.
let total: i32 = shared_map.borrow().values().sum();
assert_eq!(total, 116089);
}
```

Expand Down
53 changes: 37 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,25 @@
//!
//! ```
//! use owned_ref_cell::OwnedRefCell;

Check failure on line 31 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Lints

needless `fn main` in doctest
//! let cell = OwnedRefCell::new(42);
//! use std::collections::HashMap;
//!
//! {
//! let value = cell.borrow();
//! assert_eq!(*value, 42);
//! // Cannot borrow mutably when already borrowed immutably
//! assert!(cell.try_borrow_mut().is_none());
//! }
//! fn main() {
//! let shared_map = OwnedRefCell::new(HashMap::new());
//!
//! {
//! let mut value = cell.borrow_mut();
//! *value = 45;
//! }
//! // Create a new block to limit the scope of the dynamic borrow
//! {
//! let mut map = shared_map.borrow_mut();
//! map.insert("green", 92388);
//! map.insert("blue", 11837);
//! map.insert("red", 11826);
//! map.insert("yellow", 38);
//! }
//!
//! {
//! let value = cell.borrow();
//! assert_eq!(*value, 45);
//! // Note that if we had not let the previous borrow of the cache fall out
//! // of scope then the subsequent borrow would cause a dynamic thread panic.
//! // This is the major hazard of using `RefCell`.
//! let total: i32 = shared_map.borrow().values().sum();
//! assert_eq!(total, 116089);
//! }
//! ```
//!
Expand Down Expand Up @@ -183,12 +185,31 @@ impl<T> Drop for OwnedRefMut<T> {

#[cfg(test)]
mod tests {
use std::panic::{self, AssertUnwindSafe};
use std::{
collections::HashMap,
panic::{self, AssertUnwindSafe},
};

use super::*;

#[test]
fn test_owned_ref_cell_extend_borrow_across_scopes() {
fn hashmap_borrow_mut_modify_descope_borrow() {
let shared_map = OwnedRefCell::new(HashMap::new());

{
let mut map = shared_map.borrow_mut();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
map.insert("d", 4);
}

let total: i32 = shared_map.borrow().values().sum();
assert_eq!(total, 10);
}

#[test]
fn extend_borrow_across_scopes() {
let cell = OwnedRefCell::new(10);

// Function that extends the mutable borrow across its original scope
Expand Down

0 comments on commit c413863

Please sign in to comment.