pub struct SharedContainer<T: Debug> { /* private fields */ }Expand description
A unified container for shared data that works in both multi-threaded and single-threaded environments.
This struct provides an abstraction over Arc<RwLock<T>> (used in multi-threaded environments)
and Rc<RefCell<T>> (used in single-threaded environments like WebAssembly).
It allows code to be written once but compile to the most efficient implementation based on the environment where it will run.
Implementations§
Sourcepub fn get_cloned(&self) -> Option<T>
pub fn get_cloned(&self) -> Option<T>
Gets a clone of the contained value.
This method acquires a read lock, clones the value, and releases the lock.
§Returns
Some(T): A clone of the contained valueNone: If the lock couldn’t be acquired
Sourcepub fn read(&self) -> Option<SharedReadGuard<'_, T>>
pub fn read(&self) -> Option<SharedReadGuard<'_, T>>
Gets a read-only access guard to the contained value.
§Returns
Some(SharedReadGuard<T>): A guard allowing read-only access to the valueNone: If the lock couldn’t be acquired (in multi-threaded mode)
Sourcepub fn write(&self) -> Option<SharedWriteGuard<'_, T>>
pub fn write(&self) -> Option<SharedWriteGuard<'_, T>>
Gets a writable access guard to the contained value.
§Returns
Some(SharedWriteGuard<T>): A guard allowing read-write access to the valueNone: If the lock couldn’t be acquired (in multi-threaded mode)
Sourcepub fn downgrade(&self) -> WeakSharedContainer<T>
pub fn downgrade(&self) -> WeakSharedContainer<T>
Creates a weak reference to this container.
A weak reference doesn’t prevent the value from being dropped when no strong references remain, which helps break reference cycles that could cause memory leaks.
§Returns
A WeakSharedContainer that points to the same data