-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathreceiver.rs
79 lines (70 loc) · 1.84 KB
/
receiver.rs
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
use super::{Component, ComponentSender, Handler, Message};
use async_trait::async_trait;
use chroma_error::{ChromaError, ErrorCodes};
use std::fmt::Debug;
use thiserror::Error;
/// A ReceiverForMessage is generic over a message type, and useful if you want to send a given message type to any component that can handle it.
#[async_trait]
pub(crate) trait ReceiverForMessage<M>:
Send + Sync + Debug + ReceiverForMessageClone<M>
{
async fn send(
&self,
message: M,
tracing_context: Option<tracing::Span>,
) -> Result<(), ChannelError>;
}
pub(crate) trait ReceiverForMessageClone<M> {
fn clone_box(&self) -> Box<dyn ReceiverForMessage<M>>;
}
impl<M> Clone for Box<dyn ReceiverForMessage<M>> {
fn clone(&self) -> Box<dyn ReceiverForMessage<M>> {
self.clone_box()
}
}
impl<T, M> ReceiverForMessageClone<M> for T
where
T: 'static + ReceiverForMessage<M> + Clone,
{
fn clone_box(&self) -> Box<dyn ReceiverForMessage<M>> {
Box::new(self.clone())
}
}
#[async_trait]
impl<C, M> ReceiverForMessage<M> for ComponentSender<C>
where
C: Component + Handler<M>,
M: Message,
{
async fn send(
&self,
message: M,
tracing_context: Option<tracing::Span>,
) -> Result<(), ChannelError> {
self.wrap_and_send(message, tracing_context).await
}
}
// Errors
#[derive(Error, Debug)]
pub enum ChannelError {
#[error("Failed to send message")]
SendError,
}
impl ChromaError for ChannelError {
fn code(&self) -> ErrorCodes {
ErrorCodes::Internal
}
}
#[derive(Error, Debug, PartialEq)]
#[allow(dead_code)]
pub enum RequestError {
#[error("Failed to send request")]
SendError,
#[error("Failed to receive response")]
ReceiveError,
}
impl ChromaError for RequestError {
fn code(&self) -> ErrorCodes {
ErrorCodes::Internal
}
}