Shared Container
A unified abstraction for shared data access in both multi-threaded and single-threaded environments.
Overview
shared-container provides a unified abstraction over different container types used for shared data access with
interior mutability in different contexts. It abstracts over the differences between:
- Thread-safe
Arc<RwLock<T>>used in multi-threaded environments Rc<RefCell<T>>used in single-threaded environments like WebAssembly
This allows code using these containers to be written once but work efficiently in both contexts.
Features
- Platform-aware implementation: Automatically uses the most efficient implementation based on the target platform
- Unified API: Same API for both multi-threaded and single-threaded environments
- Read/Write access: Provides both read-only and read-write access to the contained data
- Weak references: Supports weak references to prevent reference cycles
- Clone support: Containers can be cloned to create multiple references to the same data
- Transparent access: Uses Rust's deref mechanism for ergonomic access to the contained data
- Async support: Optional support for async/await with Tokio
Usage
Add this to your Cargo.toml:
[]
= "0.2"
Basic Example
use SharedContainer;
// Create a new container with a value
let container = new;
// Read access
if let Some = container.read
// Write access
if let Some = container.write
// Clone the container (both point to the same data)
let container2 = container.clone;
// Changes through one container are visible through the other
if let Some = container2.read
// Create a weak reference
let weak = container.downgrade;
// Upgrade weak reference to strong reference
if let Some = weak.upgrade
Working with Custom Types
use SharedContainer;
use Debug;
let user = User ;
let container = new;
// Get a clone of the contained value
if let Some = container.get_cloned
// Modify the user
if let Some = container.write
Async Support with Tokio
This library provides optional support for async/await with Tokio through the tokio-sync feature:
[]
= { = "0.2", = ["tokio-sync"] }
When the tokio-sync feature is enabled, the library uses Arc<tokio::sync::RwLock<T>> internally, and provides async
methods for read and write access:
use SharedContainer;
async
Platform-specific Behavior
- On native platforms,
SharedContainer<T>usesArc<RwLock<T>>internally - On WebAssembly (
wasm32target), it usesRc<RefCell<T>>internally - With the
tokio-syncfeature, it usesArc<tokio::sync::RwLock<T>>for async support - The API remains the same, but the behavior differs slightly:
- On native platforms, read/write operations can fail if the lock is poisoned
- On WebAssembly, read/write operations can fail if there's already a borrow
- With
tokio-sync, synchronous methods returnNoneand you should use async methods instead
Testing WebAssembly Compatibility
This library includes a feature flag to help test WebAssembly compatibility even on native platforms:
[]
= { = "0.2", = ["force-wasm-impl"] }
When the force-wasm-impl feature is enabled, the library will use the WebAssembly implementation (Rc<RefCell<T>>)
even when compiling for native platforms. This allows you to test WebAssembly-specific behavior without actually
compiling to WebAssembly.
To run the WebAssembly-specific tests:
License
This project is licensed under the MIT License - see the LICENSE file for details.