diff --git a/src/sync/arc.rs b/src/sync/arc.rs index cdbdc473..35ea36d0 100644 --- a/src/sync/arc.rs +++ b/src/sync/arc.rs @@ -5,7 +5,7 @@ use std::rc::Rc; use std::sync::atomic::Ordering::*; -/// TODO +/// Mock implementation of `std::sync::Arc`. #[derive(Debug)] pub struct Arc { inner: Rc>, @@ -20,7 +20,7 @@ struct Inner { } impl Arc { - /// TODO + /// Constructs a new `Arc`. pub fn new(value: T) -> Arc { Arc { inner: Rc::new(Inner { @@ -29,6 +29,17 @@ impl Arc { }) } } + + /// Gets the number of strong (`Arc`) pointers to this value. + pub fn strong_count(this: &Self) -> usize { + this.inner.ref_cnt.load(SeqCst) + } + + /// Returns `true` if the two `Arc`s point to the same value (not + /// just values that compare as equal). + pub fn ptr_eq(this: &Self, other: &Self) -> bool { + Rc::ptr_eq(&this.inner, &other.inner) + } } impl ops::Deref for Arc { @@ -52,3 +63,15 @@ impl Drop for Arc { self.inner.ref_cnt.fetch_sub(1, AcqRel); } } + +impl Default for Arc { + fn default() -> Arc { + Arc::new(Default::default()) + } +} + +impl From for Arc { + fn from(t: T) -> Self { + Arc::new(t) + } +}