📦 AnyRef — Runtime-Typed Reference-Counted Smart Pointer for Rust
AnyRef is a custom smart pointer similar to Arc, designed for storing dynamically typed (dyn Any) values with strong and weak reference support, runtime downcasting, and optional thread-safe interior mutability.
It is ideal for scenarios where type erasure and runtime polymorphism are needed without exposing generic interfaces.
✨ Features
- ✅ Runtime type storage via
dyn Any - 🔁 Strong and weak reference counting
- 🔐 Optional thread-safe mutability (with internal locking)
- 🔍 Safe runtime downcasting (
try_downcast,try_downcast_mut) - 🚫 No generics in the pointer interface
- 🧠 Suitable for runtime-managed object graphs
⚙️ Example Usage
Basic Allocation and Access
use AnyRef;
let a = new;
assert_eq!;
Runtime Downcasting
use AnyRef;
let a = new;
if let Some = a.
Cloning and Reference Counting
use AnyRef;
let a = new;
let b = a.clone;
assert_eq!;
Weak Reference
use AnyRef;
let a = new;
let w = a.downgrade;
assert!;
drop;
assert!;
Thread-Safe Mode
use AnyRef;
use Barrier;
use thread;
let x = new;
let mut handles = vec!;
let barrier = new;
for i in 0..10
for h in handles
assert_eq!;
assert_eq!;
📦 Installation
Install AnyRef from crates.io Open your Cargo.toml and add:
[]
= "0.1.4" # or the latest version available
📄 License
Apache-2.0