Expand description
Trc is a performant heap-allocated smart pointer that implements a thread reference counting.
Trc stands for: Thread Reference Counted.
Trc provides shared ownership of the data similar to Arc<T> and Rc<T>.
It implements thread reference counting, which is based on the observation that most objects are only used by one thread.
This means that two reference counts can be created: one for local thread use, and one atomic one for sharing between threads.
Thread reference counting sets the atomic reference count to the number of threads using the data.
§Breaking reference cycles with Weak<T>
A cycle between Trc pointers cannot be deallocated as the reference counts will never reach zero. The solution is a Weak<T>.
A Weak is a non-owning reference to the data held by a Trc.
They break reference cycles by adding a layer of indirection and act as an observer. They cannot access the data directly, and
must be converted back into Trc. Weak does not keep the value alive (which can be dropped), and only keeps the backing allocation alive.
§Sending data across threads with SharedTrc<T>
To soundly implement thread safety Trc<T> is !Send and !Sync.
To solve this, Trc introduces a SharedTrc<T>, which is Send and Sync.
SharedTrc is the only way to safely send a Trc’s data across threads without using a Weak.
Because Trc is not part of the standard library,
the CoerceUnsized and Receiver traits cannot currently be implemented by default.
However, Trc provides dyn_unstable trait which enables the above traits for
Trc and SharedTrc and must be used with nightly Rust (cargo +nightly ...).
Structs§
- Shared
Trc SharedTrcis a thread-safe wrapper used to sendTrcs across threads. UnlikeTrc(which is!Sendand!Sync),SharedTrcisSendandSync. This means that along withWeak,SharedTrcis one of the ways to send aTrcacross threads. However, unlikeWeak,SharedTrcdoes not modify the weak count - and only modifies the atomic count. In addition,SharedTrcwill not fail on conversion back to aTrcbecause it prevents the dataTfrom being dropped.- Trc
Trcis a performant heap-allocated smart pointer that implements thread reference counting.Trcstands for: Thread Reference Counted.Trcprovides shared ownership of the data similar toArc<T>andRc<T>. It implements thread reference counting, which is based on the observation that most objects are only used by one thread. This means that two reference counts can be created: one for local thread use, and one atomic one for sharing between threads. Thread reference counting sets the atomic reference count to the number of threads using the data.- Weak
Weakis a non-owning reference toTrc’s data. It is used to prevent cyclic references which cause memory to never be freed.Weakdoes not keep the value alive (which can be dropped), they only keep the backing allocation alive.Weakcannot directly access the data, and must be converted intoTrcto do so.