Skip to content

Commit

Permalink
Fixes 905, fix: Use full path of Result type + unit test + Option unt…
Browse files Browse the repository at this point in the history
…i 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>
  • Loading branch information
anstylian and LucioFranco authored Sep 15, 2023
1 parent bf86646 commit 6079136
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{
devShells.default = pkgs.mkShell {
packages = with pkgs; [ cargo rustc ];
buildInputs = with pkgs; [ pkg-config protobuf curl ];
buildInputs = with pkgs; [ pkg-config protobuf curl cmake ninja ];
};
});
}
4 changes: 2 additions & 2 deletions prost-derive/src/field/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,14 @@ impl Field {
#[doc=#get_doc]
pub fn #get(&self, key: #key_ref_ty) -> ::core::option::Option<#ty> {
self.#ident.get(#take_ref key).cloned().and_then(|x| {
let result: Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
result.ok()
})
}
#[doc=#insert_doc]
pub fn #insert(&mut self, key: #key_ty, value: #ty) -> ::core::option::Option<#ty> {
self.#ident.insert(key, value as i32).and_then(|x| {
let result: Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
result.ok()
})
}
Expand Down
6 changes: 3 additions & 3 deletions prost-derive/src/field/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl Field {
struct #wrap_name<'a>(&'a i32);
impl<'a> ::core::fmt::Debug for #wrap_name<'a> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let res: Result<#ty, _> = ::core::convert::TryFrom::try_from(*self.0);
let res: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(*self.0);
match res {
Err(_) => ::core::fmt::Debug::fmt(&self.0, f),
Ok(en) => ::core::fmt::Debug::fmt(&en, f),
Expand Down Expand Up @@ -316,7 +316,7 @@ impl Field {
#[doc=#get_doc]
pub fn #get(&self) -> #ty {
self.#ident.and_then(|x| {
let result: Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
result.ok()
}).unwrap_or(#default)
}
Expand All @@ -341,7 +341,7 @@ impl Field {
fn(i32) -> ::core::option::Option<#ty>,
> {
self.#ident.iter().cloned().filter_map(|x| {
let result: Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
result.ok()
})
}
Expand Down
16 changes: 16 additions & 0 deletions tests/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ fn main() {
.compile_protos(&[src.join("custom_debug.proto")], includes)
.unwrap();

config
.compile_protos(&[src.join("result_enum.proto")], includes)
.unwrap();

config
.compile_protos(&[src.join("result_struct.proto")], includes)
.unwrap();

config
.compile_protos(&[src.join("option_enum.proto")], includes)
.unwrap();

config
.compile_protos(&[src.join("option_struct.proto")], includes)
.unwrap();

prost_build::Config::new()
.protoc_arg("--experimental_allow_proto3_optional")
.compile_protos(&[src.join("proto3_presence.proto")], includes)
Expand Down
18 changes: 16 additions & 2 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,22 @@ mod no_unused_results;
#[cfg(test)]
#[cfg(feature = "std")]
mod skip_debug;
#[cfg(test)]
mod well_known_types;

mod test_enum_named_option_value {
include!(concat!(env!("OUT_DIR"), "/myenum.optionn.rs"));
}

mod test_enum_named_result_value {
include!(concat!(env!("OUT_DIR"), "/myenum.result.rs"));
}

mod test_result_named_option_value {
include!(concat!(env!("OUT_DIR"), "/mystruct.optionn.rs"));
}

mod test_result_named_result_value {
include!(concat!(env!("OUT_DIR"), "/mystruct.result.rs"));
}

pub mod foo {
pub mod bar_baz {
Expand Down
3 changes: 3 additions & 0 deletions tests/src/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

include!(concat!(env!("OUT_DIR"), "/myenum.result.rs"));
include!(concat!(env!("OUT_DIR"), "/mymessage.result.rs"));
11 changes: 11 additions & 0 deletions tests/src/option_enum.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";
package myenum.optionn;


enum Option {
HELLO = 0;
}

message FailMessage {
Option result = 1;
}
8 changes: 8 additions & 0 deletions tests/src/option_struct.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";
package mystruct.optionn;


message Option {
string msg = 1;
}

11 changes: 11 additions & 0 deletions tests/src/result_enum.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";
package myenum.result;


enum Result {
HELLO = 0;
}

message FailMessage {
Result result = 1;
}
8 changes: 8 additions & 0 deletions tests/src/result_struct.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";
package mystruct.result;


message Result {
string msg = 1;
}

0 comments on commit 6079136

Please sign in to comment.