use std::{marker::PhantomData, sync::Arc};
use noosphere_core::context::{
HasMutableSphereContext, HasSphereContext, SphereContext, SphereCursor,
};
use noosphere_storage::Storage;
use tokio::sync::Mutex;
#[derive(Clone)]
pub struct SphereChannel<S, Ci, Cm>
where
S: Storage + 'static,
Ci: HasSphereContext<S>,
Cm: HasMutableSphereContext<S>,
{
immutable: Ci,
mutable: Cm,
storage_marker: PhantomData<S>,
}
impl<S, Ci, Cm> SphereChannel<S, Ci, Cm>
where
S: Storage + 'static,
Ci: HasSphereContext<S>,
Cm: HasMutableSphereContext<S>,
{
pub fn new(immutable: Ci, mutable: Cm) -> Self {
Self {
immutable,
mutable,
storage_marker: PhantomData,
}
}
pub fn immutable(&self) -> &Ci {
&self.immutable
}
pub fn mutable(&mut self) -> &mut Cm {
&mut self.mutable
}
}
impl<S> From<SphereContext<S>>
for SphereChannel<S, Arc<SphereContext<S>>, Arc<Mutex<SphereContext<S>>>>
where
S: Storage + 'static,
{
fn from(value: SphereContext<S>) -> Self {
SphereChannel::new(Arc::new(value.clone()), Arc::new(Mutex::new(value)))
}
}
impl<S> From<SphereCursor<Arc<SphereContext<S>>, S>>
for SphereChannel<S, SphereCursor<Arc<SphereContext<S>>, S>, Arc<Mutex<SphereContext<S>>>>
where
S: Storage + 'static,
{
fn from(value: SphereCursor<Arc<SphereContext<S>>, S>) -> Self {
let mutable = Arc::new(Mutex::new(value.clone().to_inner().as_ref().clone()));
SphereChannel::new(value, mutable)
}
}