From c413863c1f02d72dcdb2a243797e2682113b4ffb Mon Sep 17 00:00:00 2001 From: Steven Normore Date: Sat, 27 Apr 2024 11:00:35 -0400 Subject: [PATCH] docs: example consistent with refcell example --- README.md | 31 +++++++++++++------------------ src/lib.rs | 53 +++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 7256b84..a7865a1 100644 --- a/README.md +++ b/README.md @@ -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); } ``` diff --git a/src/lib.rs b/src/lib.rs index 925818a..ef06e2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,23 +29,25 @@ //! //! ``` //! use owned_ref_cell::OwnedRefCell; -//! 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); //! } //! ``` //! @@ -183,12 +185,31 @@ impl Drop for OwnedRefMut { #[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