From 75e87f32fcbf5b2b94baf109245a455bc76db09c Mon Sep 17 00:00:00 2001 From: Lucio Franco Date: Wed, 13 Oct 2021 16:17:36 -0400 Subject: [PATCH] Fix derive macro build error when using map and bytes together (#546) --- prost-derive/src/field/map.rs | 11 +++++++++++ tests/src/build.rs | 1 + tests/src/debug.rs | 9 +++++++-- tests/src/message_encoding.rs | 4 ++++ tests/src/well_known_types.proto | 6 ++++++ 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/prost-derive/src/field/map.rs b/prost-derive/src/field/map.rs index 1228a6fae..608b9b0b1 100644 --- a/prost-derive/src/field/map.rs +++ b/prost-derive/src/field/map.rs @@ -319,6 +319,17 @@ impl Field { }; match &self.value_ty { ValueTy::Scalar(ty) => { + if let &scalar::Ty::Bytes(_) = ty { + return quote! { + struct #wrapper_name<'a>(&'a dyn ::core::fmt::Debug); + impl<'a> ::core::fmt::Debug for #wrapper_name<'a> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + self.0.fmt(f) + } + } + }; + } + let value = ty.rust_type(); quote! { struct #wrapper_name<'a>(&'a ::#libname::collections::#type_name<#key, #value>); diff --git a/tests/src/build.rs b/tests/src/build.rs index 522768b6a..334bdc8f4 100644 --- a/tests/src/build.rs +++ b/tests/src/build.rs @@ -105,6 +105,7 @@ fn main() { } config + .bytes(&["."]) .compile_protos(&[src.join("well_known_types.proto")], includes) .unwrap(); diff --git a/tests/src/debug.rs b/tests/src/debug.rs index ae5729f8e..23031e434 100644 --- a/tests/src/debug.rs +++ b/tests/src/debug.rs @@ -27,13 +27,17 @@ fn basic() { string_map: {}, \ enumeration_btree_map: {}, \ string_btree_map: {}, \ - oneof: None \ + oneof: None, \ + bytes_map: {} \ }" ); basic .enumeration_map .insert(0, BasicEnumeration::TWO as i32); basic.enumeration = 42; + basic + .bytes_map + .insert("hello".to_string(), "world".as_bytes().into()); assert_eq!( format!("{:?}", basic), "Basic { \ @@ -46,7 +50,8 @@ fn basic() { string_map: {}, \ enumeration_btree_map: {}, \ string_btree_map: {}, \ - oneof: None \ + oneof: None, \ + bytes_map: {\"hello\": [119, 111, 114, 108, 100]} \ }" ); } diff --git a/tests/src/message_encoding.rs b/tests/src/message_encoding.rs index 367c0f837..3726e6762 100644 --- a/tests/src/message_encoding.rs +++ b/tests/src/message_encoding.rs @@ -359,6 +359,10 @@ pub struct Basic { #[prost(oneof = "BasicOneof", tags = "8, 9")] pub oneof: Option, + + #[prost(map = "string, bytes", tag = "12")] + #[cfg(feature = "std")] + pub bytes_map: ::std::collections::HashMap>, } #[derive(Clone, PartialEq, Message)] diff --git a/tests/src/well_known_types.proto b/tests/src/well_known_types.proto index ef4047947..be0d8b7c2 100644 --- a/tests/src/well_known_types.proto +++ b/tests/src/well_known_types.proto @@ -30,3 +30,9 @@ message Foo { google.protobuf.BytesValue bytes = 11; } + +// https://github.com/tokio-rs/prost/issues/531 +message Test { + bytes bytes = 1; + map bytes_dict = 2; +} \ No newline at end of file