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§
- ByRef
- Compare and hash shared values by reference
- ByVal
- Compare and hash shared values by value
- Guard
Sync - A thread-safe guard
GuardKind
- Guard
Unsync - A non-thread-safe
GuardKind
- Read
Guard - A guard that allows read-only access to a shared value
- Share
Sync - A thread-safe
ShareKind
- Share
Unsync - A non-thread-safe
ShareKind
- Shared
- A shared, mutable value
- Write
Guard - A guard that allows read-write access to a shared value
Traits§
- EqKind
- A way of comparing two shared value for equality
- Guard
Kind - A family of guards
- Hash
Kind - A way of hashing a shared value
- OrdKind
- A way of comparing two shared value for ordering
- Partial
EqKind - A way of comparing two shared value for partial equality
- Partial
OrdKind - A way of comparing two shared value for partial ordering
- Share
Kind - A way of constructing and accessing shared mutable state
Type Aliases§
- Sync
ByRef - A thread-safe shared value with by-reference comparison and hashing
- Sync
ByVal - A thread-safe shared value with by-value comparison and hashing
- Sync
Read Guard - A thread-safe read guard
- Sync
Write Guard - A thread-safe write guard
- Unsync
ByRef - A non-thread-safe shared value with by-reference comparison and hashing
- Unsync
ByVal - A non-thread-safe shared value with by-value comparison and hashing
- Unsync
Read Guard - A non-thread-safe read guard
- Unsync
Write Guard - A non-thread-safe write guard