aboutsummaryrefslogtreecommitdiff
path: root/src/message.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/message.rs')
-rw-r--r--src/message.rs54
1 files changed, 17 insertions, 37 deletions
diff --git a/src/message.rs b/src/message.rs
index f92eb8c..22cae6a 100644
--- a/src/message.rs
+++ b/src/message.rs
@@ -1,4 +1,3 @@
-use async_trait::async_trait;
use bytes::Bytes;
use serde::{Deserialize, Serialize};
@@ -43,6 +42,10 @@ pub trait Message: SerializeMessage + Send + Sync {
}
/// A trait for de/serializing messages, with possible associated stream.
+/// This is default-implemented by anything that can already be serialized
+/// and deserialized. Adapters are provided that implement this for
+/// adding a body, either from a fixed Bytes buffer (which allows the thing
+/// to be Clone), or from a streaming byte stream.
pub trait SerializeMessage: Sized {
type SerializableSelf: Serialize + for<'de> Deserialize<'de> + Send;
@@ -53,38 +56,9 @@ pub trait SerializeMessage: Sized {
// ----
-impl<T, E> SerializeMessage for Result<T, E>
-where
- T: SerializeMessage + Send,
- E: Serialize + for<'de> Deserialize<'de> + Send,
-{
- type SerializableSelf = Result<T::SerializableSelf, E>;
-
- fn into_parts(self) -> (Self::SerializableSelf, Option<ByteStream>) {
- match self {
- Ok(ok) => {
- let (msg, stream) = ok.into_parts();
- (Ok(msg), stream)
- }
- Err(err) => (Err(err), None),
- }
- }
-
- fn from_parts(ser_self: Self::SerializableSelf, stream: ByteStream) -> Self {
- match ser_self {
- Ok(ok) => Ok(T::from_parts(ok, stream)),
- Err(err) => Err(err),
- }
- }
-}
-
-// ---
-
-pub trait SimpleMessage: Serialize + for<'de> Deserialize<'de> + Clone + Send + Sync {}
-
impl<T> SerializeMessage for T
where
- T: SimpleMessage,
+ T: Serialize + for<'de> Deserialize<'de> + Send,
{
type SerializableSelf = Self;
fn into_parts(self) -> (Self::SerializableSelf, Option<ByteStream>) {
@@ -97,12 +71,15 @@ where
}
}
-impl SimpleMessage for () {}
-
-impl<T: SimpleMessage> SimpleMessage for std::sync::Arc<T> {}
-
// ----
+/// An adapter that adds a body from a fixed Bytes buffer to a serializable message,
+/// implementing the SerializeMessage trait. This allows for the SerializeMessage object
+/// to be cloned, which is usefull for requests that must be sent to multiple servers.
+/// Note that cloning the body is cheap thanks to Bytes; make sure that your serializable
+/// part is also easily clonable (e.g. by wrapping it in an Arc).
+/// Note that this CANNOT be used for a response type, as it cannot be reconstructed
+/// from a remote stream.
#[derive(Clone)]
pub struct WithFixedBody<T: Serialize + for<'de> Deserialize<'de> + Clone + Send + 'static>(
pub T,
@@ -123,11 +100,14 @@ where
)
}
- fn from_parts(ser_self: Self::SerializableSelf, stream: ByteStream) -> Self {
- panic!("Cannot reconstruct a WithFixedBody type from parts");
+ fn from_parts(_ser_self: Self::SerializableSelf, _stream: ByteStream) -> Self {
+ panic!("Cannot use a WithFixedBody as a response type");
}
}
+/// An adapter that adds a body from a ByteStream. This is usefull for receiving
+/// responses to requests that contain attached byte streams. This type is
+/// not clonable.
pub struct WithStreamingBody<T: Serialize + for<'de> Deserialize<'de> + Send>(
pub T,
pub ByteStream,