From c3f29ca00933fdbe8a1da945a35115687ff93ae1 Mon Sep 17 00:00:00 2001 From: Pure White Date: Sat, 21 Oct 2023 14:37:42 +0800 Subject: [PATCH] feat: use afit to optimize gen code --- Cargo.lock | 64 +++++++++++++----------- Cargo.toml | 7 ++- examples/Cargo.toml | 1 - examples/src/compression/grpc_server.rs | 1 - examples/src/hello/grpc_server.rs | 1 - examples/src/hello/thrift_server.rs | 1 - examples/src/loadbalance/grpc_server.rs | 1 - examples/src/multiplex/grpc_server.rs | 2 - examples/src/streaming/grpc_server.rs | 1 - examples/src/unknown/thrift_server.rs | 1 - examples/volo-gen/Cargo.toml | 1 - volo-build/Cargo.toml | 1 - volo-build/src/grpc_backend.rs | 5 +- volo-build/src/thrift_backend.rs | 5 +- volo-cli/src/templates/grpc/src/lib_rs | 1 - volo-cli/src/templates/thrift/src/lib_rs | 1 - volo-grpc/Cargo.toml | 1 - volo-thrift/Cargo.toml | 1 - volo-thrift/src/error.rs | 1 + volo/Cargo.toml | 1 - volo/src/lib.rs | 1 - volo/src/net/dial.rs | 9 ++-- volo/src/net/incoming.rs | 14 ++---- 23 files changed, 56 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ff97f985..071c175f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -514,7 +514,6 @@ version = "0.0.0" dependencies = [ "anyhow", "async-stream", - "async-trait", "lazy_static", "metainfo", "pilota", @@ -824,7 +823,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite", - "socket2 0.4.9", + "socket2 0.4.10", "tokio", "tower-service", "tracing", @@ -921,9 +920,9 @@ dependencies = [ [[package]] name = "integer-encoding" -version = "3.0.4" +version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bb03732005da905c88227371639bf1ad885cc712789c011c31c5fb3ab3ccf02" +checksum = "924df4f0e24e2e7f9cdd90babb0b96f93b20f3ecfa949ea9e6613756b8c8e1bf" dependencies = [ "async-trait", "tokio", @@ -931,9 +930,9 @@ dependencies = [ [[package]] name = "ipnet" -version = "2.8.0" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28b29a3cd74f0f4598934efe3aeba42bae0eb4680554128851ebbecb02af14e6" +checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" [[package]] name = "is-terminal" @@ -948,9 +947,9 @@ dependencies = [ [[package]] name = "itertools" -version = "0.10.5" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" dependencies = [ "either", ] @@ -1370,12 +1369,11 @@ dependencies = [ [[package]] name = "pilota" -version = "0.8.1" -source = "git+https://github.com/cloudwego/pilota?branch=main#d6693254e00af826ada7d6ff4364fd0462a99044" +version = "0.9.0" +source = "git+https://github.com/cloudwego/pilota?branch=main#b9fff2cad3e0dcff755a181720eaa651b23bab69" dependencies = [ "anyhow", "async-recursion", - "async-trait", "bytes", "derivative", "faststr", @@ -1392,8 +1390,8 @@ dependencies = [ [[package]] name = "pilota-build" -version = "0.8.6" -source = "git+https://github.com/cloudwego/pilota?branch=main#d6693254e00af826ada7d6ff4364fd0462a99044" +version = "0.9.0" +source = "git+https://github.com/cloudwego/pilota?branch=main#b9fff2cad3e0dcff755a181720eaa651b23bab69" dependencies = [ "anyhow", "dashmap", @@ -1424,8 +1422,8 @@ dependencies = [ [[package]] name = "pilota-thrift-parser" -version = "0.4.2" -source = "git+https://github.com/cloudwego/pilota?branch=main#d6693254e00af826ada7d6ff4364fd0462a99044" +version = "0.9.0" +source = "git+https://github.com/cloudwego/pilota?branch=main#b9fff2cad3e0dcff755a181720eaa651b23bab69" dependencies = [ "nom", ] @@ -1485,7 +1483,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" dependencies = [ "once_cell", - "toml_edit", + "toml_edit 0.19.15", ] [[package]] @@ -2025,9 +2023,9 @@ checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" [[package]] name = "socket2" -version = "0.4.9" +version = "0.4.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64a4a911eed85daf18834cfaa86a79b7d266ff93ff5ba14005426219480ed662" +checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d" dependencies = [ "libc", "winapi", @@ -2035,9 +2033,9 @@ dependencies = [ [[package]] name = "socket2" -version = "0.5.4" +version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4031e820eb552adee9295814c0ced9e5cf38ddf1e8b7d566d6de8e2538ea989e" +checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9" dependencies = [ "libc", "windows-sys", @@ -2189,7 +2187,7 @@ dependencies = [ "parking_lot 0.12.1", "pin-project-lite", "signal-hook-registry", - "socket2 0.5.4", + "socket2 0.5.5", "tokio-macros", "windows-sys", ] @@ -2253,14 +2251,14 @@ dependencies = [ [[package]] name = "toml" -version = "0.7.8" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd79e69d3b627db300ff956027cc6c3798cef26d22526befdfcd12feeb6d2257" +checksum = "185d8ab0dfbb35cf1399a6344d8484209c088f75f8f68230da55d48d95d43e3d" dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit", + "toml_edit 0.20.2", ] [[package]] @@ -2277,6 +2275,17 @@ name = "toml_edit" version = "0.19.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" +dependencies = [ + "indexmap 2.0.2", + "toml_datetime", + "winnow", +] + +[[package]] +name = "toml_edit" +version = "0.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "396e4d48bbb2b7554c944bde63101b5ae446cff6ec4a24227428f15eb72ef338" dependencies = [ "indexmap 2.0.2", "serde", @@ -2496,7 +2505,6 @@ name = "volo" version = "0.8.0" dependencies = [ "async-broadcast", - "async-trait", "dashmap", "faststr", "futures", @@ -2509,7 +2517,7 @@ dependencies = [ "once_cell", "pin-project", "rand", - "socket2 0.5.4", + "socket2 0.5.5", "thiserror", "tokio", "tokio-stream", @@ -2522,7 +2530,6 @@ name = "volo-build" version = "0.8.0" dependencies = [ "anyhow", - "async-trait", "dirs", "heck 0.4.1", "itertools", @@ -2572,7 +2579,6 @@ name = "volo-gen" version = "0.0.0" dependencies = [ "anyhow", - "async-trait", "futures", "pilota", "tokio", @@ -2588,7 +2594,6 @@ version = "0.8.0" dependencies = [ "anyhow", "async-stream", - "async-trait", "base64 0.13.1", "bytes", "flate2", @@ -2629,7 +2634,6 @@ name = "volo-thrift" version = "0.8.0" dependencies = [ "anyhow", - "async-trait", "bytes", "chrono", "futures", diff --git a/Cargo.toml b/Cargo.toml index ca510712..10bd9be9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,9 +21,9 @@ repository = "https://github.com/cloudwego/volo" license = "MIT OR Apache-2.0" [workspace.dependencies] -# pilota = "0.8" -# pilota-build = "0.8" -# pilota-thrift-parser = "0.4" +# pilota = "0.9" +# pilota-build = "0.9" +# pilota-thrift-parser = "0.9" pilota = { git = "https://github.com/cloudwego/pilota", branch = "main" } pilota-build = { git = "https://github.com/cloudwego/pilota", branch = "main" } pilota-thrift-parser = { git = "https://github.com/cloudwego/pilota", branch = "main" } @@ -36,7 +36,6 @@ metainfo = "0.7" anyhow = "1" async-broadcast = "0.6" async-stream = "0.3" -async-trait = "0.1" base64 = "0.13" bytes = "1" chrono = { version = "0.4", default-features = false, features = [ diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 96c07038..33097d14 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -66,7 +66,6 @@ path = "src/unknown/thrift_client.rs" [dependencies] anyhow.workspace = true async-stream.workspace = true -async-trait.workspace = true lazy_static.workspace = true metainfo.workspace = true tokio = { workspace = true, features = ["full"] } diff --git a/examples/src/compression/grpc_server.rs b/examples/src/compression/grpc_server.rs index 9bc42e96..3fef6380 100644 --- a/examples/src/compression/grpc_server.rs +++ b/examples/src/compression/grpc_server.rs @@ -10,7 +10,6 @@ use volo_grpc::{ pub struct S; -#[volo::async_trait] impl volo_gen::proto_gen::hello::Greeter for S { async fn say_hello( &self, diff --git a/examples/src/hello/grpc_server.rs b/examples/src/hello/grpc_server.rs index 9829d17a..a25c460d 100644 --- a/examples/src/hello/grpc_server.rs +++ b/examples/src/hello/grpc_server.rs @@ -4,7 +4,6 @@ use volo_grpc::server::{Server, ServiceBuilder}; pub struct S; -#[volo::async_trait] impl volo_gen::proto_gen::hello::Greeter for S { async fn say_hello( &self, diff --git a/examples/src/hello/thrift_server.rs b/examples/src/hello/thrift_server.rs index 142b0be2..84fc3ffc 100644 --- a/examples/src/hello/thrift_server.rs +++ b/examples/src/hello/thrift_server.rs @@ -2,7 +2,6 @@ use std::net::SocketAddr; pub struct S; -#[volo::async_trait] impl volo_gen::thrift_gen::hello::HelloService for S { async fn hello( &self, diff --git a/examples/src/loadbalance/grpc_server.rs b/examples/src/loadbalance/grpc_server.rs index 83f2b788..99c26cf7 100644 --- a/examples/src/loadbalance/grpc_server.rs +++ b/examples/src/loadbalance/grpc_server.rs @@ -14,7 +14,6 @@ impl S { } } -#[volo::async_trait] impl volo_gen::proto_gen::hello::Greeter for S { async fn say_hello( &self, diff --git a/examples/src/multiplex/grpc_server.rs b/examples/src/multiplex/grpc_server.rs index cd01914d..097931ef 100644 --- a/examples/src/multiplex/grpc_server.rs +++ b/examples/src/multiplex/grpc_server.rs @@ -4,7 +4,6 @@ use volo_grpc::server::{Server, ServiceBuilder}; pub struct G; -#[volo::async_trait] impl volo_gen::proto_gen::hello::Greeter for G { async fn say_hello( &self, @@ -20,7 +19,6 @@ impl volo_gen::proto_gen::hello::Greeter for G { pub struct E; -#[volo::async_trait] impl volo_gen::proto_gen::echo::Echo for E { async fn echo( &self, diff --git a/examples/src/streaming/grpc_server.rs b/examples/src/streaming/grpc_server.rs index 21e2eeaa..960ab898 100644 --- a/examples/src/streaming/grpc_server.rs +++ b/examples/src/streaming/grpc_server.rs @@ -10,7 +10,6 @@ use volo_grpc::{ pub struct S; -#[volo::async_trait] impl volo_gen::proto_gen::streaming::Streaming for S { async fn unary( &self, diff --git a/examples/src/unknown/thrift_server.rs b/examples/src/unknown/thrift_server.rs index 5255bde1..40a6b31b 100644 --- a/examples/src/unknown/thrift_server.rs +++ b/examples/src/unknown/thrift_server.rs @@ -2,7 +2,6 @@ use std::net::SocketAddr; pub struct S; -#[volo::async_trait] impl volo_gen::thrift_gen::echo_unknown::EchoService for S { async fn hello( &self, diff --git a/examples/volo-gen/Cargo.toml b/examples/volo-gen/Cargo.toml index ed41de4d..959e4d5a 100644 --- a/examples/volo-gen/Cargo.toml +++ b/examples/volo-gen/Cargo.toml @@ -12,7 +12,6 @@ publish = false [dependencies] anyhow.workspace = true -async-trait.workspace = true futures.workspace = true tokio = { workspace = true, features = ["full"] } diff --git a/volo-build/Cargo.toml b/volo-build/Cargo.toml index 7a095944..db058b1b 100644 --- a/volo-build/Cargo.toml +++ b/volo-build/Cargo.toml @@ -22,7 +22,6 @@ volo = { version = "0.8", path = "../volo" } pilota-build.workspace = true anyhow.workspace = true -async-trait.workspace = true dirs.workspace = true heck.workspace = true itertools.workspace = true diff --git a/volo-build/src/grpc_backend.rs b/volo-build/src/grpc_backend.rs index 1c43550b..eee3e306 100644 --- a/volo-build/src/grpc_backend.rs +++ b/volo-build/src/grpc_backend.rs @@ -590,7 +590,10 @@ impl CodegenBackend for VoloGrpcBackend { let name = self.cx().rust_name(method.def_id); - format!("async fn {name}(&self, {args}) -> ::std::result::Result<{ret_ty}>;") + format!( + "fn {name}(&self, {args}) -> impl ::std::future::Future> + Send;" + ) } fn codegen_service_method_with_global_path( diff --git a/volo-build/src/thrift_backend.rs b/volo-build/src/thrift_backend.rs index 3388aa7c..8a4367f0 100644 --- a/volo-build/src/thrift_backend.rs +++ b/volo-build/src/thrift_backend.rs @@ -627,7 +627,10 @@ impl pilota_build::CodegenBackend for VoloThriftBackend { "::volo_thrift::AnyhowError".into() }; - format!("async fn {name}(&self, {args}) -> ::core::result::Result<{ret_ty}, {exception}>;") + format!( + "fn {name}(&self, {args}) -> impl ::std::future::Future> + Send;" + ) } fn codegen_service_method_with_global_path( diff --git a/volo-cli/src/templates/grpc/src/lib_rs b/volo-cli/src/templates/grpc/src/lib_rs index e4c010c0..c5fe0d21 100644 --- a/volo-cli/src/templates/grpc/src/lib_rs +++ b/volo-cli/src/templates/grpc/src/lib_rs @@ -2,7 +2,6 @@ pub struct S; -#[volo::async_trait] impl volo_gen::{service_global_name} for S {{ {methods} }} diff --git a/volo-cli/src/templates/thrift/src/lib_rs b/volo-cli/src/templates/thrift/src/lib_rs index e4c010c0..c5fe0d21 100644 --- a/volo-cli/src/templates/thrift/src/lib_rs +++ b/volo-cli/src/templates/thrift/src/lib_rs @@ -2,7 +2,6 @@ pub struct S; -#[volo::async_trait] impl volo_gen::{service_global_name} for S {{ {methods} }} diff --git a/volo-grpc/Cargo.toml b/volo-grpc/Cargo.toml index 0146787c..9c4f2a34 100644 --- a/volo-grpc/Cargo.toml +++ b/volo-grpc/Cargo.toml @@ -25,7 +25,6 @@ metainfo.workspace = true anyhow.workspace = true async-stream.workspace = true -async-trait.workspace = true base64.workspace = true bytes.workspace = true fxhash.workspace = true diff --git a/volo-thrift/Cargo.toml b/volo-thrift/Cargo.toml index c73f006e..54fe18bc 100644 --- a/volo-thrift/Cargo.toml +++ b/volo-thrift/Cargo.toml @@ -24,7 +24,6 @@ motore.workspace = true metainfo.workspace = true anyhow.workspace = true -async-trait.workspace = true bytes.workspace = true chrono.workspace = true futures.workspace = true diff --git a/volo-thrift/src/error.rs b/volo-thrift/src/error.rs index eea31d4b..6677435a 100644 --- a/volo-thrift/src/error.rs +++ b/volo-thrift/src/error.rs @@ -120,6 +120,7 @@ impl From for Error { pilota::thrift::ProtocolErrorKind::Unknown, value.to_string(), )), + pilota::thrift::DecodeErrorKind::Unknown => protocol_err!(Unknown), } } } diff --git a/volo/Cargo.toml b/volo/Cargo.toml index e1b39b04..66e72872 100644 --- a/volo/Cargo.toml +++ b/volo/Cargo.toml @@ -23,7 +23,6 @@ maintenance = { status = "actively-developed" } motore.workspace = true async-broadcast.workspace = true -async-trait.workspace = true dashmap.workspace = true faststr.workspace = true futures.workspace = true diff --git a/volo/src/lib.rs b/volo/src/lib.rs index 70f1b467..fdf3cc32 100644 --- a/volo/src/lib.rs +++ b/volo/src/lib.rs @@ -3,7 +3,6 @@ )] #![cfg_attr(not(doctest), doc = include_str!("../README.md"))] -pub use async_trait::async_trait; pub use motore::{layer, layer::Layer, service, Service}; pub use tokio::main; diff --git a/volo/src/net/dial.rs b/volo/src/net/dial.rs index 8e51279b..e787a7b4 100644 --- a/volo/src/net/dial.rs +++ b/volo/src/net/dial.rs @@ -1,4 +1,4 @@ -use std::io; +use std::{future::Future, io}; use socket2::{Domain, Protocol, Socket, Type}; #[cfg(target_family = "unix")] @@ -15,12 +15,14 @@ use super::{ }; /// [`MakeTransport`] creates an [`AsyncRead`] and an [`AsyncWrite`] for the given [`Address`]. -#[async_trait::async_trait] pub trait MakeTransport: Clone + Send + Sync + 'static { type ReadHalf: AsyncRead + Send + Sync + Unpin + 'static; type WriteHalf: AsyncWrite + Send + Sync + Unpin + 'static; - async fn make_transport(&self, addr: Address) -> io::Result<(Self::ReadHalf, Self::WriteHalf)>; + fn make_transport( + &self, + addr: Address, + ) -> impl Future> + Send; fn set_connect_timeout(&mut self, timeout: Option); fn set_read_timeout(&mut self, timeout: Option); fn set_write_timeout(&mut self, timeout: Option); @@ -73,7 +75,6 @@ impl DefaultMakeTransport { } } -#[async_trait::async_trait] impl MakeTransport for DefaultMakeTransport { type ReadHalf = OwnedReadHalf; diff --git a/volo/src/net/incoming.rs b/volo/src/net/incoming.rs index 989155cb..91a25cd0 100644 --- a/volo/src/net/incoming.rs +++ b/volo/src/net/incoming.rs @@ -1,5 +1,7 @@ use std::{ - fmt, io, + fmt, + future::Future, + io, task::{Context, Poll}, }; @@ -22,7 +24,6 @@ pub enum DefaultIncoming { Unix(#[pin] UnixListenerStream), } -#[async_trait::async_trait] impl MakeIncoming for DefaultIncoming { type Incoming = DefaultIncoming; @@ -44,12 +45,10 @@ impl From for DefaultIncoming { } } -#[async_trait::async_trait] pub trait Incoming: fmt::Debug + Send + 'static { - async fn accept(&mut self) -> io::Result>; + fn accept(&mut self) -> impl Future>> + Send; } -#[async_trait::async_trait] impl Incoming for DefaultIncoming { async fn accept(&mut self) -> io::Result> { if let Some(conn) = self.try_next().await? { @@ -61,15 +60,13 @@ impl Incoming for DefaultIncoming { } } -#[async_trait::async_trait] pub trait MakeIncoming { type Incoming: Incoming; - async fn make_incoming(self) -> io::Result; + fn make_incoming(self) -> impl Future> + Send; } #[cfg(target_family = "unix")] -#[async_trait::async_trait] impl MakeIncoming for Address { type Incoming = DefaultIncoming; @@ -88,7 +85,6 @@ impl MakeIncoming for Address { } #[cfg(not(target_family = "unix"))] -#[async_trait::async_trait] impl MakeIncoming for Address { type Incoming = DefaultIncoming;