Skip to content

Commit

Permalink
Fix derive macro build error when using map and bytes together (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioFranco committed Oct 13, 2021
1 parent 1825b38 commit 75e87f3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
11 changes: 11 additions & 0 deletions prost-derive/src/field/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>);
Expand Down
1 change: 1 addition & 0 deletions tests/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ fn main() {
}

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

Expand Down
9 changes: 7 additions & 2 deletions tests/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { \
Expand All @@ -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]} \
}"
);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/src/message_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ pub struct Basic {

#[prost(oneof = "BasicOneof", tags = "8, 9")]
pub oneof: Option<BasicOneof>,

#[prost(map = "string, bytes", tag = "12")]
#[cfg(feature = "std")]
pub bytes_map: ::std::collections::HashMap<String, Vec<u8>>,
}

#[derive(Clone, PartialEq, Message)]
Expand Down
6 changes: 6 additions & 0 deletions tests/src/well_known_types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, bytes> bytes_dict = 2;
}

0 comments on commit 75e87f3

Please sign in to comment.