rcell/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
#![warn(rustdoc::missing_crate_level_docs)]
//! Example:
//! ```
//! use rcell::*;
//!
//! // Our Type
//! #[derive(Debug, PartialEq)]
//! struct MyType<T>(T);
//!
//! let my_rcell = RCell::new(MyType(100u8));
//! assert_eq!(*my_rcell.request().unwrap(), MyType(100));
//! ```
use std::mem;
#[cfg(feature = "sync")]
#[doc(hidden)]
pub use std::sync::{Arc as Strong, Weak};
#[cfg(not(feature = "sync"))]
#[doc(hidden)]
pub use std::rc::{Rc as Strong, Weak};
/// A RCell holding either an `Strong<T>`, a `Weak<T>` or being `Empty`.
#[derive(Debug)]
pub enum RCell<T> {
/// Strong reference
Strong(Strong<T>),
/// Weak reference
Weak(Weak<T>),
/// Empty cell
Empty,
}
impl<T> RCell<T> {
/// Creates a new strong (Strong<T>) RCell from the supplied value.
pub fn new(value: T) -> Self {
RCell::Strong(Strong::new(value))
}
/// Returns 'true' when this RCell contains a `Strong<T>`.
pub fn retained(&self) -> bool {
matches!(*self, RCell::Strong(_))
}
/// Returns the number of strong references holding an object alive. The returned strong
/// count is informal only, the result may be approximate and has race conditions when
/// other threads modify the reference count concurrently.
pub fn refcount(&self) -> usize {
match self {
RCell::Strong(arc) => Strong::strong_count(arc),
RCell::Weak(weak) => weak.strong_count(),
RCell::Empty => 0,
}
}
/// Tries to upgrade this RCell from Weak<T> to Strong<T>. This means that as long the RCell
/// is not dropped the associated data won't be either. When successful it returns
/// Some<Strong<T>> containing the value, otherwise None is returned on failure.
pub fn retain(&mut self) -> Option<Strong<T>> {
match self {
RCell::Strong(strong) => Some(strong.clone()),
RCell::Weak(weak) => {
if let Some(strong) = weak.upgrade() {
let _ = mem::replace(self, RCell::Strong(strong.clone()));
Some(strong)
} else {
None
}
}
RCell::Empty => None,
}
}
/// Downgrades the RCell, any associated value may become dropped when no other references
/// exist. When no strong reference left remaining this cell becomes Empty.
pub fn release(&mut self) {
if let Some(weak) = match self {
RCell::Strong(strong) => Some(Strong::downgrade(strong)),
RCell::Weak(weak) => Some(weak.clone()),
RCell::Empty => None,
} {
if weak.strong_count() > 0 {
let _ = mem::replace(self, RCell::Weak(weak));
} else {
let _ = mem::replace(self, RCell::Empty);
}
}
}
/// Removes the reference to the value. The rationale for this function is to release
/// *any* resource associated with a RCell (potentially member of a struct that lives
/// longer) in case one knows that it will never be upgraded again.
pub fn remove(&mut self) {
let _ = mem::replace(self, RCell::Empty);
}
/// Tries to get an `Strong<T>` from the RCell. This may fail if the RCell was Weak and all
/// other strong references became dropped.
pub fn request(&self) -> Option<Strong<T>> {
match self {
RCell::Strong(arc) => Some(arc.clone()),
RCell::Weak(weak) => weak.upgrade(),
RCell::Empty => None,
}
}
}
/// Helper Trait for replacing the content of a RCell with something new.
pub trait Replace<T> {
/// Replaces the contained value in self with T.
fn replace(&mut self, new: T);
}
impl<T> Replace<Strong<T>> for RCell<T> {
/// Replaces the RCell with the supplied `Strong<T>`. The old entry becomes dropped.
fn replace(&mut self, strong: Strong<T>) {
let _ = mem::replace(self, RCell::Strong(strong));
}
}
impl<T> Replace<Weak<T>> for RCell<T> {
/// Replaces the RCell with the supplied `Weak<T>`. The old entry becomes dropped.
fn replace(&mut self, weak: Weak<T>) {
let _ = mem::replace(self, RCell::Weak(weak));
}
}
impl<T> From<Strong<T>> for RCell<T> {
/// Creates a new strong RCell with the supplied `Strong<T>`.
fn from(strong: Strong<T>) -> Self {
RCell::Strong(strong)
}
}
impl<T> From<Weak<T>> for RCell<T> {
/// Creates a new weak RCell with the supplied `Weak<T>`.
fn from(weak: Weak<T>) -> Self {
RCell::Weak(weak)
}
}
impl<T> Default for RCell<T> {
/// Creates an RCell that doesn't hold any reference.
fn default() -> Self {
RCell::Empty
}
}
#[cfg(test)]
mod tests {
use crate::{RCell, Strong, Replace};
#[test]
fn smoke() {
let rcell = RCell::new("foobar");
assert!(rcell.retained());
}
#[test]
fn new() {
let mut rcell = RCell::new("foobar");
assert!(rcell.retained());
assert_eq!(*rcell.request().unwrap(), "foobar");
rcell.release();
assert_eq!(rcell.request(), None);
}
#[test]
fn default() {
let rcell = RCell::<i32>::default();
assert!(!rcell.retained());
assert_eq!(rcell.request(), None);
}
#[test]
fn from_strong() {
let strong = Strong::new("foobar");
let mut rcell = RCell::from(strong);
assert!(rcell.retained());
assert_eq!(*rcell.request().unwrap(), "foobar");
rcell.release();
assert_eq!(rcell.request(), None);
}
#[test]
fn from_weak_release() {
let strong = Strong::new("foobar");
let weak = Strong::downgrade(&strong);
let mut rcell = RCell::from(weak);
assert!(!rcell.retained());
assert_eq!(*rcell.request().unwrap(), "foobar");
rcell.release();
assert_eq!(*rcell.request().unwrap(), "foobar");
rcell.remove();
assert_eq!(rcell.request(), None);
}
#[test]
fn from_weak_drop_original() {
let strong = Strong::new("foobar");
let weak = Strong::downgrade(&strong);
let mut rcell = RCell::from(weak);
assert!(!rcell.retained());
assert_eq!(*rcell.request().unwrap(), "foobar");
drop(strong);
assert_eq!(rcell.request(), None);
rcell.remove();
assert_eq!(rcell.request(), None);
}
#[test]
fn replace_strong() {
let mut rcell = RCell::default();
assert!(!rcell.retained());
rcell.replace(Strong::new("foobar"));
assert_eq!(*rcell.request().unwrap(), "foobar");
rcell.remove();
assert_eq!(rcell.request(), None);
}
#[test]
fn replace_weak() {
let strong = Strong::new("foobar");
let mut rcell = RCell::default();
assert!(!rcell.retained());
rcell.replace(Strong::downgrade(&strong));
assert_eq!(*rcell.request().unwrap(), "foobar");
rcell.remove();
assert_eq!(rcell.request(), None);
}
}