use crate::cell::cell::Share2Cell;
use crate::{Core, Cx};
use std::rc::{Rc, Weak};
pub struct Share2<T> {
pub(crate) rc: Rc<Share2Cell<T>>,
}
impl<T> Share2<T> {
pub fn new<A>(cx: &Cx<A>, val: T) -> Self {
Self {
rc: Rc::new(cx.nexus.share2_owner.cell(val)),
}
}
#[inline]
pub fn rw<'a, A>(&'a self, cx: &'a mut Cx<A>) -> (&'a mut T, &'a mut Core) {
let borrow = cx.nexus.share2_owner.rw(&self.rc);
(borrow, &mut cx.nexus.core)
}
#[inline]
pub fn downgrade(&self) -> Share2Weak<T> {
Share2Weak {
weak: Rc::downgrade(&self.rc),
}
}
#[inline]
pub fn strong_count(&self) -> usize {
Rc::strong_count(&self.rc)
}
#[inline]
pub fn weak_count(&self) -> usize {
Rc::weak_count(&self.rc)
}
}
impl<T> Clone for Share2<T> {
fn clone(&self) -> Self {
Self {
rc: self.rc.clone(),
}
}
}
pub struct Share2Weak<T> {
weak: Weak<Share2Cell<T>>,
}
impl<T> Share2Weak<T> {
pub fn upgrade(&self) -> Option<Share2<T>> {
self.weak.upgrade().map(|rc| Share2 { rc })
}
pub fn strong_count(&self) -> usize {
self.weak.strong_count()
}
pub fn weak_count(&self) -> usize {
self.weak.weak_count()
}
}
impl<T> Clone for Share2Weak<T> {
fn clone(&self) -> Self {
Self {
weak: self.weak.clone(),
}
}
}