use std::{any::Any, future::Future, sync::Arc};
use super::instance::Instance;
use super::{CallContext, ContextError, ContextValue, OperationDeclaration, Response};
use crate::{Message, StreamSpec};
#[derive(Clone)]
pub struct RequestClaims {
owner: Arc<dyn Any + Send + Sync>,
}
pub trait Host: Send + Sync + Sized {
type Error: std::error::Error + Send + Sync + 'static;
type Instance<O: OperationDeclaration>: Instance<O, Error = Self::Error>;
fn context(&self) -> &CallContext;
fn with_value<T: ContextValue>(&self, value: T) -> Result<Self, ContextError>;
fn submit<O: OperationDeclaration>(&self, request: O::Request) -> impl Future<Output = Result<Self::Instance<O>, Self::Error>> + Send;
fn submit_owned<O: OperationDeclaration>(
&self, request: O::Request, claims: RequestClaims,
) -> impl Future<Output = Result<Self::Instance<O>, Self::Error>> + Send;
fn call_owned<O: OperationDeclaration>(
&self, request: O::Request, claims: RequestClaims,
) -> impl Future<Output = Result<Response<O::Response>, Self::Error>> + Send {
async move { self.submit_owned::<O>(request, claims).await?.join().await }
}
fn call<O: OperationDeclaration>(&self, request: O::Request)
-> impl Future<Output = Result<Response<O::Response>, Self::Error>> + Send;
fn publish_on<S: StreamSpec>(&self, row: &S::Row) -> impl Future<Output = Result<(), Self::Error>> + Send
where
S::Row: Message;
fn publish<S: StreamSpec<Row = S> + Message>(&self, row: &S) -> impl Future<Output = Result<(), Self::Error>> + Send {
self.publish_on::<S>(row)
}
}
impl RequestClaims {
pub fn retain(owner: Arc<dyn Any + Send + Sync>) -> Self {
Self { owner }
}
pub fn owner<T: Any + Send + Sync>(&self) -> Option<&T> {
self.owner.downcast_ref()
}
}