Crate shared_container

Source
Expand description

§Shared Container Module

This module 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:

  • Arc<std::sync::RwLock<T>> (used in standard multi-threaded environments)
  • Arc<tokio::sync::RwLock<T>> (used for async/await support)
  • Rc<RefCell<T>> (used in single-threaded environments like WebAssembly)

This allows code using these containers to be written once but work efficiently in different contexts.

§Feature Flags

This library provides several feature flags to customize its behavior:

  • std-sync (default): Uses Arc<std::sync::RwLock<T>> for thread-safe access
  • tokio-sync: Uses Arc<tokio::sync::RwLock<T>> for async/await support
  • wasm-sync: Uses Rc<RefCell<T>> for single-threaded environments
  • force-wasm-impl: Legacy feature, equivalent to wasm-sync

§Async Support

When the tokio-sync feature is enabled, the library provides async versions of the read and write methods:

let container = SharedContainer::new(42);

// Read access
let guard = container.read_async().await;
println!("Value: {}", *guard);

// Write access
let mut guard = container.write_async().await;
*guard = 100;

Note that when using the tokio-sync feature, the synchronous read() and write() methods will always return None. You should use the async methods instead.

Structs§

SharedContainer
A unified container for shared data that works in both multi-threaded and single-threaded environments.
WeakSharedContainer
A weak reference to a SharedContainer.

Enums§

SharedReadGuard
A read-only guard for accessing data in a SharedContainer.
SharedWriteGuard
A writable guard for accessing and modifying data in a SharedContainer.