Skip to content

Commit 869d9ee

Browse files
committed
Address review
1 parent cd77988 commit 869d9ee

File tree

9 files changed

+82
-23
lines changed

9 files changed

+82
-23
lines changed

.github/workflows/CI.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ jobs:
5252
# Share repository cache between workflows.
5353
repository-cache: true
5454
module-root: ./protoc-gen-rust-grpc
55+
# Building the protoc plugin from scratch takes 6–14 minutes, depending on
56+
# the OS. This delays the execution of workflows that use the plugin in
57+
# build.rs files. We try to avoid rebuilding the plugin if it hasn't
58+
# changed.
5559
- name: Build protoc plugin
5660
if: steps.cache-plugin.outputs.cache-hit != 'true'
5761
working-directory: ./protoc-gen-rust-grpc

grpc/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ dns = ["dep:hickory-resolver"]
2828
allowed_external_types = [
2929
"tonic::*",
3030
"futures_core::stream::Stream",
31-
"protobuf::codegen_traits::Message",
3231
]

grpc/src/macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@
5151
/// ```rust,ignore
5252
/// mod protos {
5353
/// // Include message code.
54-
/// include!("/protobuf/directory/protos/generated.rs");
54+
/// include!("relative/protobuf/directory/generated.rs");
5555
///
5656
/// // Include service code.
57-
/// include!("/protobuf/directory/protos/helloworld_grpc.pb.rs");
57+
/// include!("relative/protobuf/directory/helloworld_grpc.pb.rs");
5858
/// }
5959
/// ```
6060
///
@@ -64,12 +64,12 @@
6464
/// ```rust,ignore
6565
/// mod protos {
6666
/// // Include message code.
67-
/// include!("/protobuf/directory/protos/generated.rs");
67+
/// include!("relative/protobuf/directory/generated.rs");
6868
/// }
6969
///
7070
/// mod grpc {
7171
/// // Include service code.
72-
/// include!("/protobuf/directory/proto/helloworld_grpc.pb.rs");
72+
/// include!("relative/protobuf/directory/helloworld_grpc.pb.rs");
7373
/// }
7474
/// ```
7575
///

interop/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() {
55
tonic_protobuf_build::CodeGen::new()
66
.include("proto/grpc/testing")
77
.inputs(["test.proto", "empty.proto", "messages.proto"])
8-
.generate_and_compile()
8+
.compile()
99
.unwrap();
1010

1111
// prevent needing to rebuild if files (or deps) haven't changed

tonic-protobuf-build/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tonic-protobuf-build"
3-
version = "0.9.0-alpha.1"
3+
version = "0.14.0"
44
edition = "2021"
55
authors = ["gRPC Authors"]
66
license = "MIT"

tonic-protobuf-build/README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Required dependencies
1111
[dependencies]
1212
tonic = "<tonic-version>"
1313
protobuf = "<protobuf-version>"
14+
tonic-protobuf = "<tonic-protobuf-version>"
1415

1516
[build-dependencies]
1617
tonic-protobuf-build = "<tonic-protobuf-build-version>"
@@ -31,7 +32,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3132
tonic_protobuf_build::CodeGen::new()
3233
.include("proto")
3334
.inputs(["service.proto"])
34-
.generate_and_compile()?;
35+
.compile()?;
3536
Ok(())
3637
}
3738
```
@@ -40,18 +41,20 @@ Or configure the generated code deeper via
4041

4142
```rust,no_run
4243
fn main() -> Result<(), Box<dyn std::error::Error>> {
43-
tonic_protobuf_build::configure()
44+
let dependency = tonic_protobuf_build::Dependency::builder()
45+
.crate_name("external_protos".to_string())
46+
.proto_import_paths(vec![PathBuf::from("external/message.proto")])
47+
.proto_files(vec!["message.proto".to_string()])
48+
.build()?;
49+
50+
tonic_protobuf_build::CodeGen::new()
4451
.generate_message_code(false)
4552
.inputs(["proto/helloworld/helloworld.proto"])
4653
.include("external")
4754
.message_module_path("super::proto")
48-
.dependencies(vec![tonic_protobuf_build::Dependency {
49-
crate_name: "external_protos".to_string(),
50-
proto_import_paths: vec![PathBuf::from("external/message.proto")],
51-
proto_files: vec!["message.proto".to_string()],
52-
}])
55+
.dependencies(vec![dependency])
5356
//.out_dir("src/generated") // you can change the generated code's location
54-
.generate_and_compile()?;
57+
.compile()?;
5558
Ok(())
5659
}
5760
```

tonic-protobuf-build/src/lib.rs

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,74 @@ use std::path::{Path, PathBuf};
2828

2929
use syn::parse_file;
3030

31-
/// Details about a crate containing proto files with symbols refferenced in
31+
/// Details about a crate containing proto files with symbols referenced in
3232
/// the file being compiled currently.
3333
#[derive(Debug, Clone)]
3434
pub struct Dependency {
35+
crate_name: String,
36+
proto_import_paths: Vec<PathBuf>,
37+
proto_files: Vec<String>,
38+
}
39+
40+
impl Dependency {
41+
pub fn builder() -> DependencyBuilder {
42+
DependencyBuilder::default()
43+
}
44+
}
45+
46+
#[derive(Default, Debug)]
47+
pub struct DependencyBuilder {
48+
crate_name: Option<String>,
49+
proto_import_paths: Vec<PathBuf>,
50+
proto_files: Vec<String>,
51+
}
52+
53+
impl DependencyBuilder {
3554
/// Name of the external crate.
36-
pub crate_name: String,
55+
pub fn crate_name(mut self, name: impl Into<String>) -> Self {
56+
self.crate_name = Some(name.into());
57+
self
58+
}
59+
3760
/// List of paths .proto files whose codegen is present in the crate. This
3861
/// is used to re-run the build command if required.
39-
pub proto_import_paths: Vec<PathBuf>,
62+
pub fn proto_import_path(mut self, path: impl Into<PathBuf>) -> Self {
63+
self.proto_import_paths.push(path.into());
64+
self
65+
}
66+
4067
/// List of .proto file names whose codegen is present in the crate.
41-
pub proto_files: Vec<String>,
68+
pub fn proto_import_paths(mut self, paths: Vec<PathBuf>) -> Self {
69+
self.proto_import_paths = paths;
70+
self
71+
}
72+
73+
pub fn proto_file(mut self, file: impl Into<String>) -> Self {
74+
self.proto_files.push(file.into());
75+
self
76+
}
77+
78+
pub fn proto_files(mut self, files: Vec<String>) -> Self {
79+
self.proto_files = files;
80+
self
81+
}
82+
83+
pub fn build(self) -> Result<Dependency, &'static str> {
84+
let crate_name = self.crate_name.ok_or("crate_name is required")?;
85+
Ok(Dependency {
86+
crate_name,
87+
proto_import_paths: self.proto_import_paths,
88+
proto_files: self.proto_files,
89+
})
90+
}
4291
}
4392

4493
impl From<&Dependency> for protobuf_codegen::Dependency {
4594
fn from(val: &Dependency) -> Self {
4695
protobuf_codegen::Dependency {
4796
crate_name: val.crate_name.clone(),
4897
proto_import_paths: val.proto_import_paths.clone(),
49-
// TODO: Is this useful to expose the following field? It's not used
50-
// by protobuf codegen.
98+
// The following field is not used by protobuf codegen.
5199
c_include_paths: Vec::new(),
52100
proto_files: val.proto_files.clone(),
53101
}
@@ -146,7 +194,7 @@ impl CodeGen {
146194
self
147195
}
148196

149-
pub fn generate_and_compile(&self) -> Result<(), String> {
197+
pub fn compile(&self) -> Result<(), String> {
150198
// Generate the message code.
151199
if self.generate_message_code {
152200
protobuf_codegen::CodeGen::new()

tonic-protobuf/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
[package]
22
name = "tonic-protobuf"
3-
version = "0.9.0-alpha.1"
3+
version = "0.14.0"
44
edition = "2021"
55
authors = ["gRPC Authors"]
66
license = "MIT"
7+
publish = false
78

89
[dependencies]
910
tonic = { version = "0.14.0", path = "../tonic", default-features = false, features = ["codegen"] }

tonic-protobuf/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ impl<T: Message> Encoder for ProtoEncoder<T> {
8181
type Error = Status;
8282

8383
fn encode(&mut self, item: Self::Item, buf: &mut EncodeBuf<'_>) -> Result<(), Self::Error> {
84+
// The protobuf library doesn't support serializing into a user-provided
85+
// buffer. Instead, it allocates its own buffer, resulting in an extra
86+
// copy and allocation.
87+
// TODO: Find a way to avoid this extra copy.
8488
let serialized = item.serialize().map_err(from_decode_error)?;
8589
buf.put_slice(serialized.as_slice());
8690
Ok(())

0 commit comments

Comments
 (0)