Make async_trait dependency optional
This commit is contained in:
parent
6619db9128
commit
554cb937a9
9 changed files with 66 additions and 29 deletions
9
Cargo.lock
generated
9
Cargo.lock
generated
|
@ -34,9 +34,9 @@ checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
|
|||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.73"
|
||||
version = "0.1.76"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bc00ceb34980c03614e35a3a4e218276a0a824e911d07651cd0d858a51e8c0f0"
|
||||
checksum = "531b97fb4cd3dfdce92c35dedbfdc1f0b9d8091c8ca943d6dae340ef5012d514"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -409,7 +409,7 @@ checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a"
|
|||
|
||||
[[package]]
|
||||
name = "nrpc"
|
||||
version = "0.10.0"
|
||||
version = "1.0.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"bytes",
|
||||
|
@ -419,7 +419,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "nrpc-build"
|
||||
version = "0.10.0"
|
||||
version = "1.0.0"
|
||||
dependencies = [
|
||||
"nrpc",
|
||||
"prettyplease 0.2.12",
|
||||
|
@ -435,7 +435,6 @@ dependencies = [
|
|||
name = "nrpc-codegen-test"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"bytes",
|
||||
"nrpc",
|
||||
"nrpc-build",
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
[workspace]
|
||||
resolver = "2"
|
||||
members = [ "nrpc", "nrpc-*" ]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "nrpc-build"
|
||||
version = "0.10.0"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
repository = "https://github.com/NGnius/nRPC"
|
||||
|
@ -21,4 +21,7 @@ quote = "1.0"
|
|||
syn = "2.0"
|
||||
proc-macro2 = "1.0"
|
||||
|
||||
nrpc = { version = "0.10", path = "../nrpc" }
|
||||
nrpc = { version = "1.0", path = "../nrpc" }
|
||||
|
||||
[features]
|
||||
async-trait = []
|
||||
|
|
|
@ -65,9 +65,24 @@ fn stream_client_type(item_type: &syn::Ident) -> proc_macro2::TokenStream {
|
|||
}
|
||||
}*/
|
||||
|
||||
fn async_fn_prelude() -> proc_macro2::TokenStream {
|
||||
#[cfg(not(feature = "async-trait"))]
|
||||
quote!{ #[allow(async_fn_in_trait)] }
|
||||
#[cfg(feature = "async-trait")]
|
||||
quote!{}
|
||||
}
|
||||
|
||||
fn async_trait_prelude() -> proc_macro2::TokenStream {
|
||||
#[cfg(not(feature = "async-trait"))]
|
||||
quote!{}
|
||||
#[cfg(feature = "async-trait")]
|
||||
quote!{ #[::nrpc::_helpers::async_trait::async_trait] }
|
||||
}
|
||||
|
||||
fn trait_methods_server(descriptors: &Vec<prost_build::Method>) -> proc_macro2::TokenStream {
|
||||
let mut gen_methods = Vec::with_capacity(descriptors.len());
|
||||
let mut gen_method_match_arms = Vec::with_capacity(descriptors.len());
|
||||
let fn_prelude = async_fn_prelude();
|
||||
for descriptor in descriptors {
|
||||
let input_ty = quote::format_ident!("{}", descriptor.input_type);
|
||||
let output_ty = quote::format_ident!("{}", descriptor.output_type);
|
||||
|
@ -78,6 +93,7 @@ fn trait_methods_server(descriptors: &Vec<prost_build::Method>) -> proc_macro2::
|
|||
// no streaming; 1->1
|
||||
gen_methods.push(
|
||||
quote! {
|
||||
#fn_prelude
|
||||
async fn #fn_name(&mut self, input: #input_ty) -> Result<#output_ty, Box<dyn std::error::Error + Send>>;
|
||||
}
|
||||
);
|
||||
|
@ -102,6 +118,7 @@ fn trait_methods_server(descriptors: &Vec<prost_build::Method>) -> proc_macro2::
|
|||
let stream_out_ty = stream_server_type(&output_ty);
|
||||
gen_methods.push(
|
||||
quote! {
|
||||
#fn_prelude
|
||||
async fn #fn_name<'a: 'b>(&mut self, input: #input_ty) -> Result<#stream_out_ty, Box<dyn std::error::Error + Send>>;
|
||||
}
|
||||
);
|
||||
|
@ -133,6 +150,7 @@ fn trait_methods_server(descriptors: &Vec<prost_build::Method>) -> proc_macro2::
|
|||
let stream_in_ty = stream_server_type(&input_ty);
|
||||
gen_methods.push(
|
||||
quote! {
|
||||
#fn_prelude
|
||||
async fn #fn_name<'a: 'b>(&mut self, input: #stream_in_ty) -> Result<#output_ty, Box<dyn std::error::Error + Send>>;
|
||||
}
|
||||
);
|
||||
|
@ -155,6 +173,7 @@ fn trait_methods_server(descriptors: &Vec<prost_build::Method>) -> proc_macro2::
|
|||
let stream_out_ty = stream_server_type(&output_ty);
|
||||
gen_methods.push(
|
||||
quote! {
|
||||
#fn_prelude
|
||||
async fn #fn_name<'a: 'b>(&mut self, input: #stream_in_ty) -> Result<#stream_out_ty, Box<dyn std::error::Error + Send>>;
|
||||
}
|
||||
);
|
||||
|
@ -192,6 +211,7 @@ fn trait_methods_server(descriptors: &Vec<prost_build::Method>) -> proc_macro2::
|
|||
}
|
||||
}*/
|
||||
|
||||
#fn_prelude
|
||||
async fn call<'a: 'b>(
|
||||
&mut self,
|
||||
method: &str,
|
||||
|
@ -340,14 +360,15 @@ impl ServiceGenerator for ProtobufServiceGenerator {
|
|||
let descriptor_str = format!("{}.{}", service.package, service.name);
|
||||
let service_struct_rename = quote::format_ident!("{}Server", service.name);
|
||||
let service_trait_rename = quote::format_ident!("I{}", service.name);
|
||||
let fn_prelude = async_fn_prelude();
|
||||
let trait_prelude = async_trait_prelude();
|
||||
let gen_service = quote! {
|
||||
mod #service_mod_name {
|
||||
use super::*;
|
||||
use ::nrpc::_helpers::async_trait::async_trait;
|
||||
use ::nrpc::_helpers::prost::Message;
|
||||
use ::nrpc::_helpers::futures::StreamExt;
|
||||
|
||||
#[async_trait]
|
||||
#trait_prelude
|
||||
pub trait #service_trait_name<'b>: Send {
|
||||
#service_trait_methods
|
||||
}
|
||||
|
@ -366,12 +387,13 @@ impl ServiceGenerator for ProtobufServiceGenerator {
|
|||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
#trait_prelude
|
||||
impl<'b, T: #service_trait_name<'b>> ::nrpc::ServerService<'b> for #service_struct_name<'b, T> {
|
||||
fn descriptor(&self) -> &'static str {
|
||||
#descriptor_str
|
||||
}
|
||||
|
||||
#fn_prelude
|
||||
async fn call<'a: 'b>(
|
||||
&mut self,
|
||||
method: &str,
|
||||
|
@ -407,7 +429,6 @@ impl ServiceGenerator for ProtobufServiceGenerator {
|
|||
use ::nrpc::_helpers::prost::Message;
|
||||
use ::nrpc::_helpers::futures::StreamExt;
|
||||
|
||||
//#[derive(core::any::Any)]
|
||||
pub struct #service_struct_name<'b, T: ::nrpc::ClientHandler<'b>> {
|
||||
inner: T,
|
||||
_idc: std::marker::PhantomData<&'b ()>,
|
||||
|
|
|
@ -7,10 +7,9 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
prost = "0.11"
|
||||
nrpc = { version = "*", path = "../nrpc" }
|
||||
nrpc = { version = "*", path = "../nrpc", features = [ "async-trait" ] }
|
||||
bytes = "1"
|
||||
async-trait = "0.1"
|
||||
tokio = { version = "*", features = [ "full" ] }
|
||||
|
||||
[build-dependencies]
|
||||
nrpc-build = { version = "*", path = "../nrpc-build" }
|
||||
nrpc-build = { version = "*", path = "../nrpc-build", features = [ "async-trait" ] }
|
||||
|
|
|
@ -130,8 +130,9 @@ async fn main() {
|
|||
|
||||
struct GreeterService;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl helloworld::IGreeter<'_> for GreeterService {
|
||||
#[nrpc::_helpers::async_trait::async_trait]
|
||||
impl <'b> helloworld::IGreeter<'b> for GreeterService {
|
||||
//#[allow(async_fn_in_trait)]
|
||||
async fn say_hello(
|
||||
&mut self,
|
||||
input: helloworld::HelloRequest,
|
||||
|
@ -143,7 +144,8 @@ impl helloworld::IGreeter<'_> for GreeterService {
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
async fn say_hello_one_to_many<'a>(
|
||||
#[allow(async_fn_in_trait)]
|
||||
async fn say_hello_one_to_many<'a: 'b>(
|
||||
&mut self,
|
||||
input: helloworld::HelloRequest,
|
||||
) -> Result<
|
||||
|
@ -157,7 +159,8 @@ impl helloworld::IGreeter<'_> for GreeterService {
|
|||
Ok(Box::new(::nrpc::VecStream::from_iter([(); 3].iter().map(move |_| Ok(result.clone())))))
|
||||
}
|
||||
|
||||
async fn say_hello_many_to_one<'a>(
|
||||
#[allow(async_fn_in_trait)]
|
||||
async fn say_hello_many_to_one<'a: 'b>(
|
||||
&mut self,
|
||||
mut input: ::nrpc::ServiceServerStream<'a, helloworld::HelloRequest>,
|
||||
) -> Result<helloworld::HelloReply, Box<dyn Error + Send>>{
|
||||
|
@ -171,7 +174,8 @@ impl helloworld::IGreeter<'_> for GreeterService {
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
async fn say_hello_many_to_many<'a>(
|
||||
#[allow(async_fn_in_trait)]
|
||||
async fn say_hello_many_to_many<'a: 'b>(
|
||||
&mut self,
|
||||
input: ::nrpc::ServiceServerStream<'a, helloworld::HelloRequest>,
|
||||
) -> Result<
|
||||
|
@ -188,10 +192,14 @@ impl helloworld::IGreeter<'_> for GreeterService {
|
|||
}
|
||||
}
|
||||
|
||||
fn server_is_object_safe<'b>() -> Box<dyn helloworld::IGreeter<'b>> {
|
||||
Box::new(GreeterService)
|
||||
}
|
||||
|
||||
struct ClientHandler;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl nrpc::ClientHandler<'_> for ClientHandler {
|
||||
#[nrpc::_helpers::async_trait::async_trait]
|
||||
impl <'b> nrpc::ClientHandler<'b> for ClientHandler {
|
||||
/*async fn call(
|
||||
&mut self,
|
||||
package: &str,
|
||||
|
@ -211,7 +219,8 @@ impl nrpc::ClientHandler<'_> for ClientHandler {
|
|||
.encode(output)?)
|
||||
}*/
|
||||
|
||||
async fn call<'a>(
|
||||
#[allow(async_fn_in_trait)]
|
||||
async fn call<'a: 'b>(
|
||||
&self,
|
||||
package: &str,
|
||||
service: &str,
|
||||
|
@ -234,3 +243,7 @@ impl nrpc::ClientHandler<'_> for ClientHandler {
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn client_is_object_safe<'b>() -> Box<dyn nrpc::ClientHandler<'b>> {
|
||||
Box::new(ClientHandler)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "nrpc"
|
||||
version = "0.10.0"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
repository = "https://github.com/NGnius/nRPC"
|
||||
|
@ -10,8 +10,8 @@ description = "Yet another remote procedure call library"
|
|||
[dependencies]
|
||||
prost = "0.11"
|
||||
bytes = "1"
|
||||
async-trait = "0.1"
|
||||
futures = "0.3"
|
||||
async-trait = { version = "0.1", optional = true }
|
||||
|
||||
[features]
|
||||
default = ["client-send", "server-send"]
|
||||
|
|
|
@ -6,8 +6,9 @@ pub use service::{ClientHandler, ClientService, ServerService, ServiceError, Ser
|
|||
pub use stream_utils::{EmptyStream, OnceStream, VecStream};
|
||||
|
||||
pub mod _helpers {
|
||||
pub use async_trait;
|
||||
pub use bytes;
|
||||
pub use prost;
|
||||
pub use futures;
|
||||
#[cfg(feature = "async-trait")]
|
||||
pub use async_trait;
|
||||
}
|
||||
|
|
|
@ -13,11 +13,11 @@ pub type ServiceServerStream<'a, T> = Box<dyn Stream<Item=Result<T, ServiceError
|
|||
#[cfg(not(feature = "server-send"))]
|
||||
pub type ServiceServerStream<'a, T> = Box<dyn Stream<Item=Result<T, ServiceError>> + Unpin + 'a>;
|
||||
|
||||
#[cfg_attr(feature = "server-send", async_trait::async_trait)]
|
||||
#[cfg_attr(not(feature = "server-send"), async_trait::async_trait(?Send))]
|
||||
#[cfg_attr(feature = "async-trait", async_trait::async_trait)]
|
||||
pub trait ServerService<'b> {
|
||||
fn descriptor(&self) -> &'static str;
|
||||
|
||||
#[cfg_attr(not(feature = "async-trait"), allow(async_fn_in_trait))]
|
||||
async fn call<'a: 'b>(
|
||||
&mut self,
|
||||
method: &str,
|
||||
|
@ -25,9 +25,9 @@ pub trait ServerService<'b> {
|
|||
) -> Result<ServiceServerStream<'a, bytes::Bytes>, ServiceError>;
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "client-send", async_trait::async_trait)]
|
||||
#[cfg_attr(not(feature = "client-send"), async_trait::async_trait(?Send))]
|
||||
#[cfg_attr(feature = "async-trait", async_trait::async_trait)]
|
||||
pub trait ClientHandler<'b> {
|
||||
#[cfg_attr(not(feature = "async-trait"), allow(async_fn_in_trait))]
|
||||
async fn call<'a: 'b>(
|
||||
&self,
|
||||
package: &str,
|
||||
|
|
Loading…
Reference in a new issue