use std::fmt::Debug;
use std::time::Duration;
use async_trait::async_trait;
use crate::actor::actor::actor_error::ActorError;
use crate::actor::actor::actor_handle::ActorHandle;
use crate::actor::actor::continuer::Continuer;
use crate::actor::actor::pid::ExtendedPid;
use crate::actor::actor::props::Props;
use crate::actor::actor::spawner::SpawnError;
use crate::actor::actor_system::ActorSystem;
use crate::actor::future::Future;
use crate::actor::message::message_handle::MessageHandle;
use crate::actor::message::message_or_envelope::MessageEnvelope;
use crate::actor::message::readonly_message_headers::ReadonlyMessageHeadersHandle;
use crate::actor::message::response::ResponseHandle;
use crate::ctxext::extensions::{ContextExtensionHandle, ContextExtensionId};
pub mod actor_context;
mod actor_context_extras;
mod actor_context_test;
pub mod context_handle;
pub mod mock_context;
mod receive_timeout_timer;
pub mod receiver_context_handle;
pub mod root_context;
pub mod sender_context_handle;
pub mod spawner_context_handle;
mod state;
pub trait Context:
ExtensionContext
+ SenderContext
+ ReceiverContext
+ SpawnerContext
+ BasePart
+ StopperPart
+ Debug
+ Send
+ Sync
+ 'static {
}
pub trait ExtensionContext: ExtensionPart + Send + Sync + 'static {}
pub trait SenderContext: InfoPart + SenderPart + MessagePart + Send + Sync + 'static {}
pub trait ReceiverContext: InfoPart + ReceiverPart + MessagePart + ExtensionPart + Send + Sync + 'static {}
pub trait SpawnerContext: InfoPart + SpawnerPart + Send + Sync + 'static {}
#[async_trait]
pub trait ExtensionPart: Send + Sync + 'static {
async fn get(&mut self, id: ContextExtensionId) -> Option<ContextExtensionHandle>;
async fn set(&mut self, ext: ContextExtensionHandle);
}
#[async_trait]
pub trait InfoPart: Debug + Send + Sync + 'static {
async fn get_parent(&self) -> Option<ExtendedPid>;
async fn get_self_opt(&self) -> Option<ExtendedPid>;
async fn get_self(&self) -> ExtendedPid {
self.get_self_opt().await.expect("self pid not found")
}
async fn set_self(&mut self, pid: ExtendedPid);
async fn get_actor(&self) -> Option<ActorHandle>;
async fn get_actor_system(&self) -> ActorSystem;
}
#[async_trait]
pub trait BasePart: Debug + Send + Sync + 'static {
async fn get_receive_timeout(&self) -> Duration;
async fn get_children(&self) -> Vec<ExtendedPid>;
async fn respond(&self, response: ResponseHandle);
async fn stash(&mut self);
async fn watch(&mut self, pid: &ExtendedPid);
async fn unwatch(&mut self, pid: &ExtendedPid);
async fn set_receive_timeout(&mut self, d: &Duration);
async fn cancel_receive_timeout(&mut self);
async fn forward(&self, pid: &ExtendedPid);
async fn reenter_after(&self, f: Future, continuation: Continuer);
}
#[async_trait]
pub trait MessagePart: Debug + Send + Sync + 'static {
async fn get_message_envelope_opt(&self) -> Option<MessageEnvelope>;
async fn get_message_envelope(&self) -> MessageEnvelope {
self
.get_message_envelope_opt()
.await
.expect("message envelope not found")
}
async fn get_message_handle_opt(&self) -> Option<MessageHandle>;
async fn get_message_handle(&self) -> MessageHandle {
self.get_message_handle_opt().await.expect("message not found")
}
async fn get_message_header_handle(&self) -> Option<ReadonlyMessageHeadersHandle>;
}
#[async_trait]
pub trait SenderPart: Debug + Send + Sync + 'static {
async fn get_sender(&self) -> Option<ExtendedPid>;
async fn send(&mut self, pid: ExtendedPid, message_handle: MessageHandle);
async fn request(&mut self, pid: ExtendedPid, message_handle: MessageHandle);
async fn request_with_custom_sender(&mut self, pid: ExtendedPid, message_handle: MessageHandle, sender: ExtendedPid);
async fn request_future(&self, pid: ExtendedPid, message_handle: MessageHandle, timeout: Duration) -> Future;
}
#[async_trait]
pub trait ReceiverPart: Debug + Send + Sync + 'static {
async fn receive(&mut self, envelope: MessageEnvelope) -> Result<(), ActorError>;
}
#[async_trait]
pub trait SpawnerPart: Send + Sync + 'static {
async fn spawn(&mut self, props: Props) -> ExtendedPid;
async fn spawn_prefix(&mut self, props: Props, prefix: &str) -> ExtendedPid;
async fn spawn_named(&mut self, props: Props, id: &str) -> Result<ExtendedPid, SpawnError>;
}
#[async_trait]
pub trait StopperPart: Debug + Send + Sync + 'static {
async fn stop(&mut self, pid: &ExtendedPid);
async fn stop_future_with_timeout(&mut self, pid: &ExtendedPid, timeout: Duration) -> Future;
async fn stop_future(&mut self, pid: &ExtendedPid) -> Future {
self.stop_future_with_timeout(pid, Duration::from_secs(10)).await
}
async fn poison(&mut self, pid: &ExtendedPid);
async fn poison_future_with_timeout(&mut self, pid: &ExtendedPid, timeout: Duration) -> Future;
async fn poison_future(&mut self, pid: &ExtendedPid) -> Future {
self.stop_future_with_timeout(pid, Duration::from_secs(10)).await
}
}