Expand description
Description
our
provides a highly generic shared mutable data abstraction.
Usage
Shared
is a generic wrapper around what is usually a smart pointer to something with interior mutability.
It provides a way to construct and access shared mutable data, and also provides a way to compare and hash shared values.
Even though Shared
is usually implemented with some kind of interior mutability,
Shared
’s methods that return write guards to the shared value require mutable references to the Shared
itself.
While this can be easily circumvented by simply cloning the Shared
, it is implemented this way to try
to prevent at compile-time accidentally trying to acquire two exclusive guards, which would panic for non-thread-safe
Shared
s and deadlock for thread-safe ones.
Shared
has three type parameters:
- The type of the shared value
- A
ShareKind
, which determines how the shared value is constructed and accessedShareUnsync
is a non-thread-safe shared value implemented asRc<RefCell<T>>
ShareSync
is a thread-safe shared value implemented asArc<parking_lot::RwLock<T>>
- A type which usually implements
PartialEqKind
,EqKind
,PartialOrdKind
,OrdKind
, andHashKind
, which determines how shared values are compared and hashed.
Type aliases
There are four type aliases for Shared
provided for convenience:
Non-thread-safe | Thread-safe | |
---|---|---|
Compare by reference | UnsyncByRef | SyncByRef |
Compare by value | UnsyncByVal | SyncByVal |
Example
use our::*;
// `SyncByRef` is a thread-safe shared value with by-reference comparison and hashing.
let mut a = SyncByRef::new(0);
let mut b = a.clone();
std::thread::spawn(move || b.set(1)).join().unwrap();
assert_eq!(a.get(), 1);
let c = SyncByRef::new(1);
assert_ne!(a, c); // Notice that while while `a` and `c` both equal `1`,
// they do not compare equal because they are different
// pointers.
// `UnsyncByVal` is a non-thread-safe shared value with by-value comparison and hashing.
let a = UnsyncByVal::new(5);
let b = UnsyncByVal::new(5);
assert_eq!(a, b); // Notice that `a` and `b` compare equal
// even though they are different pointers.
Structs
- Compare and hash shared values by reference
- Compare and hash shared values by value
- A thread-safe guard
GuardKind
- A non-thread-safe
GuardKind
- A guard that allows read-only access to a shared value
- A thread-safe
ShareKind
- A non-thread-safe
ShareKind
- A shared, mutable value
- A guard that allows read-write access to a shared value
Traits
- A way of comparing two shared value for equality
- A family of guards
- A way of hashing a shared value
- A way of comparing two shared value for ordering
- A way of comparing two shared value for partial equality
- A way of comparing two shared value for partial ordering
- A way of constructing and accessing shared mutable state
Type Definitions
- A thread-safe shared value with by-reference comparison and hashing
- A thread-safe shared value with by-value comparison and hashing
- A thread-safe read guard
- A thread-safe write guard
- A non-thread-safe shared value with by-reference comparison and hashing
- A non-thread-safe shared value with by-value comparison and hashing
- A non-thread-safe read guard
- A non-thread-safe write guard