Skip to content

Commit

Permalink
fix: idl map type with const enum (#262)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
WUST-mengqinyu authored Jul 10, 2024
1 parent e1b3ec2 commit 21efbba
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pilota-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
1 change: 0 additions & 1 deletion pilota-build/src/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)
Expand Down
131 changes: 131 additions & 0 deletions pilota-build/test_data/thrift/enum_map.rs
Original file line number Diff line number Diff line change
@@ -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<i32> for TypeB {
fn from(v: i32) -> Self {
Self(v)
}
}

impl ::pilota::thrift::Message for TypeB {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&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<T: ::pilota::thrift::TInputProtocol>(
__protocol: &mut T,
) -> ::std::result::Result<Self, ::pilota::thrift::ThriftException> {
#[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<Self, ::pilota::thrift::ThriftException>,
> + Send
+ 'a,
>,
> {
::std::boxed::Box::pin(async move {
::std::result::Result::Ok(TypeB(__protocol.read_i32().await?))
})
}

fn size<T: ::pilota::thrift::TLengthProtocol>(&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<TypeB, TypeA> = {
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<T: ::pilota::thrift::TOutputProtocol>(
&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<T: ::pilota::thrift::TInputProtocol>(
__protocol: &mut T,
) -> ::std::result::Result<Self, ::pilota::thrift::ThriftException> {
#[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<Self, ::pilota::thrift::ThriftException>,
> + Send
+ 'a,
>,
> {
::std::boxed::Box::pin(async move {
::std::result::Result::Ok(TypeA(__protocol.read_faststr().await?))
})
}

fn size<T: ::pilota::thrift::TLengthProtocol>(&self, __protocol: &mut T) -> usize {
#[allow(unused_imports)]
use ::pilota::thrift::TLengthProtocolExt;
__protocol.faststr_len(&**self)
}
}
}
}
14 changes: 14 additions & 0 deletions pilota-build/test_data/thrift/enum_map.thrift
Original file line number Diff line number Diff line change
@@ -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<TypeB, TypeA> TypeAMap = {
TypeB1: TypeA1,
TypeB2: TypeA2,
}

0 comments on commit 21efbba

Please sign in to comment.