Skip to content

Commit 4929e90

Browse files
authored
Merge pull request #291 from jprendes/bump-protobuf
Compiler: Bump protobuf to 3.7.2 from 2.27.1
2 parents b3e229e + b0d14cc commit 4929e90

File tree

10 files changed

+421
-209
lines changed

10 files changed

+421
-209
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ description = "A Rust version of ttrpc."
1212
rust-version = "1.70"
1313

1414
[dependencies]
15-
protobuf = { version = "3.1.0" }
15+
protobuf = { version = "3.7.2" }
1616
libc = { version = "0.2.59", features = [ "extra_traits" ] }
1717
nix = "0.26.2"
1818
log = "0.4"
1919
byteorder = "1.3.2"
2020
thiserror = "1.0"
2121
async-trait = { version = "0.1.31", optional = true }
2222
async-stream = { version = "0.3.6", optional = true }
23-
tokio = { version = "1", features = ["rt", "sync", "io-util", "macros", "time"], optional = true }
23+
tokio = { version = "1", features = ["rt", "sync", "io-util", "macros", "time", "net"], optional = true }
2424
futures = { version = "0.3", optional = true }
2525
crossbeam = "0.8.0"
2626

@@ -33,7 +33,7 @@ tokio-vsock = { version = "0.7.0", optional = true }
3333
[build-dependencies]
3434
# lock home to avoid conflict with latest version
3535
home = "=0.5.9"
36-
protobuf-codegen = "3.1.0"
36+
protobuf-codegen = "3.7.2"
3737

3838
[features]
3939
default = ["sync"]

compiler/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ readme = "README.md"
1414
[dependencies]
1515
# lock home to avoid conflict with latest version
1616
home = "=0.5.9"
17-
protobuf = "2.27.1"
18-
protobuf-codegen = "2.27.1"
17+
protobuf = "3.7.2"
18+
protobuf-codegen = "3.7.2"
1919
prost = "0.8"
2020
prost-build = "0.8"
2121
prost-types = "0.8"

compiler/src/codegen.rs

Lines changed: 78 additions & 102 deletions
Large diffs are not rendered by default.

compiler/src/util.rs renamed to compiler/src/util/mod.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
use protobuf_codegen::code_writer::CodeWriter;
1716
use std::fmt;
1817
use std::str;
1918

19+
pub mod scope;
20+
pub mod writer;
21+
22+
use writer::CodeWriter;
23+
2024
// A struct that divide a name into serveral parts that meets rust's guidelines.
2125
struct NameSpliter<'a> {
2226
name: &'a [u8],
@@ -111,9 +115,9 @@ where
111115
F: Fn(&mut CodeWriter),
112116
{
113117
if public {
114-
w.expr_block(&format!("pub async fn {}", sig), cb);
118+
w.expr_block(format!("pub async fn {}", sig), cb);
115119
} else {
116-
w.expr_block(&format!("async fn {}", sig), cb);
120+
w.expr_block(format!("async fn {}", sig), cb);
117121
}
118122
}
119123

@@ -131,6 +135,15 @@ where
131135
async_fn_block(w, false, sig, cb);
132136
}
133137

138+
// proto_name_to_rs is constructor as "{proto_path_to_rust_mod}.rs"
139+
// see https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/paths.rs#L43
140+
pub fn proto_path_to_rust_mod(path: &str) -> String {
141+
protobuf_codegen::proto_name_to_rs(path)
142+
.strip_suffix(".rs")
143+
.unwrap()
144+
.to_string()
145+
}
146+
134147
pub enum MethodType {
135148
Unary,
136149
ClientStreaming,

compiler/src/util/scope.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
//! This module contains functionalities that where previously available in
2+
//! the protobuf / protobuf-codegen crates, but were then removed.
3+
//! The missing functionalities have been reimplemented in this module.
4+
5+
use protobuf::descriptor::{DescriptorProto, FileDescriptorProto};
6+
7+
// vendered from https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/rust/keywords.rs
8+
fn is_rust_keyword(ident: &str) -> bool {
9+
#[rustfmt::skip]
10+
static RUST_KEYWORDS: &[&str] = &[
11+
"_",
12+
"as",
13+
"async",
14+
"await",
15+
"break",
16+
"crate",
17+
"dyn",
18+
"else",
19+
"enum",
20+
"extern",
21+
"false",
22+
"fn",
23+
"for",
24+
"if",
25+
"impl",
26+
"in",
27+
"let",
28+
"loop",
29+
"match",
30+
"mod",
31+
"move",
32+
"mut",
33+
"pub",
34+
"ref",
35+
"return",
36+
"static",
37+
"self",
38+
"Self",
39+
"struct",
40+
"super",
41+
"true",
42+
"trait",
43+
"type",
44+
"unsafe",
45+
"use",
46+
"while",
47+
"continue",
48+
"box",
49+
"const",
50+
"where",
51+
"virtual",
52+
"proc",
53+
"alignof",
54+
"become",
55+
"offsetof",
56+
"priv",
57+
"pure",
58+
"sizeof",
59+
"typeof",
60+
"unsized",
61+
"yield",
62+
"do",
63+
"abstract",
64+
"final",
65+
"override",
66+
"macro",
67+
];
68+
RUST_KEYWORDS.contains(&ident)
69+
}
70+
71+
// reimplementation based on https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/scope.rs#L26
72+
// it only implements the `find_message` method with not extra dependencies
73+
pub struct RootScope<'a> {
74+
pub file_descriptors: &'a [FileDescriptorProto],
75+
}
76+
77+
// re-implementation of https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/scope.rs#L340
78+
// also based on https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/scope.rs#L156
79+
pub struct ScopedMessage<'a> {
80+
pub fd: &'a FileDescriptorProto,
81+
pub path: Vec<&'a DescriptorProto>,
82+
pub msg: &'a DescriptorProto,
83+
}
84+
85+
impl ScopedMessage<'_> {
86+
pub fn prefix(&self) -> String {
87+
let mut prefix = String::new();
88+
for m in &self.path {
89+
prefix.push_str(m.name());
90+
prefix.push('.');
91+
}
92+
prefix
93+
}
94+
95+
// rust type name prefix for this scope
96+
pub fn rust_prefix(&self) -> String {
97+
self.prefix().replace(".", "_")
98+
}
99+
100+
// rust type name of this descriptor
101+
pub fn rust_name(&self) -> String {
102+
let mut r = self.rust_prefix();
103+
// Only escape if prefix is not empty
104+
if r.is_empty() && is_rust_keyword(self.msg.name()) {
105+
r.push_str("message_");
106+
}
107+
r.push_str(self.msg.name());
108+
r
109+
}
110+
111+
// fully-qualified name of this type
112+
pub fn rust_fq_name(&self) -> String {
113+
format!(
114+
"{}::{}",
115+
super::proto_path_to_rust_mod(self.fd.name()),
116+
self.rust_name()
117+
)
118+
}
119+
}
120+
121+
impl<'a> RootScope<'a> {
122+
pub fn find_message(&'a self, fqn: impl AsRef<str>) -> ScopedMessage<'a> {
123+
let Some(fqn1) = fqn.as_ref().strip_prefix(".") else {
124+
panic!("name must start with dot: {}", fqn.as_ref())
125+
};
126+
for fd in self.file_descriptors {
127+
let mut fqn2 = match fqn1.strip_prefix(fd.package()) {
128+
Some(rest) if fd.package().is_empty() => rest,
129+
Some(rest) if rest.starts_with(".") => &rest[1..],
130+
_ => continue,
131+
};
132+
133+
assert!(!fqn2.starts_with("."));
134+
135+
let mut pending = Some(fd.message_type.as_slice());
136+
let mut path = vec![];
137+
while let Some(msgs) = pending.take() {
138+
for msg in msgs {
139+
fqn2 = match fqn2.strip_prefix(msg.name()) {
140+
Some("") => return ScopedMessage { msg, path, fd },
141+
Some(rest) if rest.starts_with(".") => &rest[1..],
142+
_ => continue,
143+
};
144+
path.push(msg);
145+
pending = Some(&msg.nested_type);
146+
break;
147+
}
148+
}
149+
}
150+
panic!("enum not found by name: {}", fqn.as_ref())
151+
}
152+
}

compiler/src/util/writer.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! This module contains functionalities that where previously available in
2+
//! the protobuf / protobuf-codegen crates, but were then removed.
3+
//! The missing functionalities have been reimplemented in this module.
4+
5+
// adapted from https://github.com/stepancheg/rust-protobuf/blob/v3.7.2/protobuf-codegen/src/gen/code_writer.rs#L12
6+
#[derive(Default)]
7+
pub struct CodeWriter {
8+
writer: String,
9+
indent: String,
10+
}
11+
12+
impl CodeWriter {
13+
pub fn new() -> CodeWriter {
14+
Self::default()
15+
}
16+
17+
pub fn code(&self) -> &str {
18+
&self.writer
19+
}
20+
21+
pub fn take_code(&mut self) -> String {
22+
std::mem::take(&mut self.writer)
23+
}
24+
25+
pub fn write_line(&mut self, line: impl AsRef<str>) {
26+
if line.as_ref().is_empty() {
27+
self.writer.push('\n');
28+
} else {
29+
self.writer.push_str(&self.indent);
30+
self.writer.push_str(line.as_ref());
31+
self.writer.push('\n');
32+
}
33+
}
34+
35+
pub fn block(
36+
&mut self,
37+
first_line: impl AsRef<str>,
38+
last_line: impl AsRef<str>,
39+
cb: impl FnOnce(&mut CodeWriter),
40+
) {
41+
self.write_line(first_line);
42+
self.indented(cb);
43+
self.write_line(last_line);
44+
}
45+
46+
pub fn expr_block(&mut self, prefix: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
47+
self.block(format!("{} {{", prefix.as_ref()), "}", cb);
48+
}
49+
50+
pub fn indented(&mut self, cb: impl FnOnce(&mut CodeWriter)) {
51+
self.indent.push_str(" ");
52+
cb(self);
53+
self.indent.truncate(self.indent.len() - 4);
54+
}
55+
56+
pub fn pub_fn(&mut self, sig: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
57+
self.expr_block(format!("pub fn {}", sig.as_ref()), cb)
58+
}
59+
60+
pub fn def_fn(&mut self, sig: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
61+
self.expr_block(format!("fn {}", sig.as_ref()), cb)
62+
}
63+
64+
pub fn pub_struct(&mut self, name: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
65+
self.expr_block(format!("pub struct {}", name.as_ref()), cb);
66+
}
67+
68+
pub fn field_decl(&mut self, name: impl AsRef<str>, field_type: impl AsRef<str>) {
69+
self.write_line(format!("{}: {},", name.as_ref(), field_type.as_ref()));
70+
}
71+
72+
pub fn impl_self_block(&mut self, name: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
73+
self.expr_block(format!("impl {}", name.as_ref()), cb);
74+
}
75+
76+
pub fn pub_trait(&mut self, name: impl AsRef<str>, cb: impl FnOnce(&mut CodeWriter)) {
77+
self.expr_block(format!("pub trait {}", name.as_ref()), cb);
78+
}
79+
}

example/Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ homepage = "https://github.com/alipay/ttrpc-rust"
1111
description = "An example of ttrpc."
1212

1313
[dev-dependencies]
14-
protobuf = "3.1.0"
14+
protobuf = "3.7.2"
1515
bytes = "0.4.11"
1616
libc = "0.2.79"
1717
byteorder = "1.3.2"
@@ -51,7 +51,3 @@ path = "./async-stream-client.rs"
5151

5252
[build-dependencies]
5353
ttrpc-codegen = { path = "../ttrpc-codegen"}
54-
55-
[patch.crates-io]
56-
ttrpc-compiler = { path = "../compiler"}
57-

ttrpc-codegen/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ readme = "README.md"
1515
[dependencies]
1616
# lock home to avoid conflict with latest version
1717
home = "=0.5.9"
18-
protobuf-support = "3.2.0"
19-
protobuf = { version = "2.27.1" }
20-
protobuf-codegen = "3.5.1"
21-
ttrpc-compiler = "0.7.0"
18+
protobuf-support = "3.7.2"
19+
protobuf = "3.7.2"
20+
protobuf-codegen = "3.7.2"
21+
ttrpc-compiler = { version = "0.7.0", path = "../compiler" }

0 commit comments

Comments
 (0)