From 21efbbab2b7d3c4d79bf81a9e767a2b90560f7be Mon Sep 17 00:00:00 2001 From: badcw Date: Wed, 10 Jul 2024 16:59:57 +0800 Subject: [PATCH] fix: idl map type with const enum (#262) * fix: idl map type with const enum * update test case * rm useless clone for const or temporary var in map * fix: faststr can use from_static_str to get const var * rm useless clone for vec * rm unused clone in map * increase version --- Cargo.lock | 2 +- pilota-build/Cargo.toml | 2 +- pilota-build/src/middle/ty.rs | 1 - pilota-build/test_data/thrift/enum_map.rs | 131 ++++++++++++++++++ pilota-build/test_data/thrift/enum_map.thrift | 14 ++ 5 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 pilota-build/test_data/thrift/enum_map.rs create mode 100644 pilota-build/test_data/thrift/enum_map.thrift diff --git a/Cargo.lock b/Cargo.lock index c3ddfb7e..831d12ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -805,7 +805,7 @@ dependencies = [ [[package]] name = "pilota-build" -version = "0.11.13" +version = "0.11.14" dependencies = [ "ahash", "anyhow", diff --git a/pilota-build/Cargo.toml b/pilota-build/Cargo.toml index 4eeb39b6..267e32e9 100644 --- a/pilota-build/Cargo.toml +++ b/pilota-build/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pilota-build" -version = "0.11.13" +version = "0.11.14" edition = "2021" description = "Compile thrift and protobuf idl into rust code at compile-time." documentation = "https://docs.rs/pilota-build" diff --git a/pilota-build/src/middle/ty.rs b/pilota-build/src/middle/ty.rs index bf67499e..a6f5fe14 100644 --- a/pilota-build/src/middle/ty.rs +++ b/pilota-build/src/middle/ty.rs @@ -86,7 +86,6 @@ impl CodegenTy { pub fn should_lazy_static(&self) -> bool { match self { CodegenTy::String - | CodegenTy::FastStr | CodegenTy::LazyStaticRef(_) | CodegenTy::StaticRef(_) | CodegenTy::Vec(_) diff --git a/pilota-build/test_data/thrift/enum_map.rs b/pilota-build/test_data/thrift/enum_map.rs new file mode 100644 index 00000000..df128ea4 --- /dev/null +++ b/pilota-build/test_data/thrift/enum_map.rs @@ -0,0 +1,131 @@ +pub mod enum_map { + #![allow(warnings, clippy::all)] + + pub mod enum_map { + #[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)] + pub struct TypeB(pub i32); + + impl ::std::ops::Deref for TypeB { + type Target = i32; + + fn deref(&self) -> &Self::Target { + &self.0 + } + } + + impl From for TypeB { + fn from(v: i32) -> Self { + Self(v) + } + } + + impl ::pilota::thrift::Message for TypeB { + fn encode( + &self, + __protocol: &mut T, + ) -> ::std::result::Result<(), ::pilota::thrift::ThriftException> { + #[allow(unused_imports)] + use ::pilota::thrift::TOutputProtocolExt; + __protocol.write_i32(*(&**self))?; + ::std::result::Result::Ok(()) + } + + fn decode( + __protocol: &mut T, + ) -> ::std::result::Result { + #[allow(unused_imports)] + use ::pilota::{thrift::TLengthProtocolExt, Buf}; + ::std::result::Result::Ok(TypeB(__protocol.read_i32()?)) + } + + fn decode_async<'a, T: ::pilota::thrift::TAsyncInputProtocol>( + __protocol: &'a mut T, + ) -> ::std::pin::Pin< + ::std::boxed::Box< + dyn ::std::future::Future< + Output = ::std::result::Result, + > + Send + + 'a, + >, + > { + ::std::boxed::Box::pin(async move { + ::std::result::Result::Ok(TypeB(__protocol.read_i32().await?)) + }) + } + + fn size(&self, __protocol: &mut T) -> usize { + #[allow(unused_imports)] + use ::pilota::thrift::TLengthProtocolExt; + __protocol.i32_len(*&**self) + } + } + pub const TYPE_A2: TypeA = TypeA(::pilota::FastStr::from_static_str("a2")); + pub const TYPE_B2: TypeB = TypeB(2i32); + pub const TYPE_A1: TypeA = TypeA(::pilota::FastStr::from_static_str("a1")); + pub const TYPE_B1: TypeB = TypeB(1i32); + ::pilota::lazy_static::lazy_static! { + pub static ref TYPE_A_MAP: ::pilota::AHashMap = { + let mut map = ::pilota::AHashMap::with_capacity(2); + map.insert(TYPE_B1, TYPE_A1);map.insert(TYPE_B2, TYPE_A2); + map + }; + } + #[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)] + pub struct TypeA(pub ::pilota::FastStr); + + impl ::std::ops::Deref for TypeA { + type Target = ::pilota::FastStr; + + fn deref(&self) -> &Self::Target { + &self.0 + } + } + + impl From<::pilota::FastStr> for TypeA { + fn from(v: ::pilota::FastStr) -> Self { + Self(v) + } + } + + impl ::pilota::thrift::Message for TypeA { + fn encode( + &self, + __protocol: &mut T, + ) -> ::std::result::Result<(), ::pilota::thrift::ThriftException> { + #[allow(unused_imports)] + use ::pilota::thrift::TOutputProtocolExt; + __protocol.write_faststr((&**self).clone())?; + ::std::result::Result::Ok(()) + } + + fn decode( + __protocol: &mut T, + ) -> ::std::result::Result { + #[allow(unused_imports)] + use ::pilota::{thrift::TLengthProtocolExt, Buf}; + ::std::result::Result::Ok(TypeA(__protocol.read_faststr()?)) + } + + fn decode_async<'a, T: ::pilota::thrift::TAsyncInputProtocol>( + __protocol: &'a mut T, + ) -> ::std::pin::Pin< + ::std::boxed::Box< + dyn ::std::future::Future< + Output = ::std::result::Result, + > + Send + + 'a, + >, + > { + ::std::boxed::Box::pin(async move { + ::std::result::Result::Ok(TypeA(__protocol.read_faststr().await?)) + }) + } + + fn size(&self, __protocol: &mut T) -> usize { + #[allow(unused_imports)] + use ::pilota::thrift::TLengthProtocolExt; + __protocol.faststr_len(&**self) + } + } + } +} diff --git a/pilota-build/test_data/thrift/enum_map.thrift b/pilota-build/test_data/thrift/enum_map.thrift new file mode 100644 index 00000000..16675284 --- /dev/null +++ b/pilota-build/test_data/thrift/enum_map.thrift @@ -0,0 +1,14 @@ +typedef i32 TypeB + +const TypeB TypeB1 = 1 +const TypeB TypeB2 = 2 + +typedef string TypeA + +const TypeA TypeA1 = "a1" +const TypeA TypeA2 = "a2" + +const map TypeAMap = { + TypeB1: TypeA1, + TypeB2: TypeA2, +}