Change internal bytes representation to Bytes object from bytes crate

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/38/head
trivernis 3 years ago
parent ca264abae8
commit ac471d296e
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

3
Cargo.lock generated

@ -93,11 +93,12 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "bromine" name = "bromine"
version = "0.19.0" version = "0.20.0"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"bincode", "bincode",
"byteorder", "byteorder",
"bytes",
"criterion", "criterion",
"crossbeam-utils", "crossbeam-utils",
"futures", "futures",

@ -1,6 +1,6 @@
[package] [package]
name = "bromine" name = "bromine"
version = "0.19.0" version = "0.20.0"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"
@ -31,6 +31,7 @@ trait-bound-typemap = "0.3.3"
rmp-serde = { version = "1.0.0", optional = true } rmp-serde = { version = "1.0.0", optional = true }
bincode = { version = "1.3.3", optional = true } bincode = { version = "1.3.3", optional = true }
serde_json = { version = "1.0.79", optional = true } serde_json = { version = "1.0.79", optional = true }
bytes = "1.1.0"
[dependencies.serde] [dependencies.serde]
optional = true optional = true

@ -1,3 +1,4 @@
use bytes::Bytes;
use criterion::{black_box, BenchmarkId, Throughput}; use criterion::{black_box, BenchmarkId, Throughput};
use criterion::{criterion_group, criterion_main}; use criterion::{criterion_group, criterion_main};
use criterion::{BatchSize, Criterion}; use criterion::{BatchSize, Criterion};
@ -9,17 +10,21 @@ use tokio::runtime::Runtime;
pub const EVENT_NAME: &str = "bench_event"; pub const EVENT_NAME: &str = "bench_event";
fn create_event_bytes_reader(data_size: usize) -> Cursor<Vec<u8>> { fn create_event_bytes_reader(data_size: usize) -> Cursor<Vec<u8>> {
let bytes = Event::initiator(None, EVENT_NAME.to_string(), vec![0u8; data_size]) let bytes = Event::initiator(
None,
EVENT_NAME.to_string(),
Bytes::from(vec![0u8; data_size]),
)
.into_bytes() .into_bytes()
.unwrap(); .unwrap();
Cursor::new(bytes) Cursor::new(bytes.to_vec())
} }
fn event_deserialization(c: &mut Criterion) { fn event_deserialization(c: &mut Criterion) {
let runtime = Runtime::new().unwrap(); let runtime = Runtime::new().unwrap();
let mut group = c.benchmark_group("event_deserialization"); let mut group = c.benchmark_group("event_deserialization");
for size in (0..10) for size in (0..16)
.step_by(2) .step_by(2)
.map(|i| 1024 * 2u32.pow(i as u32) as usize) .map(|i| 1024 * 2u32.pow(i as u32) as usize)
{ {

@ -1,4 +1,5 @@
use bromine::event::Event; use bromine::event::Event;
use bytes::Bytes;
use criterion::{ use criterion::{
black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput, black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput,
}; };
@ -6,13 +7,17 @@ use criterion::{
pub const EVENT_NAME: &str = "bench_event"; pub const EVENT_NAME: &str = "bench_event";
fn create_event(data_size: usize) -> Event { fn create_event(data_size: usize) -> Event {
Event::initiator(None, EVENT_NAME.to_string(), vec![0u8; data_size]) Event::initiator(
None,
EVENT_NAME.to_string(),
Bytes::from(vec![0u8; data_size]),
)
} }
fn event_serialization(c: &mut Criterion) { fn event_serialization(c: &mut Criterion) {
let mut group = c.benchmark_group("event_serialization"); let mut group = c.benchmark_group("event_serialization");
for size in (0..10) for size in (0..16)
.step_by(2) .step_by(2)
.map(|i| 1024 * 2u32.pow(i as u32) as usize) .map(|i| 1024 * 2u32.pow(i as u32) as usize)
{ {

@ -3,6 +3,7 @@ use crate::error::Result;
use crate::payload::{FromPayload, IntoPayload}; use crate::payload::{FromPayload, IntoPayload};
use crate::prelude::{IPCError, IPCResult}; use crate::prelude::{IPCError, IPCResult};
use byteorder::{BigEndian, ReadBytesExt}; use byteorder::{BigEndian, ReadBytesExt};
use bytes::{BufMut, Bytes, BytesMut};
use std::error::Error; use std::error::Error;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::io::Read; use std::io::Read;
@ -29,14 +30,13 @@ impl Display for ErrorEventData {
} }
impl IntoPayload for ErrorEventData { impl IntoPayload for ErrorEventData {
fn into_payload(self, _: &Context) -> IPCResult<Vec<u8>> { fn into_payload(self, _: &Context) -> IPCResult<Bytes> {
let mut buf = Vec::new(); let mut buf = BytesMut::new();
buf.append(&mut self.code.to_be_bytes().to_vec()); buf.put_u16(self.code);
let message_len = self.message.len() as u32; buf.put_u32(self.message.len() as u32);
buf.append(&mut message_len.to_be_bytes().to_vec()); buf.put(Bytes::from(self.message));
buf.append(&mut self.message.into_bytes());
Ok(buf.freeze())
Ok(buf)
} }
} }

@ -2,6 +2,7 @@ use crate::error::{Error, Result};
use crate::events::generate_event_id; use crate::events::generate_event_id;
use crate::events::payload::FromPayload; use crate::events::payload::FromPayload;
use byteorder::{BigEndian, ReadBytesExt}; use byteorder::{BigEndian, ReadBytesExt};
use bytes::{BufMut, Bytes, BytesMut};
use num_enum::{IntoPrimitive, TryFromPrimitive}; use num_enum::{IntoPrimitive, TryFromPrimitive};
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fmt::Debug; use std::fmt::Debug;
@ -16,7 +17,7 @@ pub const FORMAT_VERSION: [u8; 3] = [0, 9, 0];
#[derive(Debug)] #[derive(Debug)]
pub struct Event { pub struct Event {
header: EventHeader, header: EventHeader,
data: Vec<u8>, data: Bytes,
} }
#[derive(Debug)] #[derive(Debug)]
@ -41,21 +42,21 @@ impl Event {
/// Creates a new event that acts as an initiator for further response events /// Creates a new event that acts as an initiator for further response events
#[tracing::instrument(level = "trace", skip(data))] #[tracing::instrument(level = "trace", skip(data))]
#[inline] #[inline]
pub fn initiator(namespace: Option<String>, name: String, data: Vec<u8>) -> Self { pub fn initiator(namespace: Option<String>, name: String, data: Bytes) -> Self {
Self::new(namespace, name, data, None, EventType::Initiator) Self::new(namespace, name, data, None, EventType::Initiator)
} }
/// Creates a new event that is a response to a previous event /// Creates a new event that is a response to a previous event
#[tracing::instrument(level = "trace", skip(data))] #[tracing::instrument(level = "trace", skip(data))]
#[inline] #[inline]
pub fn response(namespace: Option<String>, name: String, data: Vec<u8>, ref_id: u64) -> Self { pub fn response(namespace: Option<String>, name: String, data: Bytes, ref_id: u64) -> Self {
Self::new(namespace, name, data, Some(ref_id), EventType::Response) Self::new(namespace, name, data, Some(ref_id), EventType::Response)
} }
/// Creates a new error event as a response to a previous event /// Creates a new error event as a response to a previous event
#[tracing::instrument(level = "trace", skip(data))] #[tracing::instrument(level = "trace", skip(data))]
#[inline] #[inline]
pub fn error(namespace: Option<String>, name: String, data: Vec<u8>, ref_id: u64) -> Self { pub fn error(namespace: Option<String>, name: String, data: Bytes, ref_id: u64) -> Self {
Self::new(namespace, name, data, Some(ref_id), EventType::Error) Self::new(namespace, name, data, Some(ref_id), EventType::Error)
} }
@ -63,7 +64,7 @@ impl Event {
/// and might contain a final response payload /// and might contain a final response payload
#[tracing::instrument(level = "trace", skip(data))] #[tracing::instrument(level = "trace", skip(data))]
#[inline] #[inline]
pub fn end(namespace: Option<String>, name: String, data: Vec<u8>, ref_id: u64) -> Self { pub fn end(namespace: Option<String>, name: String, data: Bytes, ref_id: u64) -> Self {
Self::new(namespace, name, data, Some(ref_id), EventType::Response) Self::new(namespace, name, data, Some(ref_id), EventType::Response)
} }
@ -72,7 +73,7 @@ impl Event {
pub(crate) fn new( pub(crate) fn new(
namespace: Option<String>, namespace: Option<String>,
name: String, name: String,
data: Vec<u8>, data: Bytes,
ref_id: Option<u64>, ref_id: Option<u64>,
event_type: EventType, event_type: EventType,
) -> Self { ) -> Self {
@ -145,57 +146,59 @@ impl Event {
// additional header fields can be added a the end because when reading they will just be ignored // additional header fields can be added a the end because when reading they will just be ignored
let header: EventHeader = EventHeader::from_read(&mut Cursor::new(header_bytes))?; let header: EventHeader = EventHeader::from_read(&mut Cursor::new(header_bytes))?;
let mut data = vec![0u8; data_length as usize]; let mut buf = vec![0u8; data_length as usize];
reader.read_exact(&mut data).await?; reader.read_exact(&mut buf).await?;
let event = Event { header, data }; let event = Event {
header,
data: Bytes::from(buf),
};
Ok(event) Ok(event)
} }
/// Encodes the event into bytes /// Encodes the event into bytes
#[tracing::instrument(level = "trace", skip(self))] #[tracing::instrument(level = "trace", skip(self))]
pub fn into_bytes(mut self) -> Result<Vec<u8>> { pub fn into_bytes(self) -> Result<Bytes> {
let mut header_bytes = self.header.into_bytes(); let header_bytes = self.header.into_bytes();
let header_length = header_bytes.len() as u16; let header_length = header_bytes.len() as u16;
let data_length = self.data.len(); let data_length = self.data.len();
let total_length = header_length as u64 + data_length as u64; let total_length = header_length as u64 + data_length as u64;
tracing::trace!(total_length, header_length, data_length); tracing::trace!(total_length, header_length, data_length);
let mut buf = Vec::with_capacity(total_length as usize); let mut buf = BytesMut::with_capacity(total_length as usize);
buf.append(&mut total_length.to_be_bytes().to_vec()); buf.put_u64(total_length);
buf.append(&mut header_length.to_be_bytes().to_vec()); buf.put_u16(header_length);
buf.append(&mut header_bytes); buf.put(header_bytes);
buf.append(&mut self.data); buf.put(self.data);
Ok(buf) Ok(buf.freeze())
} }
} }
impl EventHeader { impl EventHeader {
/// Serializes the event header into bytes /// Serializes the event header into bytes
pub fn into_bytes(self) -> Vec<u8> { pub fn into_bytes(self) -> Bytes {
let mut buf = FORMAT_VERSION.to_vec(); let mut buf = BytesMut::with_capacity(256);
buf.append(&mut self.id.to_be_bytes().to_vec()); buf.put_slice(&FORMAT_VERSION);
buf.push(self.event_type.into()); buf.put_u64(self.id);
buf.put_u8(u8::from(self.event_type));
if let Some(ref_id) = self.ref_id { if let Some(ref_id) = self.ref_id {
buf.push(0xFF); buf.put_u8(0xFF);
buf.append(&mut ref_id.to_be_bytes().to_vec()); buf.put_u64(ref_id);
} else { } else {
buf.push(0x00); buf.put_u8(0x00);
} }
if let Some(namespace) = self.namespace { if let Some(namespace) = self.namespace {
let namespace_len = namespace.len() as u16; buf.put_u16(namespace.len() as u16);
buf.append(&mut namespace_len.to_be_bytes().to_vec()); buf.put(Bytes::from(namespace));
buf.append(&mut namespace.into_bytes());
} else { } else {
buf.append(&mut 0u16.to_be_bytes().to_vec()); buf.put_u16(0);
} }
let name_len = self.name.len() as u16; buf.put_u16(self.name.len() as u16);
buf.append(&mut name_len.to_be_bytes().to_vec()); buf.put(Bytes::from(self.name));
buf.append(&mut self.name.into_bytes());
buf buf.freeze()
} }
/// Parses an event header from an async reader /// Parses an event header from an async reader

@ -2,13 +2,14 @@ use crate::error::Result;
use crate::events::event::Event; use crate::events::event::Event;
use crate::ipc::context::Context; use crate::ipc::context::Context;
use crate::payload::{BytePayload, IntoPayload}; use crate::payload::{BytePayload, IntoPayload};
use bytes::Bytes;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use std::future::Future; use std::future::Future;
use std::pin::Pin; use std::pin::Pin;
use std::sync::Arc; use std::sync::Arc;
pub struct Response(Vec<u8>); pub struct Response(Bytes);
impl Response { impl Response {
/// Creates a new response with a given payload /// Creates a new response with a given payload
@ -20,11 +21,11 @@ impl Response {
/// Creates an empty response /// Creates an empty response
pub fn empty() -> Self { pub fn empty() -> Self {
Self(vec![]) Self(Bytes::new())
} }
pub(crate) fn into_byte_payload(self) -> BytePayload { pub(crate) fn into_byte_payload(self) -> BytePayload {
BytePayload::new(self.0) BytePayload::from(self.0)
} }
} }

@ -1,5 +1,6 @@
use crate::prelude::IPCResult; use crate::prelude::IPCResult;
use byteorder::{BigEndian, ReadBytesExt}; use byteorder::{BigEndian, ReadBytesExt};
use bytes::{BufMut, Bytes, BytesMut};
use std::io::Read; use std::io::Read;
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
@ -7,18 +8,13 @@ pub use super::payload_serializer::*;
/// Trait that serializes a type into bytes and can fail /// Trait that serializes a type into bytes and can fail
pub trait TryIntoBytes { pub trait TryIntoBytes {
fn try_into_bytes(self) -> IPCResult<Vec<u8>>; fn try_into_bytes(self) -> IPCResult<Bytes>;
}
/// Trait that serializes a type into bytes and never fails
pub trait IntoBytes {
fn into_bytes(self) -> Vec<u8>;
} }
/// Trait to convert event data into sending bytes /// Trait to convert event data into sending bytes
/// It is implemented for all types that implement Serialize /// It is implemented for all types that implement Serialize
pub trait IntoPayload { pub trait IntoPayload {
fn into_payload(self, ctx: &Context) -> IPCResult<Vec<u8>>; fn into_payload(self, ctx: &Context) -> IPCResult<Bytes>;
} }
/// Trait to get the event data from receiving bytes. /// Trait to get the event data from receiving bytes.
@ -31,25 +27,39 @@ pub trait FromPayload: Sized {
/// serializing them /// serializing them
#[derive(Clone)] #[derive(Clone)]
pub struct BytePayload { pub struct BytePayload {
bytes: Vec<u8>, bytes: Bytes,
} }
impl BytePayload { impl BytePayload {
#[inline] #[inline]
pub fn new(bytes: Vec<u8>) -> Self { pub fn new(bytes: Vec<u8>) -> Self {
Self { bytes } Self {
bytes: Bytes::from(bytes),
}
} }
/// Returns the bytes of the payload /// Returns the bytes as a `Vec<u8>` of the payload
#[inline] #[inline]
pub fn into_inner(self) -> Vec<u8> { pub fn into_inner(self) -> Vec<u8> {
self.bytes.to_vec()
}
/// Returns the bytes struct of the payload
#[inline]
pub fn into_bytes(self) -> Bytes {
self.bytes self.bytes
} }
} }
impl From<Bytes> for BytePayload {
fn from(bytes: Bytes) -> Self {
Self { bytes }
}
}
impl IntoPayload for BytePayload { impl IntoPayload for BytePayload {
#[inline] #[inline]
fn into_payload(self, _: &Context) -> IPCResult<Vec<u8>> { fn into_payload(self, _: &Context) -> IPCResult<Bytes> {
Ok(self.bytes) Ok(self.bytes)
} }
} }
@ -87,20 +97,18 @@ impl<P1, P2> TandemPayload<P1, P2> {
} }
impl<P1: IntoPayload, P2: IntoPayload> IntoPayload for TandemPayload<P1, P2> { impl<P1: IntoPayload, P2: IntoPayload> IntoPayload for TandemPayload<P1, P2> {
fn into_payload(self, ctx: &Context) -> IPCResult<Vec<u8>> { fn into_payload(self, ctx: &Context) -> IPCResult<Bytes> {
let mut p1_bytes = self.load1.into_payload(&ctx)?; let p1_bytes = self.load1.into_payload(&ctx)?;
let mut p2_bytes = self.load2.into_payload(&ctx)?; let p2_bytes = self.load2.into_payload(&ctx)?;
let mut p1_length_bytes = (p1_bytes.len() as u64).to_be_bytes().to_vec(); let mut bytes = BytesMut::with_capacity(p1_bytes.len() + p2_bytes.len() + 16);
let mut p2_length_bytes = (p2_bytes.len() as u64).to_be_bytes().to_vec();
let mut bytes = Vec::new(); bytes.put_u64(p1_bytes.len() as u64);
bytes.append(&mut p1_length_bytes); bytes.put(p1_bytes);
bytes.append(&mut p1_bytes); bytes.put_u64(p2_bytes.len() as u64);
bytes.append(&mut p2_length_bytes); bytes.put(p2_bytes);
bytes.append(&mut p2_bytes);
Ok(bytes) Ok(bytes.freeze())
} }
} }
@ -123,8 +131,8 @@ impl<P1: FromPayload, P2: FromPayload> FromPayload for TandemPayload<P1, P2> {
#[cfg(not(feature = "serialize"))] #[cfg(not(feature = "serialize"))]
impl IntoPayload for () { impl IntoPayload for () {
fn into_payload(self, _: &Context) -> IPCResult<Vec<u8>> { fn into_payload(self, _: &Context) -> IPCResult<Bytes> {
Ok(vec![]) Ok(Bytes::new())
} }
} }
@ -135,6 +143,7 @@ mod serde_payload {
use crate::payload::{FromPayload, TryIntoBytes}; use crate::payload::{FromPayload, TryIntoBytes};
use crate::prelude::{IPCResult, IntoPayload}; use crate::prelude::{IPCResult, IntoPayload};
use byteorder::ReadBytesExt; use byteorder::ReadBytesExt;
use bytes::{BufMut, Bytes, BytesMut};
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
use std::io::Read; use std::io::Read;
@ -168,20 +177,20 @@ mod serde_payload {
} }
impl<T: Serialize> TryIntoBytes for SerdePayload<T> { impl<T: Serialize> TryIntoBytes for SerdePayload<T> {
fn try_into_bytes(self) -> IPCResult<Vec<u8>> { fn try_into_bytes(self) -> IPCResult<Bytes> {
let mut buf = Vec::new(); let mut buf = BytesMut::new();
let mut data_bytes = self.serializer.serialize(self.data)?; let data_bytes = self.serializer.serialize(self.data)?;
let format_id = self.serializer as u8; let format_id = self.serializer as u8;
buf.push(format_id); buf.put_u8(format_id);
buf.append(&mut data_bytes); buf.put(data_bytes);
Ok(buf) Ok(buf.freeze())
} }
} }
impl<T: Serialize> IntoPayload for SerdePayload<T> { impl<T: Serialize> IntoPayload for SerdePayload<T> {
#[inline] #[inline]
fn into_payload(self, _: &Context) -> IPCResult<Vec<u8>> { fn into_payload(self, _: &Context) -> IPCResult<Bytes> {
self.try_into_bytes() self.try_into_bytes()
} }
} }
@ -198,7 +207,7 @@ mod serde_payload {
impl<T: Serialize> IntoPayload for T { impl<T: Serialize> IntoPayload for T {
#[inline] #[inline]
fn into_payload(self, ctx: &Context) -> IPCResult<Vec<u8>> { fn into_payload(self, ctx: &Context) -> IPCResult<Bytes> {
ctx.create_serde_payload(self).into_payload(&ctx) ctx.create_serde_payload(self).into_payload(&ctx)
} }
} }

@ -1,3 +1,4 @@
use bytes::Bytes;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
use std::io::Read; use std::io::Read;
@ -49,7 +50,7 @@ pub enum SerializationError {
UnknownFormat(usize), UnknownFormat(usize),
} }
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum DynamicSerializer { pub enum DynamicSerializer {
Messagepack, Messagepack,
Bincode, Bincode,
@ -109,7 +110,7 @@ impl DynamicSerializer {
} }
} }
pub fn serialize<T: Serialize>(&self, data: T) -> SerializationResult<Vec<u8>> { pub fn serialize<T: Serialize>(&self, data: T) -> SerializationResult<Bytes> {
match self { match self {
#[cfg(feature = "serialize_rmp")] #[cfg(feature = "serialize_rmp")]
DynamicSerializer::Messagepack => serialize_rmp::serialize(data), DynamicSerializer::Messagepack => serialize_rmp::serialize(data),

@ -1,13 +1,14 @@
use crate::payload::SerializationResult; use crate::payload::SerializationResult;
use bytes::Bytes;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
use std::io::Read; use std::io::Read;
#[inline] #[inline]
pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Vec<u8>> { pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Bytes> {
let bytes = bincode::serialize(&data)?; let bytes = bincode::serialize(&data)?;
Ok(bytes) Ok(Bytes::from(bytes))
} }
#[inline] #[inline]

@ -1,13 +1,14 @@
use crate::payload::SerializationResult; use crate::payload::SerializationResult;
use bytes::Bytes;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
use std::io::Read; use std::io::Read;
#[inline] #[inline]
pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Vec<u8>> { pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Bytes> {
let bytes = serde_json::to_vec(&data)?; let bytes = serde_json::to_vec(&data)?;
Ok(bytes) Ok(Bytes::from(bytes))
} }
#[inline] #[inline]

@ -1,13 +1,14 @@
use crate::payload::SerializationResult; use crate::payload::SerializationResult;
use bytes::Bytes;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
use std::io::Read; use std::io::Read;
#[inline] #[inline]
pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Vec<u8>> { pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Bytes> {
let bytes = postcard::to_allocvec(&data)?.to_vec(); let bytes = postcard::to_allocvec(&data)?.to_vec();
Ok(bytes) Ok(Bytes::from(bytes))
} }
#[inline] #[inline]

@ -1,13 +1,14 @@
use crate::payload::SerializationResult; use crate::payload::SerializationResult;
use bytes::Bytes;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
use std::io::Read; use std::io::Read;
#[inline] #[inline]
pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Vec<u8>> { pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Bytes> {
let bytes = rmp_serde::to_vec(&data)?; let bytes = rmp_serde::to_vec(&data)?;
Ok(bytes) Ok(Bytes::from(bytes))
} }
#[inline] #[inline]

@ -37,7 +37,7 @@ impl<P: IntoPayload> EventMetadata<P> {
let payload = self.payload.take().ok_or(Error::InvalidState)?; let payload = self.payload.take().ok_or(Error::InvalidState)?;
let res_id = self.res_id.take().ok_or(Error::InvalidState)?; let res_id = self.res_id.take().ok_or(Error::InvalidState)?;
let event_type = self.event_type.take().ok_or(Error::InvalidState)?; let event_type = self.event_type.take().ok_or(Error::InvalidState)?;
let payload_bytes = payload.into_payload(&ctx)?; let payload_bytes = payload.into_payload(&ctx)?.into();
let event = Event::new( let event = Event::new(
namespace, namespace,

@ -119,6 +119,12 @@ mod macros;
mod namespaces; mod namespaces;
pub mod protocol; pub mod protocol;
/// Reexported for usage in payload implementations
pub use bytes;
/// Reexported for sharing data in context
pub use trait_bound_typemap;
pub use events::error_event; pub use events::error_event;
pub use events::event; pub use events::event;
pub use events::event_handler; pub use events::event_handler;
@ -146,4 +152,5 @@ pub mod prelude {
pub use crate::payload::*; pub use crate::payload::*;
pub use crate::protocol::*; pub use crate::protocol::*;
pub use crate::*; pub use crate::*;
pub use trait_bound_typemap::TypeMap;
} }

@ -3,6 +3,7 @@ use crate::utils::protocol::TestProtocolListener;
use crate::utils::{get_free_port, start_server_and_client}; use crate::utils::{get_free_port, start_server_and_client};
use bromine::prelude::*; use bromine::prelude::*;
use byteorder::ReadBytesExt; use byteorder::ReadBytesExt;
use bytes::Bytes;
use futures::StreamExt; use futures::StreamExt;
use std::io::Read; use std::io::Read;
use std::time::Duration; use std::time::Duration;
@ -66,16 +67,16 @@ async fn handle_stream_event(ctx: &Context, event: Event) -> IPCResult<Response>
pub struct EmptyPayload; pub struct EmptyPayload;
impl IntoPayload for EmptyPayload { impl IntoPayload for EmptyPayload {
fn into_payload(self, _: &Context) -> IPCResult<Vec<u8>> { fn into_payload(self, _: &Context) -> IPCResult<Bytes> {
Ok(vec![]) Ok(Bytes::new())
} }
} }
pub struct NumberPayload(u8); pub struct NumberPayload(u8);
impl IntoPayload for NumberPayload { impl IntoPayload for NumberPayload {
fn into_payload(self, _: &Context) -> IPCResult<Vec<u8>> { fn into_payload(self, _: &Context) -> IPCResult<Bytes> {
Ok(vec![self.0]) Ok(Bytes::from(vec![self.0]))
} }
} }

@ -91,6 +91,7 @@ mod payload_impl {
use bromine::payload::{FromPayload, IntoPayload}; use bromine::payload::{FromPayload, IntoPayload};
use bromine::prelude::IPCResult; use bromine::prelude::IPCResult;
use byteorder::{BigEndian, ReadBytesExt}; use byteorder::{BigEndian, ReadBytesExt};
use bytes::{BufMut, Bytes, BytesMut};
use std::io::Read; use std::io::Read;
pub struct SimplePayload { pub struct SimplePayload {
@ -99,17 +100,13 @@ mod payload_impl {
} }
impl IntoPayload for SimplePayload { impl IntoPayload for SimplePayload {
fn into_payload(self, _: &Context) -> IPCResult<Vec<u8>> { fn into_payload(self, _: &Context) -> IPCResult<Bytes> {
let mut buf = Vec::new(); let mut buf = BytesMut::new();
let string_length = self.string.len() as u16; buf.put_u16(self.string.len() as u16);
let string_length_bytes = string_length.to_be_bytes(); buf.put(Bytes::from(self.string));
buf.append(&mut string_length_bytes.to_vec()); buf.put_u32(self.number);
let mut string_bytes = self.string.into_bytes();
buf.append(&mut string_bytes); Ok(buf.freeze())
let num_bytes = self.number.to_be_bytes();
buf.append(&mut num_bytes.to_vec());
Ok(buf)
} }
} }

@ -2,6 +2,7 @@ mod utils;
use crate::utils::start_server_and_client; use crate::utils::start_server_and_client;
use bromine::prelude::*; use bromine::prelude::*;
use bytes::Bytes;
use std::time::Duration; use std::time::Duration;
use utils::call_counter::*; use utils::call_counter::*;
use utils::get_free_port; use utils::get_free_port;
@ -132,7 +133,7 @@ async fn handle_error_event(ctx: &Context, event: Event) -> IPCResult<Response>
pub struct EmptyPayload; pub struct EmptyPayload;
impl IntoPayload for EmptyPayload { impl IntoPayload for EmptyPayload {
fn into_payload(self, _: &Context) -> IPCResult<Vec<u8>> { fn into_payload(self, _: &Context) -> IPCResult<Bytes> {
Ok(vec![]) Ok(Bytes::new())
} }
} }

Loading…
Cancel
Save