use super::Dispatch;
use super::*;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(PartialEq, Serialize, Deserialize, Debug, Clone, Copy)]
pub struct HandleToExchange(pub(crate) ServiceObjectId);
impl HandleToExchange {
pub fn create_null() -> Self {
Self(crate::forwarder::NULL_ID)
}
}
pub struct Skeleton {
pub(crate) raw: Arc<dyn Dispatch>,
}
impl Clone for Skeleton {
fn clone(&self) -> Self {
Self {
raw: Arc::clone(&self.raw),
}
}
}
impl Skeleton {
pub fn new<T: ?Sized + Service>(service: impl IntoSkeleton<T>) -> Self {
service.into_skeleton()
}
}
pub fn create_skeleton(raw: Arc<dyn Dispatch>) -> Skeleton {
Skeleton {
raw,
}
}
pub trait IntoSkeleton<T: ?Sized + Service> {
fn into_skeleton(self) -> Skeleton;
}
pub trait ImportProxy<T: ?Sized + Service>: Sized {
fn import_proxy(port: Weak<dyn Port>, handle: HandleToExchange) -> Self;
}
pub fn export_service_into_handle(context: &crate::context::Context, service: Skeleton) -> HandleToExchange {
context.get_port().upgrade().unwrap().register_service(service.raw)
}
pub fn import_service_from_handle<T: ?Sized + Service, P: ImportProxy<T>>(
context: &crate::context::Context,
handle: HandleToExchange,
) -> P {
P::import_proxy(context.get_port(), handle)
}
pub fn import_null_proxy<T: ?Sized + Service, P: ImportProxy<T>>() -> P {
P::import_proxy(crate::port::null_weak_port(), HandleToExchange::create_null())
}