Skip to content

Commit

Permalink
Use 'dummy module' instead of 'dummy const' in derive output
Browse files Browse the repository at this point in the history
The 'dummy const' pattern was copied from SerDe[1], but it causes
rustdoc not to generate documentation due to a [known bug][2].
Apparently SerDe [considered][3] using a dummy module instead of a dummy
const, but decided against it due to private type issues. Since
everything's public in `prost`, we shouldn't have such an issue.

Also removes #[automatically_derived] annotations, since apparently the
no longer do anything[4].

Fixes #11

[1]: https://github.com/serde-rs/serde/blob/775e8154e7151eb1576d65df539c4ac1612595c6/serde_derive/src/ser.rs#L28
[2]: rust-lang/rust#36922
[3]: serde-rs/serde#159 (comment)
[4]: https://botbot.me/mozilla/rust/2017-07-11/?msg=88402326&page=3
  • Loading branch information
danburkert committed Jul 11, 2017
1 parent eb15ca8 commit 446f0ce
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions prost-derive/src/prost-derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ fn try_message(input: TokenStream) -> Result<TokenStream> {
bail!("message {} has fields with duplicate tags", ident);
}

let dummy_const = Ident::new(format!("_IMPL_MESSAGE_FOR_{}", ident));
// Put impls in a special module, so that 'extern crate' can be used.
let module = Ident::new(format!("{}_MESSAGE", ident));

let encoded_len = fields.iter()
.map(|&(ref field_ident, ref field)| {
Expand Down Expand Up @@ -115,13 +116,11 @@ fn try_message(input: TokenStream) -> Result<TokenStream> {
};

let expanded = quote! {
#[allow(non_upper_case_globals, unused_attributes)]
const #dummy_const: () = {

#[allow(non_snake_case, unused_attributes)]
mod #module {
extern crate prost as _prost;
extern crate bytes as _bytes;

#[automatically_derived]
impl _prost::Message for #ident {
fn encode_raw<B>(&self, buf: &mut B) where B: _bytes::BufMut {
#(#encode)*
Expand All @@ -141,17 +140,16 @@ fn try_message(input: TokenStream) -> Result<TokenStream> {
}
}

#[automatically_derived]
impl Default for #ident {
fn default() -> #ident {
#ident {
#(#default)*
}
}
}
};

#methods
#methods
};
};

expanded.parse::<TokenStream>().map_err(|err| Error::from(format!("{:?}", err)))
Expand Down Expand Up @@ -196,20 +194,20 @@ pub fn enumeration(input: TokenStream) -> TokenStream {

let default = variants[0].0.clone();

let dummy_const = Ident::new(format!("_IMPL_ENUMERATION_FOR_{}", ident));
// Put impls in a special module, so that 'extern crate' can be used.
let module = Ident::new(format!("{}_ENUMERATION", ident));
let is_valid = variants.iter().map(|&(_, ref value)| quote!(#value => true));
let from = variants.iter().map(|&(ref variant, ref value)| quote!(#value => ::std::option::Option::Some(#ident::#variant)));

let is_valid_doc = format!("Returns `true` if `value` is a variant of `{}`.", ident);
let from_i32_doc = format!("Converts an `i32` to a `{}`, or `None` if `value` is not a valid variant.", ident);

let expanded = quote! {
#[allow(non_upper_case_globals, unused_attributes)]
const #dummy_const: () = {
#[allow(non_snake_case, unused_attributes)]
mod #module {
extern crate bytes as _bytes;
extern crate prost as _prost;

#[automatically_derived]
impl #ident {

#[doc=#is_valid_doc]
Expand All @@ -229,14 +227,12 @@ pub fn enumeration(input: TokenStream) -> TokenStream {
}
}

#[automatically_derived]
impl ::std::default::Default for #ident {
fn default() -> #ident {
#ident::#default
}
}

#[automatically_derived]
impl ::std::convert::From<#ident> for i32 {
fn from(value: #ident) -> i32 {
value as i32
Expand Down Expand Up @@ -296,7 +292,8 @@ fn try_oneof(input: TokenStream) -> Result<TokenStream> {
panic!("invalid oneof {}: variants have duplicate tags", ident);
}

let dummy_const = Ident::new(format!("_IMPL_ONEOF_FOR_{}", ident));
// Put impls in a special module, so that 'extern crate' can be used.
let module = Ident::new(format!("{}_ONEOF", ident));

let encode = fields.iter().map(|&(ref variant_ident, ref field)| {
let encode = field.encode(&Ident::new("*value"));
Expand All @@ -320,8 +317,8 @@ fn try_oneof(input: TokenStream) -> Result<TokenStream> {
});

let expanded = quote! {
#[allow(non_upper_case_globals, unused_attributes)]
const #dummy_const: () = {
#[allow(non_snake_case, unused_attributes)]
mod #module {
extern crate bytes as _bytes;
extern crate prost as _prost;

Expand Down

0 comments on commit 446f0ce

Please sign in to comment.