Add IntoSerdePayload trait

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/27/head
trivernis 3 years ago
parent 4f1884f2af
commit f2606b4704
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

2
Cargo.lock generated

@ -93,7 +93,7 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "bromine" name = "bromine"
version = "0.15.0" version = "0.15.1"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"bincode", "bincode",

@ -1,6 +1,6 @@
[package] [package]
name = "bromine" name = "bromine"
version = "0.15.0" version = "0.15.1"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"

@ -122,6 +122,7 @@ impl EventSendPayload for () {
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
mod serde_payload { mod serde_payload {
use super::DynamicSerializer; use super::DynamicSerializer;
use crate::context::Context;
use crate::payload::EventReceivePayload; use crate::payload::EventReceivePayload;
use crate::prelude::{EventSendPayload, IPCResult}; use crate::prelude::{EventSendPayload, IPCResult};
use byteorder::ReadBytesExt; use byteorder::ReadBytesExt;
@ -185,6 +186,14 @@ mod serde_payload {
Ok(Self { serializer, data }) Ok(Self { serializer, data })
} }
} }
pub trait IntoSerdePayload: Sized {
fn into_serde_payload(self, ctx: &Context) -> SerdePayload<Self> {
ctx.create_serde_payload(self)
}
}
impl<T> IntoSerdePayload for T where T: Serialize {}
} }
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]

@ -3,8 +3,6 @@ use crate::event::Event;
use crate::ipc::stream_emitter::StreamEmitter; use crate::ipc::stream_emitter::StreamEmitter;
use futures::future; use futures::future;
use futures::future::Either; use futures::future::Either;
#[cfg(feature = "serialize")]
use serde::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::mem; use std::mem;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
@ -108,7 +106,7 @@ impl Context {
} }
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
pub fn create_serde_payload<T: Serialize>(&self, data: T) -> SerdePayload<T> { pub fn create_serde_payload<T>(&self, data: T) -> SerdePayload<T> {
SerdePayload::new(self.default_serializer.clone(), data) SerdePayload::new(self.default_serializer.clone(), data)
} }

@ -17,7 +17,7 @@ async fn it_sends_payloads() {
string: String::from("Hello World"), string: String::from("Hello World"),
}; };
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
let payload = ctx.create_serde_payload(payload); let payload = payload.into_serde_payload(&ctx);
ctx.emitter.emit("ping", payload).await.unwrap(); ctx.emitter.emit("ping", payload).await.unwrap();
@ -39,7 +39,7 @@ async fn it_receives_payloads() {
string: String::from("Hello World"), string: String::from("Hello World"),
}; };
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
let payload = ctx.create_serde_payload(payload); let payload = payload.into_serde_payload(&ctx);
let reply = ctx let reply = ctx
.emitter .emitter
@ -80,7 +80,7 @@ async fn handle_ping_event(ctx: &Context, event: Event) -> IPCResult<()> {
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
{ {
ctx.emitter ctx.emitter
.emit_response(event.id(), "pong", ctx.create_serde_payload(payload)) .emit_response(event.id(), "pong", payload.into_serde_payload(&ctx))
.await?; .await?;
} }
#[cfg(not(feature = "serialize"))] #[cfg(not(feature = "serialize"))]

Loading…
Cancel
Save