Skip to content

Commit

Permalink
feat: use afit and rpitit to optimize Message trait (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWhiteWu authored Oct 18, 2023
1 parent 6b48838 commit d669325
Show file tree
Hide file tree
Showing 22 changed files with 274 additions and 302 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:

steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt, clippy
# - uses: Swatinem/rust-cache@v1
Expand All @@ -41,7 +41,7 @@ jobs:

steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt, clippy
# - uses: Swatinem/rust-cache@v1
Expand All @@ -55,7 +55,7 @@ jobs:

steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v1
Expand All @@ -69,7 +69,7 @@ jobs:

steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v1
Expand All @@ -84,11 +84,11 @@ jobs:
strategy:
matrix:
rust:
- stable
- nightly

steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt, clippy
# - uses: Swatinem/rust-cache@v1
Expand Down
8 changes: 6 additions & 2 deletions pilota-build/src/codegen/thrift/decode_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ impl DecodeHelper {
}
}

pub fn codegen_item_decode(&self) -> FastStr {
pub fn codegen_item_decode(&self, name: FastStr) -> FastStr {
if self.is_async {
"::pilota::thrift::Message::decode_async(protocol).await?".into()
format!(
"<{} as ::pilota::thrift::Message>::decode_async(protocol).await?",
name
)
.into()
} else {
"::pilota::thrift::Message::decode(protocol)?".into()
}
Expand Down
3 changes: 1 addition & 2 deletions pilota-build/src/codegen/thrift/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ impl ThriftBackend {
decode_async: String,
) -> String {
format! {r#"
#[::async_trait::async_trait]
impl ::pilota::thrift::Message for {name} {{
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
Expand Down Expand Up @@ -666,7 +665,7 @@ impl CodegenBackend for ThriftBackend {
return Err(::pilota::thrift::DecodeError::new(
::pilota::thrift::DecodeErrorKind::InvalidData,
"received multiple fields for union from remote Message"
));
));
}}"#
)
} else {
Expand Down
3 changes: 2 additions & 1 deletion pilota-build/src/codegen/thrift/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@ impl ThriftBackend {
}
.into()
}
ty::Path(_) => helper.codegen_item_decode(),
ty::Path(_) => helper
.codegen_item_decode(format!("{}", self.codegen_item_ty(ty.kind.clone())).into()),
ty::Arc(ty) => {
let inner = self.codegen_decode_ty(helper, ty);
format!("::std::sync::Arc::new({inner})").into()
Expand Down
1 change: 0 additions & 1 deletion pilota-build/test_data/must_gen_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod must_gen_items {
pub struct A {
pub a: ::std::option::Option<i32>,
}
#[::async_trait::async_trait]
impl ::pilota::thrift::Message for A {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
Expand Down
3 changes: 0 additions & 3 deletions pilota-build/test_data/plugin/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub mod serde {

pub b: i32,
}
#[::async_trait::async_trait]
impl ::pilota::thrift::Message for A {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
Expand Down Expand Up @@ -222,7 +221,6 @@ pub mod serde {
E = 1,
}

#[::async_trait::async_trait]
impl ::pilota::thrift::Message for C {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
Expand Down Expand Up @@ -294,7 +292,6 @@ pub mod serde {
}
}

#[::async_trait::async_trait]
impl ::pilota::thrift::Message for B {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
Expand Down
1 change: 0 additions & 1 deletion pilota-build/test_data/thrift/binary_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub mod binary_bytes {

pub vec: ::std::vec::Vec<u8>,
}
#[::async_trait::async_trait]
impl ::pilota::thrift::Message for A {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
Expand Down
1 change: 0 additions & 1 deletion pilota-build/test_data/thrift/const_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub mod const_val {
B = 1,
}

#[::async_trait::async_trait]
impl ::pilota::thrift::Message for Index {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
Expand Down
13 changes: 8 additions & 5 deletions pilota-build/test_data/thrift/decode_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod decode_error {
pub struct A {
pub b: B,
}
#[::async_trait::async_trait]
impl ::pilota::thrift::Message for A {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
Expand Down Expand Up @@ -104,7 +103,10 @@ pub mod decode_error {
Some(1)
if field_ident.field_type == ::pilota::thrift::TType::Struct =>
{
b = Some(::pilota::thrift::Message::decode_async(protocol).await?);
b = Some(
<B as ::pilota::thrift::Message>::decode_async(protocol)
.await?,
);
}
_ => {
protocol.skip(field_ident.field_type).await?;
Expand Down Expand Up @@ -154,7 +156,6 @@ pub mod decode_error {
pub struct C {
pub a: ::pilota::FastStr,
}
#[::async_trait::async_trait]
impl ::pilota::thrift::Message for C {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
Expand Down Expand Up @@ -302,7 +303,6 @@ pub mod decode_error {
pub struct B {
pub c: C,
}
#[::async_trait::async_trait]
impl ::pilota::thrift::Message for B {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
Expand Down Expand Up @@ -400,7 +400,10 @@ pub mod decode_error {
Some(2)
if field_ident.field_type == ::pilota::thrift::TType::Struct =>
{
c = Some(::pilota::thrift::Message::decode_async(protocol).await?);
c = Some(
<C as ::pilota::thrift::Message>::decode_async(protocol)
.await?,
);
}
_ => {
protocol.skip(field_ident.field_type).await?;
Expand Down
14 changes: 8 additions & 6 deletions pilota-build/test_data/thrift/default_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub mod default_value {
Write = 2,
}

#[::async_trait::async_trait]
impl ::pilota::thrift::Message for B {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
Expand Down Expand Up @@ -124,7 +123,6 @@ pub mod default_value {

pub alias_str: ::std::option::Option<::pilota::FastStr>,
}
#[::async_trait::async_trait]
impl ::pilota::thrift::Message for A {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
Expand Down Expand Up @@ -343,12 +341,16 @@ pub mod default_value {
a = Some(protocol.read_bool().await?);
}
Some(4) if field_ident.field_type == ::pilota::thrift::TType::I32 => {
test_b =
Some(::pilota::thrift::Message::decode_async(protocol).await?);
test_b = Some(
<B as ::pilota::thrift::Message>::decode_async(protocol)
.await?,
);
}
Some(5) if field_ident.field_type == ::pilota::thrift::TType::I32 => {
test_b2 =
Some(::pilota::thrift::Message::decode_async(protocol).await?);
test_b2 = Some(
<B as ::pilota::thrift::Message>::decode_async(protocol)
.await?,
);
}
Some(6) if field_ident.field_type == ::pilota::thrift::TType::Map => {
map = Some({
Expand Down
1 change: 0 additions & 1 deletion pilota-build/test_data/thrift/enum_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub mod enum_test {
B = 16,
}

#[::async_trait::async_trait]
impl ::pilota::thrift::Message for Index {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
Expand Down
Loading

0 comments on commit d669325

Please sign in to comment.