Skip to content

Commit

Permalink
Merge branch 'master' into builders
Browse files Browse the repository at this point in the history
  • Loading branch information
mzabaluev committed Sep 13, 2023
2 parents 89fbd7e + b258dc8 commit b5f549b
Show file tree
Hide file tree
Showing 15 changed files with 416 additions and 60 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "prost"
version = "0.11.9"
version = "0.12.0"
authors = [
"Dan Burkert <dan@danburkert.com>",
"Lucio Franco <luciofranco14@gmail.com",
Expand Down Expand Up @@ -48,7 +48,7 @@ std = []

[dependencies]
bytes = { version = "1", default-features = false }
prost-derive = { version = "0.11.9", path = "prost-derive", optional = true }
prost-derive = { version = "0.12.0", path = "prost-derive", optional = true }

[dev-dependencies]
criterion = { version = "0.4", default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -30,9 +30,9 @@ First, add `prost` and its public dependencies to your `Cargo.toml`:

```ignore
[dependencies]
prost = "0.11"
prost = "0.12"
# Only necessary if using Protobuf well-known types:
prost-types = "0.11"
prost-types = "0.12"
```

The recommended way to add `.proto` compilation to a Cargo project is to use the
Expand Down Expand Up @@ -62,7 +62,7 @@ With `prost-build` v0.11 release, `protoc` will be required to invoke
bundled a `protoc` or attempt to compile `protoc` for users. For install
instructions for `protoc` please check out the [protobuf install] instructions.

[protobuf install]: https://github.com/protocolbuffers/protobuf#protocol-compiler-installation
[protobuf install]: https://github.com/protocolbuffers/protobuf#protobuf-compiler-installation


### Packages
Expand Down
6 changes: 3 additions & 3 deletions prost-build/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "prost-build"
version = "0.11.9"
version = "0.12.0"
authors = [
"Dan Burkert <dan@danburkert.com>",
"Lucio Franco <luciofranco14@gmail.com>",
Expand All @@ -27,8 +27,8 @@ itertools = { version = ">=0.10, <0.12", default-features = false, features = ["
log = "0.4"
multimap = { version = "0.8", default-features = false }
petgraph = { version = "0.6", default-features = false }
prost = { version = "0.11.9", path = "..", default-features = false }
prost-types = { version = "0.11.9", path = "../prost-types", default-features = false }
prost = { version = "0.12.0", path = "..", default-features = false }
prost-types = { version = "0.12.0", path = "../prost-types", default-features = false }
tempfile = "3"
once_cell = "1.17.1"
regex = { version = "1.8.1", default-features = false, features = ["std", "unicode-bool"] }
Expand Down
31 changes: 25 additions & 6 deletions prost-build/src/code_generator.rs
Expand Up @@ -243,6 +243,7 @@ impl<'a> CodeGenerator<'a> {
"#[derive(Clone, PartialEq, {}::Message)]\n",
prost_path(self.config)
));
self.append_skip_debug(&fq_message_name);
self.push_indent();
self.buf.push_str("pub struct ");
self.buf.push_str(&to_upper_camel(&message_name));
Expand Down Expand Up @@ -337,6 +338,19 @@ impl<'a> CodeGenerator<'a> {
}
}

fn should_skip_debug(&self, fq_message_name: &str) -> bool {
assert_eq!(b'.', fq_message_name.as_bytes()[0]);
self.config.skip_debug.get(fq_message_name).next().is_some()
}

fn append_skip_debug(&mut self, fq_message_name: &str) {
if self.should_skip_debug(fq_message_name) {
push_indent(self.buf, self.depth);
self.buf.push_str("#[prost(skip_debug)]");
self.buf.push('\n');
}
}

fn append_enum_attributes(&mut self, fq_message_name: &str) {
assert_eq!(b'.', fq_message_name.as_bytes()[0]);
for attribute in self.config.enum_attributes.get(fq_message_name) {
Expand Down Expand Up @@ -580,6 +594,7 @@ impl<'a> CodeGenerator<'a> {
"#[derive(Clone, PartialEq, {}::Oneof)]\n",
prost_path(self.config)
));
self.append_skip_debug(&fq_message_name);
self.push_indent();
self.buf.push_str("pub enum ");
self.buf.push_str(&to_upper_camel(oneof.proto.name()));
Expand Down Expand Up @@ -886,12 +901,16 @@ impl<'a> CodeGenerator<'a> {
self.append_type_attributes(&fq_proto_enum_name);
self.append_enum_attributes(&fq_proto_enum_name);
self.push_indent();
self.buf.push_str(
&format!(
"#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, {}::Enumeration)]\n",
prost_path(self.config)
),
);
let dbg = if self.should_skip_debug(&fq_proto_enum_name) {
""
} else {
"Debug, "
};
self.buf.push_str(&format!(
"#[derive(Clone, Copy, {}PartialEq, Eq, Hash, PartialOrd, Ord, {}::Enumeration)]\n",
dbg,
prost_path(self.config),
));
self.push_indent();
self.buf.push_str("#[repr(i32)]\n");
self.push_indent();
Expand Down
18 changes: 17 additions & 1 deletion prost-build/src/lib.rs
@@ -1,4 +1,4 @@
#![doc(html_root_url = "https://docs.rs/prost-build/0.11.9")]
#![doc(html_root_url = "https://docs.rs/prost-build/0.12.0")]
#![allow(clippy::option_as_ref_deref, clippy::format_push_string)]

//! `prost-build` compiles `.proto` files into Rust.
Expand Down Expand Up @@ -255,6 +255,7 @@ pub struct Config {
default_package_filename: String,
protoc_args: Vec<OsString>,
disable_comments: PathMap<()>,
skip_debug: PathMap<()>,
skip_protoc_run: bool,
include_file: Option<PathBuf>,
prost_path: Option<String>,
Expand Down Expand Up @@ -630,6 +631,19 @@ impl Config {
self
}

/// Skips generating `impl Debug` for types
pub fn skip_debug<I, S>(&mut self, paths: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
self.skip_debug.clear();
for matcher in paths {
self.skip_debug.insert(matcher.as_ref().to_string(), ());
}
self
}

/// Declare an externally provided Protobuf package or type.
///
/// `extern_path` allows `prost` types in external crates to be referenced in generated code.
Expand Down Expand Up @@ -1251,6 +1265,7 @@ impl default::Default for Config {
default_package_filename: "_".to_string(),
protoc_args: Vec::new(),
disable_comments: PathMap::default(),
skip_debug: PathMap::default(),
skip_protoc_run: false,
include_file: None,
prost_path: None,
Expand All @@ -1276,6 +1291,7 @@ impl fmt::Debug for Config {
.field("default_package_filename", &self.default_package_filename)
.field("protoc_args", &self.protoc_args)
.field("disable_comments", &self.disable_comments)
.field("skip_debug", &self.skip_debug)
.field("prost_path", &self.prost_path)
.field("builders", &self.builders)
.finish()
Expand Down
2 changes: 1 addition & 1 deletion prost-derive/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "prost-derive"
version = "0.11.9"
version = "0.12.0"
authors = [
"Dan Burkert <dan@danburkert.com>",
"Lucio Franco <luciofranco14@gmail.com>",
Expand Down
109 changes: 69 additions & 40 deletions prost-derive/src/lib.rs
@@ -1,4 +1,4 @@
#![doc(html_root_url = "https://docs.rs/prost-derive/0.11.9")]
#![doc(html_root_url = "https://docs.rs/prost-derive/0.12.0")]
// The `quote!` macro requires deep recursion.
#![recursion_limit = "4096"]

Expand All @@ -23,6 +23,12 @@ fn try_message(input: TokenStream) -> Result<TokenStream, Error> {

let ident = input.ident;

syn::custom_keyword!(skip_debug);
let skip_debug = input
.attrs
.into_iter()
.any(|a| a.path().is_ident("prost") && a.parse_args::<skip_debug>().is_ok());

let variant_data = match input.data {
Data::Struct(variant_data) => variant_data,
Data::Enum(..) => bail!("Message can not be derived for an enum"),
Expand Down Expand Up @@ -165,26 +171,6 @@ fn try_message(input: TokenStream) -> Result<TokenStream, Error> {
}
};

let debugs = unsorted_fields.iter().map(|&(ref field_ident, ref field)| {
let wrapper = field.debug(quote!(self.#field_ident));
let call = if is_struct {
quote!(builder.field(stringify!(#field_ident), &wrapper))
} else {
quote!(builder.field(&wrapper))
};
quote! {
let builder = {
let wrapper = #wrapper;
#call
};
}
});
let debug_builder = if is_struct {
quote!(f.debug_struct(stringify!(#ident)))
} else {
quote!(f.debug_tuple(stringify!(#ident)))
};

let expanded = quote! {
impl #impl_generics ::prost::Message for #ident #ty_generics #where_clause {
#[allow(unused_variables)]
Expand Down Expand Up @@ -223,14 +209,44 @@ fn try_message(input: TokenStream) -> Result<TokenStream, Error> {
#default
}
}
};
let expanded = if skip_debug {
expanded
} else {
let debugs = unsorted_fields.iter().map(|&(ref field_ident, ref field)| {
let wrapper = field.debug(quote!(self.#field_ident));
let call = if is_struct {
quote!(builder.field(stringify!(#field_ident), &wrapper))
} else {
quote!(builder.field(&wrapper))
};
quote! {
let builder = {
let wrapper = #wrapper;
#call
};
}
});
let debug_builder = if is_struct {
quote!(f.debug_struct(stringify!(#ident)))
} else {
quote!(f.debug_tuple(stringify!(#ident)))
};
quote! {
#expanded

impl #impl_generics ::core::fmt::Debug for #ident #ty_generics #where_clause {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let mut builder = #debug_builder;
#(#debugs;)*
builder.finish()
impl #impl_generics ::core::fmt::Debug for #ident #ty_generics #where_clause {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let mut builder = #debug_builder;
#(#debugs;)*
builder.finish()
}
}
}
};

let expanded = quote! {
#expanded

#methods
};
Expand Down Expand Up @@ -358,6 +374,12 @@ fn try_oneof(input: TokenStream) -> Result<TokenStream, Error> {

let ident = input.ident;

syn::custom_keyword!(skip_debug);
let skip_debug = input
.attrs
.into_iter()
.any(|a| a.path().is_ident("prost") && a.parse_args::<skip_debug>().is_ok());

let variants = match input.data {
Data::Enum(DataEnum { variants, .. }) => variants,
Data::Struct(..) => bail!("Oneof can not be derived for a struct"),
Expand Down Expand Up @@ -440,16 +462,6 @@ fn try_oneof(input: TokenStream) -> Result<TokenStream, Error> {
quote!(#ident::#variant_ident(ref value) => #encoded_len)
});

let debug = fields.iter().map(|&(ref variant_ident, ref field)| {
let wrapper = field.debug(quote!(*value));
quote!(#ident::#variant_ident(ref value) => {
let wrapper = #wrapper;
f.debug_tuple(stringify!(#variant_ident))
.field(&wrapper)
.finish()
})
});

let expanded = quote! {
impl #impl_generics #ident #ty_generics #where_clause {
/// Encodes the message to a buffer.
Expand Down Expand Up @@ -483,10 +495,27 @@ fn try_oneof(input: TokenStream) -> Result<TokenStream, Error> {
}
}

impl #impl_generics ::core::fmt::Debug for #ident #ty_generics #where_clause {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match *self {
#(#debug,)*
};
let expanded = if skip_debug {
expanded
} else {
let debug = fields.iter().map(|&(ref variant_ident, ref field)| {
let wrapper = field.debug(quote!(*value));
quote!(#ident::#variant_ident(ref value) => {
let wrapper = #wrapper;
f.debug_tuple(stringify!(#variant_ident))
.field(&wrapper)
.finish()
})
});
quote! {
#expanded

impl #impl_generics ::core::fmt::Debug for #ident #ty_generics #where_clause {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match *self {
#(#debug,)*
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions prost-types/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "prost-types"
version = "0.11.9"
version = "0.12.0"
authors = [
"Dan Burkert <dan@danburkert.com>",
"Lucio Franco <luciofranco14@gmail.com",
Expand All @@ -22,7 +22,7 @@ default = ["std"]
std = ["prost/std"]

[dependencies]
prost = { version = "0.11.9", path = "..", default-features = false, features = ["prost-derive"] }
prost = { version = "0.12.0", path = "..", default-features = false, features = ["prost-derive"] }

[dev-dependencies]
proptest = "1"

0 comments on commit b5f549b

Please sign in to comment.