use std::{
ops::Deref,
ptr,
rc::{Rc, Weak},
};
use super::{Loop, LoopBox};
use crate::Error;
pub unsafe trait IsLoopRc: Clone + AsRef<Loop> + 'static {}
#[derive(Debug)]
struct LoopRcInner {
loop_: LoopBox,
}
#[derive(Clone, Debug)]
pub struct LoopRc {
inner: Rc<LoopRcInner>,
}
impl LoopRc {
pub fn new(properties: Option<&spa::utils::dict::DictRef>) -> Result<Self, Error> {
let loop_ = LoopBox::new(properties)?;
Ok(Self {
inner: Rc::new(LoopRcInner { loop_ }),
})
}
pub unsafe fn from_raw(ptr: ptr::NonNull<pw_sys::pw_loop>) -> Self {
let loop_ = LoopBox::from_raw(ptr);
Self {
inner: Rc::new(LoopRcInner { loop_ }),
}
}
pub fn downgrade(&self) -> LoopWeak {
let weak = Rc::downgrade(&self.inner);
LoopWeak { weak }
}
}
unsafe impl IsLoopRc for LoopRc {}
impl Deref for LoopRc {
type Target = Loop;
fn deref(&self) -> &Self::Target {
self.inner.loop_.deref()
}
}
impl std::convert::AsRef<Loop> for LoopRc {
fn as_ref(&self) -> &Loop {
self.deref()
}
}
pub struct LoopWeak {
weak: Weak<LoopRcInner>,
}
impl LoopWeak {
pub fn upgrade(&self) -> Option<LoopRc> {
self.weak.upgrade().map(|inner| LoopRc { inner })
}
}