Skip to content

Commit

Permalink
📝 use bitcode instead of bincode
Browse files Browse the repository at this point in the history
  • Loading branch information
Xudong-Huang committed Aug 9, 2024
1 parent a150624 commit 6931cb7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ description = "RPC framework for Rust based on coroutine."
may = "0.3"
log = "0.4"
bytes = "1"
bincode = "1"
bitcode = "0.6"
byteorder = "1"
thiserror = "1"
co_managed = "0.1"
may_waiter = "0.1"
serde = { version = "1", features = ["derive"] }
may_rpc_derive = { path = "./may_rpc_derive", version = "0.1" }

[dev-dependencies]
Expand Down
19 changes: 9 additions & 10 deletions may_rpc_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,13 @@ impl Parse for RpcMethod {
/// Adds the following annotations to the annotated item:
///
/// ```rust
/// #[derive(may_rpc::serde::Serialize, may_rpc::serde::Deserialize)]
/// #[serde(crate = "may_rpc::serde")]
/// #[derive(may_rpc::bitcode::Encode, may_rpc::bitcode::Decode)]
/// # struct Foo;
/// ```
#[proc_macro_attribute]
pub fn derive_serde(_attr: TokenStream, item: TokenStream) -> TokenStream {
let mut gen: proc_macro2::TokenStream = quote! {
#[derive(may_rpc::serde::Serialize, may_rpc::serde::Deserialize)]
#[serde(crate = "may_rpc::serde")]
#[derive(may_rpc::bitcode::Encode, may_rpc::bitcode::Decode)]
};
gen.extend(proc_macro2::TokenStream::from(item));
proc_macro::TokenStream::from(gen)
Expand Down Expand Up @@ -192,8 +190,7 @@ pub fn service(attr: TokenStream, input: TokenStream) -> TokenStream {
let args: &[&[PatType]] = &rpcs.iter().map(|rpc| &*rpc.args).collect::<Vec<_>>();
let derive_serialize = {
quote! {
#[derive(may_rpc::serde::Serialize, may_rpc::serde::Deserialize)]
#[serde(crate = "may_rpc::serde")]
#[derive(may_rpc::bitcode::Encode, may_rpc::bitcode::Decode)]
}
};

Expand Down Expand Up @@ -299,10 +296,11 @@ impl<'a> ServiceGenerator<'a> {
#vis trait #dispatch_service_indent: #service_ident + std::panic::RefUnwindSafe
{
fn dispatch_req(&self, req: #request_ident, rsp: &mut may_rpc::RspBuf) -> Result<(), may_rpc::WireError> {
use std::io::Write;
match req {
#(
#request_ident::#camel_case_idents{ #( #arg_pats ),* } => match std::panic::catch_unwind(|| self.#method_idents(#( #arg_pats ),*)) {
Ok(ret) => may_rpc::bincode::serialize_into(rsp, &ret).map_err(|e| may_rpc::WireError::ServerSerialize(e.to_string())),
Ok(ret) => rsp.write_all(&may_rpc::bitcode::encode(&ret)).map_err(|e| may_rpc::WireError::ServerSerialize(e.to_string())),
Err(_) => Err(may_rpc::WireError::Status("rpc panicked in server!".to_owned())),
}
)*
Expand Down Expand Up @@ -391,17 +389,18 @@ impl<'a> ServiceGenerator<'a> {
#[allow(unused)]
#( #method_attrs )*
#vis fn #method_idents(&self, #( #args ),*) -> Result<#return_types, may_rpc::Error> {
use std::io::Write;
use may_rpc::Client;
let mut req = may_rpc::ReqBuf::new();
// serialize the request
let request = #request_ident::#camel_case_idents { #( #arg_pats ),* };
may_rpc::bincode::serialize_into(&mut req, &request)
req.write_all(&may_rpc::bitcode::encode(&request))
.map_err(|e| may_rpc::Error::ClientSerialize(e.to_string()))?;
// call the server
let rsp_frame = self.transport.call_service(req)?;
let rsp = rsp_frame.decode_rsp()?;
// deserialized the response
may_rpc::bincode::deserialize(rsp)
may_rpc::bitcode::decode(rsp)
.map_err(|e| may_rpc::Error::ClientDeserialize(e.to_string()))
}
)*
Expand Down Expand Up @@ -479,7 +478,7 @@ pub fn derive_rpc_server(input: TokenStream) -> TokenStream {
fn service(&self, req: &[u8], rsp: &mut may_rpc::RspBuf) -> Result<(), may_rpc::WireError> {
use #service;
// deserialize the request
let request: #service_request = may_rpc::bincode::deserialize(req)
let request: #service_request = may_rpc::bitcode::decode(req)
.map_err(|e| may_rpc::WireError::ServerDeserialize(e.to_string()))?;
// get the dispatch_fn
self.dispatch_req(request, rsp)
Expand Down
2 changes: 1 addition & 1 deletion may_rpc_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
log = "0.4"
may = "0.3"
anyhow = "1"
bincode = "1"
bitcode = "0.6"
env_logger = "0.11"
serde_json = "1"
serde = { version = "1", features = ["derive"] }
Expand Down
22 changes: 12 additions & 10 deletions may_rpc_test/src/test_hello_foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub trait Hello {
fn add(&self, x: u32, y: u32) -> u32;
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
pub enum HelloRequest {
Echo { data: String },
Add { x: u32, y: u32 },
Expand Down Expand Up @@ -31,13 +31,12 @@ impl<S: may_rpc::StreamExt> HelloClient<S> {
let mut req = may_rpc::ReqBuf::new();
// serialize the request
let request = HelloRequest::Echo { data };
bincode::serialize_into(&mut req, &request)
.map_err(|e| may_rpc::Error::ClientSerialize(e.to_string()))?;
req.write_all(&bitcode::encode(&request)).unwrap();
// call the server
let rsp_frame = self.transport.call_service(req)?;
let rsp = rsp_frame.decode_rsp()?;
// deserialized the response
bincode::deserialize(rsp).map_err(|e| may_rpc::Error::ClientDeserialize(e.to_string()))
bitcode::decode(rsp).map_err(|e| may_rpc::Error::ClientDeserialize(e.to_string()))
}

pub fn add(&self, x: u32, y: u32) -> Result<u32, may_rpc::Error> {
Expand All @@ -46,13 +45,12 @@ impl<S: may_rpc::StreamExt> HelloClient<S> {
let mut req = may_rpc::ReqBuf::new();
// serialize the request
let request = HelloRequest::Add { x, y };
bincode::serialize_into(&mut req, &request)
.map_err(|e| may_rpc::Error::ClientSerialize(e.to_string()))?;
req.write_all(&bitcode::encode(&request)).unwrap();
// call the server
let rsp_frame = self.transport.call_service(req)?;
let rsp = rsp_frame.decode_rsp()?;
// deserialized the response
bincode::deserialize(rsp).map_err(|e| may_rpc::Error::ClientDeserialize(e.to_string()))
bitcode::decode(rsp).map_err(|e| may_rpc::Error::ClientDeserialize(e.to_string()))
}
}

Expand All @@ -65,14 +63,16 @@ pub trait HelloServiceDispatch: Hello + std::panic::RefUnwindSafe {
// dispatch call the service
match request {
HelloRequest::Echo { data } => match std::panic::catch_unwind(|| self.echo(data)) {
Ok(ret) => bincode::serialize_into(rsp, &ret)
Ok(ret) => rsp
.write_all(&bitcode::encode(&ret))
.map_err(|e| may_rpc::WireError::ServerSerialize(e.to_string())),
Err(_) => Err(may_rpc::WireError::Status(
"rpc panicked in server!".to_owned(),
)),
},
HelloRequest::Add { x, y } => match std::panic::catch_unwind(|| self.add(x, y)) {
Ok(ret) => bincode::serialize_into(rsp, &ret)
Ok(ret) => rsp
.write_all(&bitcode::encode(&ret))
.map_err(|e| may_rpc::WireError::ServerSerialize(e.to_string())),
Err(_) => Err(may_rpc::WireError::Status(
"rpc panicked in server!".to_owned(),
Expand Down Expand Up @@ -103,7 +103,7 @@ mod server {
use super::HelloServiceDispatch;

// deserialize the request
let request: HelloRequest = bincode::deserialize(req)
let request: HelloRequest = bitcode::decode(req)
.map_err(|e| may_rpc::WireError::ServerDeserialize(e.to_string()))?;

log::info!("request = {:?}", request);
Expand All @@ -114,4 +114,6 @@ mod server {
}
}

use std::io::Write;

pub use server::HelloService;
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ pub use conetty::{
};
pub use may_rpc_derive::{service, Server};

pub use bincode;
pub use serde;
pub use bitcode;

0 comments on commit 6931cb7

Please sign in to comment.