use std::{marker::PhantomData, sync::Arc};
use super::{Interface, InterfaceDeref, InterfaceDerefMut, SignalEmitter};
use crate::runtime::locks::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;
InterfaceDeref {
iface,
phantom: PhantomData,
}
}
pub async fn get_mut(&self) -> InterfaceDerefMut<'_, I> {
let iface = self.lock.write().await;
InterfaceDerefMut {
iface,
phantom: PhantomData,
}
}
pub fn signal_emitter(&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,
}
}
}