Expand description
Atomic Strong Count.
Asc is a lighter alternative to Arc for use cases
that don’t need weak references. It provides shared, thread-safe
ownership of heap-allocated data.
§Key differences from Arc
- No
Weakreferences — the allocation is freed as soon as the lastAscis dropped. This also meansAsccannot safely express reference cycles. - Custom allocators are not yet supported —
Ascalways uses the global allocator. This is blocked on the unstableallocator_apifeature and will be reconsidered once it stabilizes.
§Cycle Warning
Asc does not have weak references. Any reference cycle (e.g.,
A → B → A) will cause a memory leak because the strong count never
reaches zero. Asc is suitable for DAGs and tree structures; for
general graphs with back-references, use Arc with
Weak.
§Optional features
serde— Enablesserdeserialization and deserialization.std— EnablesUnwindSafeandRefUnwindSafeimplementations forAsc.unstable— EnablesCoerceUnsizedandDispatchFromDynimplementations, allowingAsc<T>to be coerced toAsc<dyn Trait>. Requires a nightly compiler.
§Example
use asc::Asc;
let a: Asc<i32> = Asc::new(42);
let b = a.clone();
assert_eq!(Asc::strong_count(&a), 2);
assert_eq!(*b, 42);
// try_unwrap fails while other references exist
assert!(Asc::try_unwrap(a.clone()).is_err());
drop(b);
// try_unwrap succeeds with the last reference
let value = Asc::try_unwrap(a).unwrap();
assert_eq!(value, 42);Structs§
- Asc
- Atomic Strong Count.