Skip to content

continue prost - #286

Open
jokemanfire wants to merge 11 commits into
containerd:masterfrom
jokemanfire:prost
Open

continue prost #286
jokemanfire wants to merge 11 commits into
containerd:masterfrom
jokemanfire:prost

Conversation

@jokemanfire

Copy link
Copy Markdown
Member

work continue with #173

@jokemanfire jokemanfire changed the title continue prost continue prost (not complete) Feb 24, 2025
@Tim-Zhang

Tim-Zhang commented Feb 25, 2025

Copy link
Copy Markdown
Member

@jokemanfire I appreciate you taking on this work very much.
Why don't we directly reopen #173 and continue the work?
Just let me know if there are any difficulties.

Thanks agian.

@jokemanfire

Copy link
Copy Markdown
Member Author

@jokemanfire I appreciate you taking on this work very much. Why don't we directly reopen #173 and continue the work? Just let me know if there are any difficulties.

Thanks agian.

There are some conflicts and API changes that need to be resolved and some code needs to be packaged to adapt to the current API. It's not easy to operate with the previous version of PR. I need to add some commit to complete this feature. I have communicated with the original author.

@jokemanfire

Copy link
Copy Markdown
Member Author

@Tim-Zhang @justxuewei Local test all pass but i don't know how to install protoc in CI.

@jokemanfire jokemanfire changed the title continue prost (not complete) continue prost Mar 13, 2025
@Tim-Zhang

Copy link
Copy Markdown
Member

@jokemanfire
jokemanfire force-pushed the prost branch 3 times, most recently from 21ebbd3 to 4a76b7b Compare April 1, 2025 12:19
@jokemanfire
jokemanfire force-pushed the prost branch 3 times, most recently from a29312f to 5fadae0 Compare April 9, 2025 09:50
@jokemanfire

jokemanfire commented Apr 9, 2025

Copy link
Copy Markdown
Member Author

Basically enough @Tim-Zhang @justxuewei just take a look . :)

@justxuewei

justxuewei commented Apr 10, 2025

Copy link
Copy Markdown
Contributor

Nice work, thanks @jokemanfire! Could you cleanup your commits? It makes us easier to review and maintain.

  1. Use git rebase instead of git merge.
  2. Modify the commit title to {subsystem}: {brief title for your changes}, and give it a brief introduction in the commit message.
  3. Split your commits, and don't mix bugfixes, refactoring and new features.
  4. Please modify this PR title to something like "codegen: Introduce a new codegen using prost".

Thanks!

justxuewei and others added 7 commits April 10, 2025 11:19
This commit refactors the ttrpc-codegen and the compiler, and merges the two
crates into a single crate, named "codegen". The codegen uses prost crate, a
protobuf compiler for Rust.

Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
The ttrpc provides a "prost" feature to support the new version of
codegen. An "example2" has been added to demonstrate how to use the
codegen.

Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
Install protoc when executing `make deps` of the ttrpc. Add codegen's
check and build, and example2' build to the ci testing.

Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
Fix all issues reported by cargo clippy to make ci testing pass.

Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
merge from remote branch.
ref: containerd#173

Co-authored-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
fix ci for protoc

Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
because of the api change
1. fix the marco in prost
2. fix the test

Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
fix rebase error.

Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
@jokemanfire

Copy link
Copy Markdown
Member Author

Nice work, thanks @jokemanfire! Could you cleanup your commits? It makes us easier to review and maintain.好的工作,谢谢!你能清理一下你的提交吗?这让我们更容易审查和维护。

  1. Use git rebase instead of git merge.使用 git rebase 代替 git merge
  2. Modify the commit title to {subsystem}: {brief title for your changes}, and give it a brief introduction in the commit message.修改提交标题为 {subsystem}: {brief title for your changes} ,并在提交信息中简要介绍。
  3. Split your commits, and don't mix bugfixes, refactoring and new features.拆分您的提交,不要混合修复错误、重构和新功能。
  4. Please modify this PR title to something like "codegen: Introduce a new codegen using prost".请将此 PR 标题修改为类似于“codegen:引入使用 prost 的新代码生成器”的标题。

Thanks!  谢谢!

I have changed it.

@jokemanfire
jokemanfire force-pushed the prost branch 2 times, most recently from aa000cc to 4da8148 Compare April 10, 2025 06:54
@jokemanfire
jokemanfire force-pushed the prost branch 2 times, most recently from b25b1a0 to 00fce61 Compare April 10, 2025 07:24
skip build example2 in windows

Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
@justxuewei

Copy link
Copy Markdown
Contributor

Once this feature gets merged, I prefer to bump a major version to 2 to avoid some compatibility issues, as it nearly changes the entire codegen engine. WDYT? @jokemanfire @Tim-Zhang

@jokemanfire

Copy link
Copy Markdown
Member Author

Once this feature gets merged, I prefer to bump a major version to 2 to avoid some compatibility issues, as it nearly changes the entire codegen engine. WDYT? 一旦这个功能合并,我更倾向于将主版本号提升到 2,以避免一些兼容性问题,因为它几乎改变了整个代码生成引擎。你怎么看?@jokemanfire @Tim-Zhang

This modification is indeed quite significant, and I agree. It depends on the maintainer's thoughts

Comment thread build.rs
let path: PathBuf = [out_dir.clone(), "mod.rs".to_string()].iter().collect();
fs::write(path, "pub mod ttrpc;").unwrap();

generate_ttrpc(&out_dir);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get unified internal APIs which was defined in ttrpc.proto, how about keeping build script untouched?

I think we can try to avoid so many condition compiling sentences like #[cfg(not(feature = "prost"))] in code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right ,I think adding a transformation to the camel hump naming convention can reduce this conditional compilation code

while use protoc,rename the Code enum.

Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
fix marco

Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
@jokemanfire

Copy link
Copy Markdown
Member Author

I tried to minimize conditional compilation as much as possible @Tim-Zhang @justxuewei

@jokemanfire

jokemanfire commented Jun 24, 2025

Copy link
Copy Markdown
Member Author

@Tim-Zhang Can you take some time to advance this feature? In the previous puncture, it was found that the prost library uses less memory during operation than the Rust Protobuf library

Tim-Zhang

This comment was marked as outdated.

@Tim-Zhang Tim-Zhang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several generator edge cases, size-limit regressions, and CI issues remain. The inline comments also include refactoring and idiomatic-Rust feedback focused on containing backend-specific behavior, reducing duplication, and avoiding unnecessary allocations and API divergence. Please address the inline findings before merge and add focused fixtures for imported types, multiple services with overlapping method names, and package-less schemas.

Comment thread README.md
1. The protoc should be installed.
2. Enabling "prost" feature for the ttrpc-rust.
3. The Rust files are named based on their package name, rather than the proto
filename, e.g. `ttrpc = { version = "1.0", features = ["prost"] }`.

@Tim-Zhang Tim-Zhang Aug 5, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example enables prost while keeping default features, which also enables rustprotobuf; the mutual-exclusion check then aborts compilation. Please document default-features = false and include the desired runtime feature explicitly, for example features = ["sync", "prost"].

Comment thread src/lib.rs
() => {};
($first:tt $(,$rest:tt)*) => {
$(
#[cfg(all(feature = $first, feature = $rest))]

@Tim-Zhang Tim-Zhang Aug 5, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs.rs is configured with all-features = true, so it activates both protobuf backends and always reaches this compile error. Configure docs.rs with one explicit compatible backend feature set instead of all features.

Comment thread codegen/src/svcgen.rs
fn method_handler_impl_sync_token(&self, struct_name: &Ident, method: &Method) -> TokenStream {
let mod_path = ttrpc_mod();
let context = self.ttrpc_context(false);
let input_type = format_ident!("{}", method.input_type);

@Tim-Zhang Tim-Zhang Aug 5, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method.input_type can be a qualified path such as super::google::protobuf::Empty. Passing it to format_ident! panics because it is not a single identifier. Use the path-aware type_token(&method.input_type) conversion here and in the async handler.

Comment thread codegen/src/svcgen.rs
fn finalize_package(&mut self, _package: &str, _buf: &mut String) {}
/// Generate services
fn generate(&mut self, service: Service, buf: &mut String) {
self.generate_type_aliases(buf);

@Tim-Zhang Tim-Zhang Aug 5, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prost-build calls generate once per service and appends services from a package into the same module. Emitting HashMap, Arc, Message, and async_trait imports here for every service causes E0252 on packages with multiple services. Emit package-level imports once, for example from finalize_package.

Comment thread codegen/src/svcgen.rs
}

fn method_handler_token(&self, service: &Service, method: &Method) -> TokenStream {
let struct_name = format_ident!("{}Method", to_camel_case(method.proto_name.as_str()));

@Tim-Zhang Tim-Zhang Aug 5, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The handler type is derived only from the method name, so Foo.Get and Bar.Get in one package both generate GetMethod. Include the service name in this type, for example FooGetMethod.

Comment thread codegen/src/codegen.rs
}
}

#[derive(Default)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idiomatic Rust: the boolean and enum settings can store their defaults directly rather than using Option, and build should normally consume self. The P: Default bound is unrelated to paths, while borrowing all inputs makes this builder unnecessarily restrictive. Consider owned PathBuf/Vec<PathBuf> fields and build(self).

Comment thread Cargo.toml
# lock home to avoid conflict with latest version
home = "=0.5.9"
protobuf-codegen = "3.1.0"
prost-build = { version = "0.13", optional = true }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor/dependencies: the runtime uses prost 0.11 here while the root build uses prost-build 0.13, and the new codegen crate pins the Prost family to 0.11. Keep the Prost crates on one compatible version and centralize the versions to avoid duplicate dependency trees and generated/runtime drift.

Comment thread codegen/src/util.rs
let mut camel_case_name = String::with_capacity(name.len());
for s in NameSpliter::new(name) {
let mut chs = s.chars();
camel_case_name.extend(chs.next().unwrap().to_uppercase());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor/correctness: this custom name splitter can yield an empty segment after leading underscores and then panic at this unwrap; it also reimplements casing already handled by established Rust casing utilities and by normalized prost-build names. Prefer those normalized names or a well-tested casing helper.

Comment thread example2/Cargo.toml
@@ -0,0 +1,55 @@
[package]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scope/refactor: example2 duplicates the existing examples and brings in large third-party proto trees solely to switch protobuf backends, so future fixes and tests can diverge. Prefer running the existing example under a backend feature matrix and add small focused fixtures only for Prost-specific codegen behavior.

Comment thread .gitignore
example2/protocols/**/*.rs
!example2/protocols/**/mod.rs
src/ttrpc.rs
example2/protocols/**/*.rs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These example2 ignore/negation rules duplicate lines 10-11. Please remove the redundant entries and keep the file's final newline.

@Tim-Zhang
Tim-Zhang dismissed their stale review August 5, 2026 13:15

Superseded by the inline review with additional refactoring feedback.

@Tim-Zhang

Copy link
Copy Markdown
Member

@jokemanfire I have left a few review comments, and btw please resolve the conflicts, thanks.

@jokemanfire

Copy link
Copy Markdown
Member Author

@jokemanfire I have left a few review comments, and btw please resolve the conflicts, thanks.

thanks , I will take some time to resolve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants