Skip to content

Commit 00bb1ca

Browse files
committed
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]: rust-lang/rust#36922 [3]: serde-rs/serde#159 (comment) [4]: https://botbot.me/mozilla/rust/2017-07-11/?msg=88402326&page=3
1 parent eb15ca8 commit 00bb1ca

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

prost-derive/src/prost-derive.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ fn try_message(input: TokenStream) -> Result<TokenStream> {
7373
bail!("message {} has fields with duplicate tags", ident);
7474
}
7575

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

7879
let encoded_len = fields.iter()
7980
.map(|&(ref field_ident, ref field)| {
@@ -115,13 +116,12 @@ fn try_message(input: TokenStream) -> Result<TokenStream> {
115116
};
116117

117118
let expanded = quote! {
118-
#[allow(non_upper_case_globals, unused_attributes)]
119-
const #dummy_const: () = {
120-
119+
#[allow(non_snake_case, unused_attributes)]
120+
mod #module {
121121
extern crate prost as _prost;
122122
extern crate bytes as _bytes;
123+
use super::*;
123124

124-
#[automatically_derived]
125125
impl _prost::Message for #ident {
126126
fn encode_raw<B>(&self, buf: &mut B) where B: _bytes::BufMut {
127127
#(#encode)*
@@ -141,17 +141,16 @@ fn try_message(input: TokenStream) -> Result<TokenStream> {
141141
}
142142
}
143143

144-
#[automatically_derived]
145144
impl Default for #ident {
146145
fn default() -> #ident {
147146
#ident {
148147
#(#default)*
149148
}
150149
}
151150
}
152-
};
153151

154-
#methods
152+
#methods
153+
};
155154
};
156155

157156
expanded.parse::<TokenStream>().map_err(|err| Error::from(format!("{:?}", err)))
@@ -196,20 +195,21 @@ pub fn enumeration(input: TokenStream) -> TokenStream {
196195

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

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

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

206206
let expanded = quote! {
207-
#[allow(non_upper_case_globals, unused_attributes)]
208-
const #dummy_const: () = {
207+
#[allow(non_snake_case, unused_attributes)]
208+
mod #module {
209209
extern crate bytes as _bytes;
210210
extern crate prost as _prost;
211+
use super::*;
211212

212-
#[automatically_derived]
213213
impl #ident {
214214

215215
#[doc=#is_valid_doc]
@@ -229,14 +229,12 @@ pub fn enumeration(input: TokenStream) -> TokenStream {
229229
}
230230
}
231231

232-
#[automatically_derived]
233232
impl ::std::default::Default for #ident {
234233
fn default() -> #ident {
235234
#ident::#default
236235
}
237236
}
238237

239-
#[automatically_derived]
240238
impl ::std::convert::From<#ident> for i32 {
241239
fn from(value: #ident) -> i32 {
242240
value as i32
@@ -296,7 +294,8 @@ fn try_oneof(input: TokenStream) -> Result<TokenStream> {
296294
panic!("invalid oneof {}: variants have duplicate tags", ident);
297295
}
298296

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

301300
let encode = fields.iter().map(|&(ref variant_ident, ref field)| {
302301
let encode = field.encode(&Ident::new("*value"));
@@ -320,10 +319,11 @@ fn try_oneof(input: TokenStream) -> Result<TokenStream> {
320319
});
321320

322321
let expanded = quote! {
323-
#[allow(non_upper_case_globals, unused_attributes)]
324-
const #dummy_const: () = {
322+
#[allow(non_snake_case, unused_attributes)]
323+
mod #module {
325324
extern crate bytes as _bytes;
326325
extern crate prost as _prost;
326+
use super::*;
327327

328328
impl #ident {
329329
pub fn encode<B>(&self, buf: &mut B) where B: _bytes::BufMut {

0 commit comments

Comments
 (0)