use super::S;
use crate::{ActorError, Result};
use async_trait::async_trait;
use flume::Receiver;
use interface::{TryRead, UniqueIdentifier, Who};
use std::any::type_name;
use std::fmt::Debug;
use std::{fmt::Display, sync::Arc};
use tokio::sync::Mutex;
#[derive(Clone)]
pub(crate) struct Input<C, U, const N: usize>
where
U: UniqueIdentifier,
C: TryRead<U>,
{
rx: Receiver<S<U>>,
client: Arc<Mutex<C>>,
hash: u64,
}
impl<C, U, const N: usize> Input<C, U, N>
where
U: UniqueIdentifier,
C: TryRead<U>,
{
pub fn new(rx: Receiver<S<U>>, client: Arc<Mutex<C>>, hash: u64) -> Self {
Self { rx, client, hash }
}
}
impl<C, U, const N: usize> Who<U> for Input<C, U, N>
where
C: TryRead<U>,
U: UniqueIdentifier,
{
}
impl<C, U, const N: usize> Display for Input<C, U, N>
where
C: TryRead<U>,
U: UniqueIdentifier,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "#{:>19}: {}", self.hash, Who::who(self))
}
}
impl<C, U, const N: usize> Debug for Input<C, U, N>
where
C: TryRead<U> + Debug,
U: UniqueIdentifier,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Input")
.field("rx", &self.rx)
.field("client", &self.client)
.field("hash", &self.hash)
.finish()
}
}
#[async_trait]
pub(crate) trait InputObject: Display + Send + Sync {
async fn recv(&mut self) -> Result<()>;
fn who(&self) -> String;
fn get_hash(&self) -> u64;
fn capacity(&self) -> Option<usize>;
}
impl Debug for Box<dyn InputObject> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&std::ops::Deref::deref(&self), f)
}
}
#[async_trait]
impl<'a, C, U, const N: usize> InputObject for Input<C, U, N>
where
C: TryRead<U>,
<C as TryRead<U>>::Error: 'static,
U: UniqueIdentifier,
{
async fn recv(&mut self) -> Result<()> {
let data = self.rx.recv_async().await.map_err(|e| {
ActorError::DropRecv {
msg: format!("input {} to {}", type_name::<U>(), type_name::<C>()), source: e,
}
})?;
let mut client = self.client.lock().await;
(*client).boxed_try_read(data)?;
log::debug!(
"{} RECV@{N}: {} - {}",
self.hash,
type_name::<U>(),
type_name::<C>()
); Ok(())
}
fn who(&self) -> String {
Who::who(self)
}
fn get_hash(&self) -> u64 {
self.hash
}
fn capacity(&self) -> Option<usize> {
self.rx.capacity()
}
}