use std::{marker::PhantomData, sync::Arc};
use super::{Interface, InterfaceDeref, InterfaceDerefMut, SignalEmitter};
use crate::async_lock::RwLock;
pub struct InterfaceRef<I> {
pub(crate) emitter: SignalEmitter<'static>,
pub(crate) lock: Arc<RwLock<dyn Interface>>,
pub(crate) phantom: PhantomData<I>,
}
impl<I> InterfaceRef<I>
where
I: 'static,
{
pub async fn get(&self) -> InterfaceDeref<'_, I> {
let iface = self.lock.read().await;
iface
.downcast_ref::<I>()
.expect("Unexpected interface type");
InterfaceDeref {
iface,
phantom: PhantomData,
}
}
pub async fn get_mut(&self) -> InterfaceDerefMut<'_, I> {
let mut iface = self.lock.write().await;
iface
.downcast_ref::<I>()
.expect("Unexpected interface type");
iface
.downcast_mut::<I>()
.expect("Unexpected interface type");
InterfaceDerefMut {
iface,
phantom: PhantomData,
}
}
pub fn signal_emitter(&self) -> &SignalEmitter<'static> {
&self.emitter
}
#[deprecated(since = "0.5.0", note = "Please use `signal_emitter` instead.")]
pub fn signal_context(&self) -> &SignalEmitter<'static> {
&self.emitter
}
}
impl<I> Clone for InterfaceRef<I> {
fn clone(&self) -> Self {
Self {
emitter: self.emitter.clone(),
lock: self.lock.clone(),
phantom: PhantomData,
}
}
}