From 446f0ce0a70da2acbdfe323c48e58863695174c9 Mon Sep 17 00:00:00 2001 From: Dan Burkert Date: Mon, 10 Jul 2017 20:01:33 -0700 Subject: [PATCH] Use 'dummy module' instead of 'dummy const' in derive output 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]: https://github.com/rust-lang/rust/issues/36922 [3]: https://github.com/serde-rs/serde/issues/159#issuecomment-212296130 [4]: https://botbot.me/mozilla/rust/2017-07-11/?msg=88402326&page=3 --- prost-derive/src/prost-derive.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/prost-derive/src/prost-derive.rs b/prost-derive/src/prost-derive.rs index 84344c32b..3bb1cf05a 100644 --- a/prost-derive/src/prost-derive.rs +++ b/prost-derive/src/prost-derive.rs @@ -73,7 +73,8 @@ fn try_message(input: TokenStream) -> Result { 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)| { @@ -115,13 +116,11 @@ fn try_message(input: TokenStream) -> Result { }; 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(&self, buf: &mut B) where B: _bytes::BufMut { #(#encode)* @@ -141,7 +140,6 @@ fn try_message(input: TokenStream) -> Result { } } - #[automatically_derived] impl Default for #ident { fn default() -> #ident { #ident { @@ -149,9 +147,9 @@ fn try_message(input: TokenStream) -> Result { } } } - }; - #methods + #methods + }; }; expanded.parse::().map_err(|err| Error::from(format!("{:?}", err))) @@ -196,7 +194,8 @@ 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))); @@ -204,12 +203,11 @@ pub fn enumeration(input: TokenStream) -> TokenStream { 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] @@ -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 @@ -296,7 +292,8 @@ fn try_oneof(input: TokenStream) -> Result { 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")); @@ -320,8 +317,8 @@ fn try_oneof(input: TokenStream) -> Result { }); 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;