Spaces:
Sleeping
Sleeping
File size: 3,464 Bytes
287a0bc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
use std::fmt::Debug;
use super::{Component, ComponentContext, Handler};
use async_trait::async_trait;
use thiserror::Error;
// Message Wrapper
#[derive(Debug)]
pub(crate) struct Wrapper<C>
where
C: Component,
{
wrapper: Box<dyn WrapperTrait<C>>,
}
impl<C: Component> Wrapper<C> {
pub(super) async fn handle(&mut self, component: &mut C, ctx: &ComponentContext<C>) -> () {
self.wrapper.handle(component, ctx).await;
}
}
#[async_trait]
pub(super) trait WrapperTrait<C>: Debug + Send
where
C: Component,
{
async fn handle(&mut self, component: &mut C, ctx: &ComponentContext<C>) -> ();
}
#[async_trait]
impl<C, M> WrapperTrait<C> for Option<M>
where
C: Component + Handler<M>,
M: Debug + Send + 'static,
{
async fn handle(&mut self, component: &mut C, ctx: &ComponentContext<C>) -> () {
if let Some(message) = self.take() {
component.handle(message, ctx).await;
}
}
}
pub(crate) fn wrap<C, M>(message: M) -> Wrapper<C>
where
C: Component + Handler<M>,
M: Debug + Send + 'static,
{
Wrapper {
wrapper: Box::new(Some(message)),
}
}
// Sender
pub(crate) struct Sender<C>
where
C: Component + Send + 'static,
{
pub(super) sender: tokio::sync::mpsc::Sender<Wrapper<C>>,
}
impl<C> Sender<C>
where
C: Component + Send + 'static,
{
pub(super) fn new(sender: tokio::sync::mpsc::Sender<Wrapper<C>>) -> Self {
Sender { sender }
}
pub(crate) async fn send<M>(&self, message: M) -> Result<(), ChannelError>
where
C: Component + Handler<M>,
M: Debug + Send + 'static,
{
let res = self.sender.send(wrap(message)).await;
match res {
Ok(_) => Ok(()),
Err(_) => Err(ChannelError::SendError),
}
}
}
impl<C> Clone for Sender<C>
where
C: Component,
{
fn clone(&self) -> Self {
Sender {
sender: self.sender.clone(),
}
}
}
// Reciever Traits
#[async_trait]
pub(crate) trait Receiver<M>: Send + Sync + ReceiverClone<M> {
async fn send(&self, message: M) -> Result<(), ChannelError>;
}
trait ReceiverClone<M> {
fn clone_box(&self) -> Box<dyn Receiver<M>>;
}
impl<M> Clone for Box<dyn Receiver<M>> {
fn clone(&self) -> Box<dyn Receiver<M>> {
self.clone_box()
}
}
impl<T, M> ReceiverClone<M> for T
where
T: 'static + Receiver<M> + Clone,
{
fn clone_box(&self) -> Box<dyn Receiver<M>> {
Box::new(self.clone())
}
}
// Reciever Impls
pub(super) struct ReceiverImpl<C>
where
C: Component,
{
pub(super) sender: tokio::sync::mpsc::Sender<Wrapper<C>>,
}
impl<C> Clone for ReceiverImpl<C>
where
C: Component,
{
fn clone(&self) -> Self {
ReceiverImpl {
sender: self.sender.clone(),
}
}
}
impl<C> ReceiverImpl<C>
where
C: Component,
{
pub(super) fn new(sender: tokio::sync::mpsc::Sender<Wrapper<C>>) -> Self {
ReceiverImpl { sender }
}
}
#[async_trait]
impl<C, M> Receiver<M> for ReceiverImpl<C>
where
C: Component + Handler<M>,
M: Send + Debug + 'static,
{
async fn send(&self, message: M) -> Result<(), ChannelError> {
let res = self.sender.send(wrap(message)).await;
match res {
Ok(_) => Ok(()),
Err(_) => Err(ChannelError::SendError),
}
}
}
// Errors
#[derive(Error, Debug)]
pub enum ChannelError {
#[error("Failed to send message")]
SendError,
}
|