chore: fix

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-09-24 01:14:50 +02:00
parent ca532681e5
commit 8a648c98cc
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
10 changed files with 24 additions and 24 deletions

View File

@ -1,19 +1,19 @@
use criterion::{criterion_group, criterion_main, Criterion}; use criterion::{criterion_group, criterion_main, Criterion};
use crunch_envelope::{unwrap, wrap}; use crunch_envelope::{unwrap, wrap};
fn envelope_capnp_benchmark(content: &[u8]) -> () { fn envelope_capnp_benchmark(content: &[u8]) {
let out = wrap("some-domain", "some-entity", content); let out = wrap("some-domain", "some-entity", content);
let _ = unwrap(&out).expect("to be able to unwrap capnp message"); let _ = unwrap(&out).expect("to be able to unwrap capnp message");
} }
fn envelope_json_benchmark(content: &[u8]) -> () { fn envelope_json_benchmark(content: &[u8]) {
let out = crunch_envelope::json::wrap("some-domain", "some-entity", content); let out = crunch_envelope::json::wrap("some-domain", "some-entity", content);
let _ = crunch_envelope::json::unwrap(&out).expect("to be able to unwrap capnp message"); let _ = crunch_envelope::json::unwrap(&out).expect("to be able to unwrap capnp message");
} }
fn envelope_proto_benchmark(content: &[u8]) -> () { fn envelope_proto_benchmark(content: &[u8]) {
let out = crunch_envelope::proto::wrap("some-domain", "some-entity", content); let out = crunch_envelope::proto::wrap("some-domain", "some-entity", content);
let _ = crunch_envelope::proto::unwrap(&out).expect("to be able to unwrap capnp message"); let _ = crunch_envelope::proto::unwrap(&out).expect("to be able to unwrap capnp message");

View File

@ -16,18 +16,18 @@ pub struct Metadata {
} }
pub fn wrap<'a>(domain: &'a str, entity: &'a str, content: &'a [u8]) -> Vec<u8> { pub fn wrap<'a>(domain: &'a str, entity: &'a str, content: &'a [u8]) -> Vec<u8> {
let output = serde_json::to_vec(&Envelope {
serde_json::to_vec(&Envelope {
content: general_purpose::URL_SAFE_NO_PAD.encode(content), content: general_purpose::URL_SAFE_NO_PAD.encode(content),
metadata: Metadata { metadata: Metadata {
domain: domain.to_string(), domain: domain.to_string(),
entity: entity.to_string(), entity: entity.to_string(),
}, },
}) })
.unwrap(); .unwrap()
output
} }
pub fn unwrap<'a>(message: &'a [u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> { pub fn unwrap(message: &[u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> {
let envelope: Envelope = serde_json::from_slice(message).map_err(EnvelopeError::JsonError)?; let envelope: Envelope = serde_json::from_slice(message).map_err(EnvelopeError::JsonError)?;
Ok(( Ok((

View File

@ -47,9 +47,9 @@ pub fn wrap<'a>(domain: &'a str, entity: &'a str, content: &'a [u8]) -> Vec<u8>
metadata.set_domain(domain); metadata.set_domain(domain);
metadata.set_entity(entity); metadata.set_entity(entity);
let output = serialize::write_message_to_words(&builder);
return output; serialize::write_message_to_words(&builder)
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -59,7 +59,7 @@ pub struct Metadata {
entity: String, entity: String,
} }
pub fn unwrap<'a>(message: &'a [u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> { pub fn unwrap(message: &[u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> {
let mut message = message; let mut message = message;
let message_builder = let message_builder =
serialize::read_message_from_flat_slice(&mut message, ReaderOptions::new()) serialize::read_message_from_flat_slice(&mut message, ReaderOptions::new())

View File

@ -17,7 +17,7 @@ pub fn wrap<'a>(domain: &'a str, entity: &'a str, content: &'a [u8]) -> Vec<u8>
out.encode_to_vec() out.encode_to_vec()
} }
pub fn unwrap<'a>(message: &'a [u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> { pub fn unwrap(message: &[u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> {
let out = Envelope::decode(message).map_err(EnvelopeError::ProtoError)?; let out = Envelope::decode(message).map_err(EnvelopeError::ProtoError)?;
Ok(( Ok((

View File

@ -28,7 +28,7 @@ impl InMemoryTransport {
// Possibly create a trait register handle instead, as this requires a write and then read. It may not matter for in memory though // Possibly create a trait register handle instead, as this requires a write and then read. It may not matter for in memory though
let mut events = self.events.write().await; let mut events = self.events.write().await;
if let None = events.get(&transport_key) { if events.get(&transport_key).is_none() {
let (sender, mut receiver) = tokio::sync::broadcast::channel(100); let (sender, mut receiver) = tokio::sync::broadcast::channel(100);
events.insert(transport_key.clone(), sender); events.insert(transport_key.clone(), sender);
tokio::spawn(async move { tokio::spawn(async move {
@ -64,7 +64,7 @@ impl Transport for InMemoryTransport {
.expect("transport to be available, as we just created it"); .expect("transport to be available, as we just created it");
sender sender
.send(TransportEnvelope { .send(TransportEnvelope {
info: event_info.clone(), info: *event_info,
content, content,
}) })
.map_err(|e| anyhow::anyhow!(e.to_string())) .map_err(|e| anyhow::anyhow!(e.to_string()))

View File

@ -1,7 +1,7 @@
use std::{fmt::Display, sync::Arc}; use std::{fmt::Display};
use async_trait::async_trait; use async_trait::async_trait;
use errors::{DeserializeError, PersistenceError, SerializeError, TransportError}; use errors::{DeserializeError, PersistenceError, SerializeError};
#[async_trait] #[async_trait]
pub trait Persistence { pub trait Persistence {

View File

@ -33,7 +33,7 @@ impl crunch_traits::Persistence for InMemoryPersistence {
let msg = crunch_envelope::proto::wrap(event_info.domain, event_info.entity_type, &content); let msg = crunch_envelope::proto::wrap(event_info.domain, event_info.entity_type, &content);
let msg = Msg { let msg = Msg {
id: uuid::Uuid::new_v4().to_string(), id: uuid::Uuid::new_v4().to_string(),
info: event_info.clone(), info: *event_info,
msg, msg,
state: MsgState::Pending, state: MsgState::Pending,
}; };
@ -61,8 +61,7 @@ impl crunch_traits::Persistence for InMemoryPersistence {
.read() .read()
.await .await
.get(event_id) .get(event_id)
.filter(|m| m.state == MsgState::Pending) .filter(|m| m.state == MsgState::Pending).cloned()
.map(|m| m.clone())
.map(|m| (m.info, m.msg))) .map(|m| (m.info, m.msg)))
} }

View File

@ -131,6 +131,7 @@ pub mod builder {
.clone(); .clone();
} }
#[allow(unreachable_code)]
Self { Self {
persistence: None, persistence: None,
transport: None, transport: None,

View File

@ -1,4 +1,4 @@
use crunch_traits::{Event, EventInfo}; use crunch_traits::{Event};
use futures::StreamExt; use futures::StreamExt;
use crate::{errors, Transport}; use crate::{errors, Transport};

View File

@ -1,6 +1,6 @@
fn main() { fn main() {
#[no_mangle] #[no_mangle]
fn envelope_capnp_benchmark(content: &[u8]) -> () { fn envelope_capnp_benchmark(content: &[u8]) {
let out = crunch_envelope::wrap("some-domain", "some-entity", content); let out = crunch_envelope::wrap("some-domain", "some-entity", content);
let out = crunch_envelope::unwrap(&out).expect("to be able to unwrap capnp message"); let out = crunch_envelope::unwrap(&out).expect("to be able to unwrap capnp message");
@ -9,7 +9,7 @@ fn main() {
} }
#[no_mangle] #[no_mangle]
fn envelope_json_benchmark(content: &[u8]) -> () { fn envelope_json_benchmark(content: &[u8]) {
let out = crunch_envelope::json::wrap("some-domain", "some-entity", content); let out = crunch_envelope::json::wrap("some-domain", "some-entity", content);
let out = crunch_envelope::json::unwrap(&out).expect("to be able to unwrap capnp message"); let out = crunch_envelope::json::unwrap(&out).expect("to be able to unwrap capnp message");
@ -19,9 +19,9 @@ fn main() {
let large_content: [u8; 1000000] = [0; 1000000]; let large_content: [u8; 1000000] = [0; 1000000];
_ = envelope_capnp_benchmark(&large_content); envelope_capnp_benchmark(&large_content);;
_ = envelope_json_benchmark(&large_content); envelope_json_benchmark(&large_content);;
println!("done") println!("done")
} }