Skip to content

Commit 6079136

Browse files
Fixes 905, fix: Use full path of Result type + unit test + Option unti test (#917)
* add cmake and ninja in the flake * unit-test added to replicate issue #905 * fix: Use full path of `Result` type Fixes #905 * add unit-test for Option to ensure that there is no type resolution collision between Rust Option and a user type defined with name Option, in code generation --------- Co-authored-by: Lucio Franco <luciofranco14@gmail.com>
1 parent bf86646 commit 6079136

10 files changed

Lines changed: 79 additions & 8 deletions

File tree

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{
1515
devShells.default = pkgs.mkShell {
1616
packages = with pkgs; [ cargo rustc ];
17-
buildInputs = with pkgs; [ pkg-config protobuf curl ];
17+
buildInputs = with pkgs; [ pkg-config protobuf curl cmake ninja ];
1818
};
1919
});
2020
}

prost-derive/src/field/map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,14 @@ impl Field {
276276
#[doc=#get_doc]
277277
pub fn #get(&self, key: #key_ref_ty) -> ::core::option::Option<#ty> {
278278
self.#ident.get(#take_ref key).cloned().and_then(|x| {
279-
let result: Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
279+
let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
280280
result.ok()
281281
})
282282
}
283283
#[doc=#insert_doc]
284284
pub fn #insert(&mut self, key: #key_ty, value: #ty) -> ::core::option::Option<#ty> {
285285
self.#ident.insert(key, value as i32).and_then(|x| {
286-
let result: Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
286+
let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
287287
result.ok()
288288
})
289289
}

prost-derive/src/field/scalar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl Field {
219219
struct #wrap_name<'a>(&'a i32);
220220
impl<'a> ::core::fmt::Debug for #wrap_name<'a> {
221221
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
222-
let res: Result<#ty, _> = ::core::convert::TryFrom::try_from(*self.0);
222+
let res: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(*self.0);
223223
match res {
224224
Err(_) => ::core::fmt::Debug::fmt(&self.0, f),
225225
Ok(en) => ::core::fmt::Debug::fmt(&en, f),
@@ -316,7 +316,7 @@ impl Field {
316316
#[doc=#get_doc]
317317
pub fn #get(&self) -> #ty {
318318
self.#ident.and_then(|x| {
319-
let result: Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
319+
let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
320320
result.ok()
321321
}).unwrap_or(#default)
322322
}
@@ -341,7 +341,7 @@ impl Field {
341341
fn(i32) -> ::core::option::Option<#ty>,
342342
> {
343343
self.#ident.iter().cloned().filter_map(|x| {
344-
let result: Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
344+
let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
345345
result.ok()
346346
})
347347
}

tests/src/build.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ fn main() {
9595
.compile_protos(&[src.join("custom_debug.proto")], includes)
9696
.unwrap();
9797

98+
config
99+
.compile_protos(&[src.join("result_enum.proto")], includes)
100+
.unwrap();
101+
102+
config
103+
.compile_protos(&[src.join("result_struct.proto")], includes)
104+
.unwrap();
105+
106+
config
107+
.compile_protos(&[src.join("option_enum.proto")], includes)
108+
.unwrap();
109+
110+
config
111+
.compile_protos(&[src.join("option_struct.proto")], includes)
112+
.unwrap();
113+
98114
prost_build::Config::new()
99115
.protoc_arg("--experimental_allow_proto3_optional")
100116
.compile_protos(&[src.join("proto3_presence.proto")], includes)

tests/src/lib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,22 @@ mod no_unused_results;
4545
#[cfg(test)]
4646
#[cfg(feature = "std")]
4747
mod skip_debug;
48-
#[cfg(test)]
49-
mod well_known_types;
48+
49+
mod test_enum_named_option_value {
50+
include!(concat!(env!("OUT_DIR"), "/myenum.optionn.rs"));
51+
}
52+
53+
mod test_enum_named_result_value {
54+
include!(concat!(env!("OUT_DIR"), "/myenum.result.rs"));
55+
}
56+
57+
mod test_result_named_option_value {
58+
include!(concat!(env!("OUT_DIR"), "/mystruct.optionn.rs"));
59+
}
60+
61+
mod test_result_named_result_value {
62+
include!(concat!(env!("OUT_DIR"), "/mystruct.result.rs"));
63+
}
5064

5165
pub mod foo {
5266
pub mod bar_baz {

tests/src/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
include!(concat!(env!("OUT_DIR"), "/myenum.result.rs"));
3+
include!(concat!(env!("OUT_DIR"), "/mymessage.result.rs"));

tests/src/option_enum.proto

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
syntax = "proto3";
2+
package myenum.optionn;
3+
4+
5+
enum Option {
6+
HELLO = 0;
7+
}
8+
9+
message FailMessage {
10+
Option result = 1;
11+
}

tests/src/option_struct.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
syntax = "proto3";
2+
package mystruct.optionn;
3+
4+
5+
message Option {
6+
string msg = 1;
7+
}
8+

tests/src/result_enum.proto

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
syntax = "proto3";
2+
package myenum.result;
3+
4+
5+
enum Result {
6+
HELLO = 0;
7+
}
8+
9+
message FailMessage {
10+
Result result = 1;
11+
}

tests/src/result_struct.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
syntax = "proto3";
2+
package mystruct.result;
3+
4+
5+
message Result {
6+
string msg = 1;
7+
}
8+

0 commit comments

Comments
 (0)