Skip to content
This repository was archived by the owner on Jun 4, 2023. It is now read-only.

Commit ab5380a

Browse files
committed
ttrpc: upgrade protobuf from 2.x to 3.x
Upgrade dep protobuf of ttrpc from 2.x to 3.x Signed-off-by: Tim Zhang <[email protected]>
1 parent c3e4d6d commit ab5380a

File tree

9 files changed

+41
-28
lines changed

9 files changed

+41
-28
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ homepage = "https://github.com/containerd/ttrpc-rust"
1111
description = "A Rust version of ttrpc."
1212

1313
[dependencies]
14-
protobuf = { version = "2.0" }
14+
protobuf = { version = "3.1.0" }
1515
libc = { version = "0.2.59", features = [ "extra_traits" ] }
1616
nix = "0.23.0"
1717
log = "0.4"
@@ -26,7 +26,7 @@ futures = { version = "0.3", optional = true }
2626
tokio-vsock = { version = "0.3.1", optional = true }
2727

2828
[build-dependencies]
29-
protobuf-codegen-pure = "2.14.0"
29+
protobuf-codegen = "3.1.0"
3030

3131
[features]
3232
default = ["sync"]

build.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ fn main() {
77
let path: PathBuf = [out_dir.clone(), "mod.rs".to_string()].iter().collect();
88
fs::write(path, "pub mod ttrpc;").unwrap();
99

10-
protobuf_codegen_pure::Codegen::new()
10+
let customize = protobuf_codegen::Customize::default()
11+
.gen_mod_rs(false)
12+
.generate_accessors(true);
13+
14+
protobuf_codegen::Codegen::new()
15+
.pure()
1116
.out_dir(out_dir)
1217
.inputs(&["src/ttrpc.proto"])
1318
.include("src")
19+
.customize(customize)
1420
.run()
1521
.expect("Codegen failed.");
1622
}

src/asynchronous/client.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl Client {
7070

7171
let msg: GenMessage = Message::new_request(stream_id, req)
7272
.try_into()
73-
.map_err(|e: protobuf::error::ProtobufError| Error::Others(e.to_string()))?;
73+
.map_err(|e: protobuf::Error| Error::Others(e.to_string()))?;
7474

7575
let (tx, mut rx): (ResultSender, ResultReceiver) = mpsc::channel(100);
7676

@@ -100,8 +100,8 @@ impl Client {
100100
let res = Response::decode(&msg.payload)
101101
.map_err(err_to_others_err!(e, "Unpack response error "))?;
102102

103-
let status = res.get_status();
104-
if status.get_code() != Code::OK {
103+
let status = res.status();
104+
if status.code() != Code::OK {
105105
return Err(Error::RpcStatus((*status).clone()));
106106
}
107107

@@ -119,7 +119,7 @@ impl Client {
119119

120120
let mut msg: GenMessage = Message::new_request(stream_id, req)
121121
.try_into()
122-
.map_err(|e: protobuf::error::ProtobufError| Error::Others(e.to_string()))?;
122+
.map_err(|e: protobuf::Error| Error::Others(e.to_string()))?;
123123

124124
if streaming_client {
125125
msg.header.add_flags(FLAG_REMOTE_OPEN);

src/asynchronous/stream.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ impl StreamReceiver {
454454
let resp = Response::decode(&msg.payload)
455455
.map_err(err_to_others_err!(e, "Decode message failed."))?;
456456
if let Some(status) = resp.status.as_ref() {
457-
if status.get_code() != Code::OK {
457+
if status.code() != Code::OK {
458458
return Err(Error::RpcStatus((*status).clone()));
459459
}
460460
}

src/asynchronous/utils.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,15 @@ macro_rules! async_duplex_streamimg_handler {
145145
#[macro_export]
146146
macro_rules! async_client_request {
147147
($self: ident, $ctx: ident, $req: ident, $server: expr, $method: expr, $cres: ident) => {
148-
let mut creq = ::ttrpc::Request::new();
149-
creq.set_service($server.to_string());
150-
creq.set_method($method.to_string());
151-
creq.set_timeout_nano($ctx.timeout_nano);
152-
let md = ::ttrpc::context::to_pb($ctx.metadata);
153-
creq.set_metadata(md);
154-
creq.payload.reserve($req.compute_size() as usize);
148+
let mut creq = ttrpc::Request {
149+
service: $server.to_string(),
150+
method: $method.to_string(),
151+
timeout_nano: $ctx.timeout_nano,
152+
metadata: ttrpc::context::to_pb($ctx.metadata),
153+
payload: Vec::with_capacity($req.compute_size() as usize),
154+
..Default::default()
155+
};
156+
155157
{
156158
let mut s = CodedOutputStream::vec(&mut creq.payload);
157159
$req.write_to(&mut s)

src/context.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Context {
4848
}
4949
}
5050

51-
pub fn from_pb(kvs: &protobuf::RepeatedField<KeyValue>) -> HashMap<String, Vec<String>> {
51+
pub fn from_pb(kvs: &Vec<KeyValue>) -> HashMap<String, Vec<String>> {
5252
let mut meta: HashMap<String, Vec<String>> = HashMap::new();
5353
for kv in kvs {
5454
if let Some(ref mut vl) = meta.get_mut(&kv.key) {
@@ -60,8 +60,9 @@ pub fn from_pb(kvs: &protobuf::RepeatedField<KeyValue>) -> HashMap<String, Vec<S
6060
meta
6161
}
6262

63-
pub fn to_pb(kvs: HashMap<String, Vec<String>>) -> protobuf::RepeatedField<KeyValue> {
64-
let mut meta: protobuf::RepeatedField<KeyValue> = protobuf::RepeatedField::default();
63+
pub fn to_pb(kvs: HashMap<String, Vec<String>>) -> Vec<KeyValue> {
64+
let mut meta = Vec::with_capacity(kvs.len());
65+
6566
for (k, vl) in kvs {
6667
for v in vl {
6768
let key = KeyValue {
@@ -72,6 +73,7 @@ pub fn to_pb(kvs: HashMap<String, Vec<String>>) -> protobuf::RepeatedField<KeyVa
7273
meta.push(key);
7374
}
7475
}
76+
7577
meta
7678
}
7779

@@ -83,7 +85,7 @@ mod tests {
8385
#[test]
8486
fn test_metadata() {
8587
// RepeatedField -> HashMap, test from_pb()
86-
let mut src: protobuf::RepeatedField<KeyValue> = protobuf::RepeatedField::default();
88+
let mut src = Vec::new();
8789
for i in &[
8890
("key1", "value1-1"),
8991
("key1", "value1-2"),
@@ -108,8 +110,7 @@ mod tests {
108110
assert_eq!(dst.get("key3"), None);
109111

110112
// HashMap -> RepeatedField , test to_pb()
111-
let src = context::to_pb(dst);
112-
let mut kvs = src.into_vec();
113+
let mut kvs = context::to_pb(dst);
113114
kvs.sort_by(|a, b| a.key.partial_cmp(&b.key).unwrap());
114115

115116
assert_eq!(kvs.len(), 3);

src/proto.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,18 @@ pub trait Codec {
215215
}
216216

217217
impl<M: protobuf::Message> Codec for M {
218-
type E = protobuf::error::ProtobufError;
218+
type E = protobuf::Error;
219219

220220
fn size(&self) -> u32 {
221-
self.compute_size()
221+
self.compute_size() as u32
222222
}
223223

224224
fn encode(&self) -> Result<Vec<u8>, Self::E> {
225225
let mut buf = vec![0; self.compute_size() as usize];
226226
let mut s = CodedOutputStream::bytes(&mut buf);
227227
self.write_to(&mut s)?;
228228
s.flush()?;
229+
drop(s);
229230
Ok(buf)
230231
}
231232

@@ -380,12 +381,11 @@ mod tests {
380381
creq.set_service("grpc.TestServices".to_string());
381382
creq.set_method("Test".to_string());
382383
creq.set_timeout_nano(20 * 1000 * 1000);
383-
let mut meta: protobuf::RepeatedField<KeyValue> = protobuf::RepeatedField::default();
384-
meta.push(KeyValue {
384+
let meta = vec![KeyValue {
385385
key: "test_key1".to_string(),
386386
value: "test_value1".to_string(),
387387
..Default::default()
388-
});
388+
}];
389389
creq.set_metadata(meta);
390390
creq.payload = vec![0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9];
391391
creq

src/sync/client.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ impl Client {
231231
let res =
232232
Response::decode(&buf).map_err(err_to_others_err!(e, "Unpack response error "))?;
233233

234-
let status = res.get_status();
235-
if status.get_code() != Code::OK {
234+
let status = res.status();
235+
if status.code() != Code::OK {
236236
return Err(Error::RpcStatus((*status).clone()));
237237
}
238238

src/sync/utils.rs

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub fn response_to_channel(
2020
res.write_to(&mut s).map_err(err_to_others_err!(e, ""))?;
2121
s.flush().map_err(err_to_others_err!(e, ""))?;
2222

23+
drop(s);
24+
2325
let mh = MessageHeader {
2426
length: buf.len() as u32,
2527
stream_id,
@@ -82,6 +84,8 @@ macro_rules! client_request {
8284
.map_err(::ttrpc::err_to_others!(e, ""))?;
8385
s.flush().map_err(::ttrpc::err_to_others!(e, ""))?;
8486

87+
drop(s);
88+
8589
let res = $self.client.request(creq)?;
8690
let mut s = CodedInputStream::from_bytes(&res.payload);
8791
$cres

0 commit comments

Comments
 (0)