Skip to content

Commit

Permalink
fix: potential name conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Millione committed Aug 9, 2023
1 parent c7e2c94 commit 079c5ed
Show file tree
Hide file tree
Showing 18 changed files with 399 additions and 696 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.8.0"
version = "0.8.1"
edition = "2021"
description = "Compile thrift and protobuf idl into rust code at compile-time."
documentation = "https://docs.rs/pilota-build"
Expand Down
16 changes: 12 additions & 4 deletions pilota-build/src/codegen/thrift/decode_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ macro_rules! protocol_len {
($m:ident) => {
paste! {
#[inline]
pub fn [<codegen_ $m>](&self) -> faststr::FastStr {
pub fn [<codegen_ $m>](&self, keep: bool) -> faststr::FastStr {
if self.is_async {
Default::default()
} else {
format!("offset += protocol.{}();", stringify!($m)).into()
if keep {
format!("__pilota_offset += protocol.{}();", stringify!($m)).into()
} else {
format!("protocol.{}();", stringify!($m)).into()
}
}
}
}
Expand Down Expand Up @@ -83,11 +87,15 @@ impl DecodeHelper {
}
}

pub fn codegen_field_begin_len(&self) -> FastStr {
pub fn codegen_field_begin_len(&self, keep: bool) -> FastStr {
if self.is_async {
Default::default()
} else {
"offset += protocol.field_begin_len(field_ident.field_type, field_ident.id);".into()
if keep {
"__pilota_offset += protocol.field_begin_len(field_ident.field_type, field_ident.id);".into()
} else {
"protocol.field_begin_len(field_ident.field_type, field_ident.id);".into()
}
}
}
}
91 changes: 45 additions & 46 deletions pilota-build/src/codegen/thrift/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ impl ThriftBackend {
.map(|f| self.rust_name(f.did))
.collect_vec();

let def_fields_num = if is_arg && !helper.is_async {
"let mut fields_num = 0;"
let def_fields_num = if keep && is_arg && !helper.is_async {
"let mut __pilota_fields_num = 0;"
} else {
""
};
Expand All @@ -196,8 +196,8 @@ impl ThriftBackend {
};

let mut s = format!("let mut {field_name} = {v};");
if is_arg && !helper.is_async {
s.push_str("fields_num += 1;");
if keep && is_arg && !helper.is_async {
s.push_str("__pilota_fields_num += 1;");
}
s
})
Expand Down Expand Up @@ -325,12 +325,13 @@ impl ThriftBackend {
is_arg: bool,
) -> String {
let record_ptr = if keep && !helper.is_async {
r#"let begin_ptr = protocol.buf().chunk().as_ptr();"#
r#"let mut __pilota_offset = 0;
let __pilota_begin_ptr = protocol.buf().chunk().as_ptr();"#
} else {
""
};
let read_field_begin = helper.codegen_read_field_begin();
let field_begin_len = helper.codegen_field_begin_len();
let field_begin_len = helper.codegen_field_begin_len(keep);
let match_fields = fields
.iter()
.map(|f| {
Expand All @@ -352,8 +353,8 @@ impl ThriftBackend {
read_field = format!("Some({read_field})").into();
}

let fields_num = if !helper.is_async && is_arg {
"fields_num -= 1;"
let fields_num = if keep && !helper.is_async && is_arg {
"__pilota_fields_num -= 1;"
} else {
""
};
Expand All @@ -367,42 +368,33 @@ impl ThriftBackend {
})
.join("");
let mut skip_ttype = helper.codegen_skip_ttype("field_ident.field_type".into());
if !helper.is_async {
skip_ttype = format!("offset += {skip_ttype}")
if keep && !helper.is_async {
skip_ttype = format!("__pilota_offset += {skip_ttype}")
}

let write_unknown_field = if keep && !helper.is_async {
"_unknown_fields.push_back(protocol.get_bytes(Some(begin_ptr), offset)?);"
"_unknown_fields.push_back(protocol.get_bytes(Some(__pilota_begin_ptr), __pilota_offset)?);"
} else {
""
};

let read_field_end = helper.codegen_read_field_end();
let field_end_len = helper.codegen_field_end_len();
let field_stop_len = helper.codegen_field_stop_len();

let skip_all = if !helper.is_async && is_arg {
let keep = if keep {
"let remaining = protocol.buf().remaining();
_unknown_fields.push_back(protocol.get_bytes(None, remaining - 2)?);"
} else {
""
};
format!(
"if fields_num == 0 {{
{keep}
break;
}}"
)
let field_end_len = helper.codegen_field_end_len(keep);
let field_stop_len = helper.codegen_field_stop_len(keep);

let skip_all = if keep && !helper.is_async && is_arg {
"if __pilota_fields_num == 0 {
let __pilota_remaining = protocol.buf().remaining();
_unknown_fields.push_back(protocol.get_bytes(None, __pilota_remaining - 2)?);
break;
}"
} else {
"".into()
""
};

format! {
r#"loop {{
{skip_all}
let mut offset = 0;
{record_ptr}
let field_ident = {read_field_begin};
if field_ident.field_type == ::pilota::thrift::TType::Stop {{
Expand Down Expand Up @@ -594,19 +586,20 @@ impl CodegenBackend for ThriftBackend {
},
|helper| {
let record_ptr = if keep && !helper.is_async {
r#"let begin_ptr = protocol.buf().chunk().as_ptr();"#
r#"let mut __pilota_offset = 0;
let __pilota_begin_ptr = protocol.buf().chunk().as_ptr();"#
} else {
""
};
let read_struct_begin = helper.codegen_read_struct_begin();
let read_field_begin = helper.codegen_read_field_begin();
let field_begin_len = helper.codegen_field_begin_len();
let field_begin_len = helper.codegen_field_begin_len(keep);
let read_field_end = helper.codegen_read_field_end();
let field_stop_len = helper.codegen_field_stop_len();
let field_stop_len = helper.codegen_field_stop_len(keep);
let read_struct_end = helper.codegen_read_struct_end();
let mut skip = helper.codegen_skip_ttype("field_ident.field_type".into());
if !helper.is_async {
skip = format!("offset += {skip}")
if keep && !helper.is_async {
skip = format!("__pilota_offset += {skip}")
}
let fields = e
.variants
Expand All @@ -619,16 +612,23 @@ impl CodegenBackend for ThriftBackend {
assert_eq!(v.fields.len(), 1);
let variant_id = v.id.unwrap() as i16;
let decode = self.codegen_decode_ty(helper, &v.fields[0]);
let decode_len = if helper.is_async {
let decode_len = if helper.is_async {
Default::default()
} else {
format!(
"offset += {};",
self.codegen_ty_size(
&v.fields[0],
"&field_ident".into()
let size = self.codegen_ty_size(
&v.fields[0],
"&field_ident".into()
);
if keep {
format!(
"__pilota_offset += {size};",
)
} else {
format!(
"{size};",
)
)
}

};
Some(format! {
r#"Some({variant_id}) => {{
Expand All @@ -651,9 +651,9 @@ impl CodegenBackend for ThriftBackend {
format!(
r#"if ret.is_none() {{
unsafe {{
let mut linked_bytes = ::pilota::LinkedBytes::new();
linked_bytes.push_back(protocol.get_bytes(Some(begin_ptr), offset)?);
ret = Some({name}::_UnknownFields(linked_bytes));
let mut __pilota_linked_bytes = ::pilota::LinkedBytes::new();
__pilota_linked_bytes.push_back(protocol.get_bytes(Some(__pilota_begin_ptr), __pilota_offset)?);
ret = Some({name}::_UnknownFields(__pilota_linked_bytes));
}}
}} else {{
return Err(::pilota::thrift::DecodeError::new(
Expand Down Expand Up @@ -683,7 +683,6 @@ impl CodegenBackend for ThriftBackend {
r#"let mut ret = None;
{read_struct_begin};
loop {{
let mut offset = 0;
{record_ptr}
let field_ident = {read_field_begin};
if field_ident.field_type == ::pilota::thrift::TType::Stop {{
Expand Down
13 changes: 4 additions & 9 deletions pilota-build/test_data/must_gen_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,25 @@ pub mod must_gen_items {
protocol.read_struct_begin()?;
if let Err(err) = (|| {
loop {
let mut offset = 0;

let field_ident = protocol.read_field_begin()?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
offset += protocol.field_stop_len();
protocol.field_stop_len();
break;
} else {
offset +=
protocol.field_begin_len(field_ident.field_type, field_ident.id);
protocol.field_begin_len(field_ident.field_type, field_ident.id);
}
__pilota_decoding_field_id = field_ident.id;
match field_ident.id {
Some(1) if field_ident.field_type == ::pilota::thrift::TType::I32 => {
a = Some(protocol.read_i32()?);
}
_ => {
offset += protocol.skip(field_ident.field_type)?;
protocol.skip(field_ident.field_type)?;
}
}

protocol.read_field_end()?;
offset += protocol.field_end_len();
protocol.field_end_len();
}
Ok::<_, ::pilota::thrift::DecodeError>(())
})() {
Expand Down Expand Up @@ -90,8 +87,6 @@ pub mod must_gen_items {
protocol.read_struct_begin().await?;
if let Err(err) = async {
loop {
let mut offset = 0;

let field_ident = protocol.read_field_begin().await?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
break;
Expand Down
13 changes: 4 additions & 9 deletions pilota-build/test_data/plugin/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,12 @@ pub mod serde {
protocol.read_struct_begin()?;
if let Err(err) = (|| {
loop {
let mut offset = 0;

let field_ident = protocol.read_field_begin()?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
offset += protocol.field_stop_len();
protocol.field_stop_len();
break;
} else {
offset +=
protocol.field_begin_len(field_ident.field_type, field_ident.id);
protocol.field_begin_len(field_ident.field_type, field_ident.id);
}
__pilota_decoding_field_id = field_ident.id;
match field_ident.id {
Expand All @@ -73,12 +70,12 @@ pub mod serde {
b = Some(protocol.read_i32()?);
}
_ => {
offset += protocol.skip(field_ident.field_type)?;
protocol.skip(field_ident.field_type)?;
}
}

protocol.read_field_end()?;
offset += protocol.field_end_len();
protocol.field_end_len();
}
Ok::<_, ::pilota::thrift::DecodeError>(())
})() {
Expand Down Expand Up @@ -127,8 +124,6 @@ pub mod serde {
protocol.read_struct_begin().await?;
if let Err(err) = async {
loop {
let mut offset = 0;

let field_ident = protocol.read_field_begin().await?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
break;
Expand Down
13 changes: 4 additions & 9 deletions pilota-build/test_data/thrift/binary_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,12 @@ pub mod binary_bytes {
protocol.read_struct_begin()?;
if let Err(err) = (|| {
loop {
let mut offset = 0;

let field_ident = protocol.read_field_begin()?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
offset += protocol.field_stop_len();
protocol.field_stop_len();
break;
} else {
offset +=
protocol.field_begin_len(field_ident.field_type, field_ident.id);
protocol.field_begin_len(field_ident.field_type, field_ident.id);
}
__pilota_decoding_field_id = field_ident.id;
match field_ident.id {
Expand All @@ -63,12 +60,12 @@ pub mod binary_bytes {
vec = Some(protocol.read_bytes_vec()?);
}
_ => {
offset += protocol.skip(field_ident.field_type)?;
protocol.skip(field_ident.field_type)?;
}
}

protocol.read_field_end()?;
offset += protocol.field_end_len();
protocol.field_end_len();
}
Ok::<_, ::pilota::thrift::DecodeError>(())
})() {
Expand Down Expand Up @@ -117,8 +114,6 @@ pub mod binary_bytes {
protocol.read_struct_begin().await?;
if let Err(err) = async {
loop {
let mut offset = 0;

let field_ident = protocol.read_field_begin().await?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
break;
Expand Down
Loading

0 comments on commit 079c5ed

Please sign in to comment.