Skip to content

Commit 446f0ce

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 446f0ce

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

prost-derive/src/prost-derive.rs

Lines changed: 14 additions & 17 deletions
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,11 @@ 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;
123123

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

144-
#[automatically_derived]
145143
impl Default for #ident {
146144
fn default() -> #ident {
147145
#ident {
148146
#(#default)*
149147
}
150148
}
151149
}
152-
};
153150

154-
#methods
151+
#methods
152+
};
155153
};
156154

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

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

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

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

206205
let expanded = quote! {
207-
#[allow(non_upper_case_globals, unused_attributes)]
208-
const #dummy_const: () = {
206+
#[allow(non_snake_case, unused_attributes)]
207+
mod #module {
209208
extern crate bytes as _bytes;
210209
extern crate prost as _prost;
211210

212-
#[automatically_derived]
213211
impl #ident {
214212

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

232-
#[automatically_derived]
233230
impl ::std::default::Default for #ident {
234231
fn default() -> #ident {
235232
#ident::#default
236233
}
237234
}
238235

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

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

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

322319
let expanded = quote! {
323-
#[allow(non_upper_case_globals, unused_attributes)]
324-
const #dummy_const: () = {
320+
#[allow(non_snake_case, unused_attributes)]
321+
mod #module {
325322
extern crate bytes as _bytes;
326323
extern crate prost as _prost;
327324

0 commit comments

Comments
 (0)