From 53651218773d69ab65cf06547d27f5d3fc1988cb Mon Sep 17 00:00:00 2001 From: ChillFish8 Date: Sun, 3 Sep 2023 15:35:04 +0100 Subject: [PATCH] Other cleanup --- datacake-rpc/src/client.rs | 9 +++-- datacake-rpc/src/net/client.rs | 6 +--- datacake-rpc/src/request.rs | 61 +++++++--------------------------- 3 files changed, 17 insertions(+), 59 deletions(-) diff --git a/datacake-rpc/src/client.rs b/datacake-rpc/src/client.rs index 84cc37f..8055909 100644 --- a/datacake-rpc/src/client.rs +++ b/datacake-rpc/src/client.rs @@ -1,4 +1,3 @@ -use std::borrow::Cow; use std::marker::PhantomData; use std::time::Duration; @@ -140,8 +139,8 @@ where >::Reply: RequestContents + TryIntoBody, { let metadata = MessageMetadata { - service_name: Cow::Borrowed(::service_name()), - path: Cow::Borrowed(>::path()), + service_name: ::service_name(), + path: >::path(), }; let body = msg.try_as_body()?; @@ -164,8 +163,8 @@ where >::Reply: RequestContents + TryIntoBody, { let metadata = MessageMetadata { - service_name: Cow::Borrowed(::service_name()), - path: Cow::Borrowed(>::path()), + service_name: ::service_name(), + path: >::path(), }; let body = msg.try_into_body()?; diff --git a/datacake-rpc/src/net/client.rs b/datacake-rpc/src/net/client.rs index 9ca4664..657fe28 100644 --- a/datacake-rpc/src/net/client.rs +++ b/datacake-rpc/src/net/client.rs @@ -61,11 +61,7 @@ impl Channel { metadata: MessageMetadata, msg: Body, ) -> Result, Error> { - let uri = format!( - "http://{}{}", - self.remote_addr, - crate::to_uri_path(&metadata.service_name, &metadata.path), - ); + let uri = format!("http://{}{}", self.remote_addr, metadata.to_uri_path(),); let request = Request::builder() .method(Method::POST) .uri(uri) diff --git a/datacake-rpc/src/request.rs b/datacake-rpc/src/request.rs index 10aa7f9..9515fd9 100644 --- a/datacake-rpc/src/request.rs +++ b/datacake-rpc/src/request.rs @@ -1,10 +1,9 @@ -use std::borrow::Cow; use std::fmt::{Debug, Formatter}; use std::net::SocketAddr; use std::ops::Deref; use async_trait::async_trait; -use rkyv::{Archive, Deserialize, Serialize}; +use rkyv::Archive; use crate::rkyv_tooling::DataView; use crate::{Body, Status}; @@ -48,17 +47,21 @@ where } } -#[repr(C)] -#[derive(Serialize, Deserialize, Archive, PartialEq)] +#[derive(PartialEq)] #[cfg_attr(test, derive(Debug))] -#[archive(check_bytes)] pub struct MessageMetadata { - #[with(rkyv::with::AsOwned)] /// The name of the service being targeted. - pub(crate) service_name: Cow<'static, str>, - #[with(rkyv::with::AsOwned)] + pub(crate) service_name: &'static str, /// The message name/path. - pub(crate) path: Cow<'static, str>, + pub(crate) path: &'static str, +} + +impl MessageMetadata { + #[inline] + /// Produces a uri path for the metadata. + pub(crate) fn to_uri_path(&self) -> String { + crate::to_uri_path(self.service_name, self.path) + } } /// A zero-copy view of the message data and any additional metadata provided @@ -157,43 +160,3 @@ where Self::new(addr, contents) } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_metadata() { - let meta = MessageMetadata { - service_name: Cow::Borrowed("test"), - path: Cow::Borrowed("demo"), - }; - - let bytes = rkyv::to_bytes::<_, 1024>(&meta).expect("Serialize"); - let copy: MessageMetadata = rkyv::from_bytes(&bytes).expect("Deserialize"); - assert_eq!(meta, copy, "Deserialized value should match"); - } - - #[test] - fn test_request() { - let msg = MessageMetadata { - service_name: Cow::Borrowed("test"), - path: Cow::Borrowed("demo"), - }; - - let addr = "127.0.0.1:8000".parse().unwrap(); - let mut bytes = rkyv::to_bytes::<_, 1024>(&msg).expect("Serialize"); - let checksum = crc32fast::hash(&bytes); - bytes.extend_from_slice(&checksum.to_le_bytes()); - - let view: DataView = - DataView::using(bytes).expect("Create view"); - let req = Request::::new(addr, view); - assert_eq!(req.remote_addr(), addr, "Remote addr should match."); - assert_eq!( - req.to_owned().unwrap(), - msg, - "Deserialized value should match." - ); - } -}