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]]
name = "bromine"
version = "0.15.0"
version = "0.15.1"
dependencies = [
"async-trait",
"bincode",

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

@ -122,6 +122,7 @@ impl EventSendPayload for () {
#[cfg(feature = "serialize")]
mod serde_payload {
use super::DynamicSerializer;
use crate::context::Context;
use crate::payload::EventReceivePayload;
use crate::prelude::{EventSendPayload, IPCResult};
use byteorder::ReadBytesExt;
@ -185,6 +186,14 @@ mod serde_payload {
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")]

@ -3,8 +3,6 @@ use crate::event::Event;
use crate::ipc::stream_emitter::StreamEmitter;
use futures::future;
use futures::future::Either;
#[cfg(feature = "serialize")]
use serde::Serialize;
use std::collections::HashMap;
use std::mem;
use std::ops::{Deref, DerefMut};
@ -108,7 +106,7 @@ impl Context {
}
#[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)
}

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

Loading…
Cancel
Save