1 unstable release
Uses old Rust 2015
| 0.1.2 | Jun 11, 2018 |
|---|---|
| 0.1.1 |
|
| 0.1.0 |
|
#985 in Memory management
110KB
1.5K
SLoC
drc
A Rust library to introduce the Drc smart pointer along with its Weak counterpart.
Usage
Add the following to your Cargo.toml:
[dependencies]
drc = "0.1"
and then add this to your crate root:
extern crate drc;
lib.rs:
This small library provides the Drc and Weak types. 'Drc'
stands for 'Dynamically Reference Counted', and is the primary type that
is used to interact with data stored within. It behaves identically to an
Rc, except that it can be converted into an Arc, which is safe to
pass across threads and then convert back into a Drc. This prevents
unnecessary atomic operations when working on a single thread, without
limiting the data to the thread.
Technical Details
Essentially, when an Arc is converted into a Drc, it is actually
preserved in memory within a data structure located on the heap. Kept here
are also a "local" strong and weak reference count. If the local strong
reference count is zero, the stored Arc will be dropped, but at any
other positive value, the Arc will be preserved. This effectively means
that, within a thread, a Drc may be cloned and passed around indefinitely
without any atomic operations occuring until the final Drc is dropped.
Drc's Weak functions similarly, but is a bit more complicated than
Arc's Weak or Rc's Weak. Essentially,
even when Drc's local strong reference count reaches zero, though the
Arc will be dropped, a Drc can still be created if another Arc or
set of Drcs exists by upgrading a Weak Drc.
Drc::new is simply a convenience method to create the Arc and place
it within. It works exactly the same way as using from with an Arc.