Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions prost-derive/src/field/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn fake_scalar(ty: scalar::Ty) -> scalar::Field {
ty,
kind,
tag: 0, // Not used here
deprecated: false,
}
}

Expand Down
3 changes: 2 additions & 1 deletion prost-derive/src/field/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ impl Field {
/// If the meta items are invalid, an error will be returned.
/// If the field should be ignored, `None` is returned.
pub fn new(attrs: Vec<Attribute>, inferred_tag: Option<u32>) -> Result<Option<Field>, Error> {
let deprecated = attrs.iter().any(|v| v.path().is_ident("deprecated"));
let attrs = prost_attrs(attrs)?;

// TODO: check for ignore attribute.

let field = if let Some(field) = scalar::Field::new(&attrs, inferred_tag)? {
let field = if let Some(field) = scalar::Field::new(&attrs, inferred_tag, deprecated)? {
Field::Scalar(field)
} else if let Some(field) = message::Field::new(&attrs, inferred_tag)? {
Field::Message(field)
Expand Down
28 changes: 25 additions & 3 deletions prost-derive/src/field/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ pub struct Field {
pub ty: Ty,
pub kind: Kind,
pub tag: u32,
pub deprecated: bool,
}

impl Field {
pub fn new(attrs: &[Meta], inferred_tag: Option<u32>) -> Result<Option<Field>, Error> {
pub fn new(
attrs: &[Meta],
inferred_tag: Option<u32>,
deprecated: bool,
) -> Result<Option<Field>, Error> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can itself determine the deprecated state from attrs. It makes sense to do that in the for attr in attrs loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case all non-prost attributes are filtered out by prost_attrs() before they are passed to this method. Which is why I check for the deprecated attribute before. I suppose we could adjust prost_attrs() to pass it through, though I'm not sure what other affects that might have.

What are your thoughts?

let mut ty = None;
let mut label = None;
let mut packed = None;
Expand Down Expand Up @@ -86,11 +91,16 @@ impl Field {
(Some(Label::Repeated), _, false) => Kind::Repeated,
};

Ok(Some(Field { ty, kind, tag }))
Ok(Some(Field {
ty,
kind,
tag,
deprecated,
}))
}

pub fn new_oneof(attrs: &[Meta]) -> Result<Option<Field>, Error> {
if let Some(mut field) = Field::new(attrs, None)? {
if let Some(mut field) = Field::new(attrs, None, false)? {
match field.kind {
Kind::Plain(default) => {
field.kind = Kind::Required(default);
Expand Down Expand Up @@ -284,6 +294,11 @@ impl Field {
Err(_) => quote!(#ident),
};

let deprecated = if self.deprecated {
Some(quote!(#[deprecated]))
} else {
None
};
if let Ty::Enumeration(ref ty) = self.ty {
let set = Ident::new(&format!("set_{ident_str}"), Span::call_site());
let set_doc = format!("Sets `{ident_str}` to the provided enum value.");
Expand All @@ -295,11 +310,13 @@ impl Field {
);
quote! {
#[doc=#get_doc]
#deprecated
pub fn #get(&self) -> #ty {
::core::convert::TryFrom::try_from(self.#ident).unwrap_or(#default)
}

#[doc=#set_doc]
#deprecated
pub fn #set(&mut self, value: #ty) {
self.#ident = value as i32;
}
Expand All @@ -312,6 +329,7 @@ impl Field {
);
quote! {
#[doc=#get_doc]
#deprecated
pub fn #get(&self) -> #ty {
self.#ident.and_then(|x| {
let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
Expand All @@ -320,6 +338,7 @@ impl Field {
}

#[doc=#set_doc]
#deprecated
pub fn #set(&mut self, value: #ty) {
self.#ident = ::core::option::Option::Some(value as i32);
}
Expand All @@ -333,6 +352,7 @@ impl Field {
let push_doc = format!("Appends the provided enum value to `{ident_str}`.");
quote! {
#[doc=#iter_doc]
#deprecated
pub fn #get(&self) -> ::core::iter::FilterMap<
::core::iter::Cloned<::core::slice::Iter<i32>>,
fn(i32) -> ::core::option::Option<#ty>,
Expand All @@ -343,6 +363,7 @@ impl Field {
})
}
#[doc=#push_doc]
#deprecated
pub fn #push(&mut self, value: #ty) {
self.#ident.push(value as i32);
}
Expand All @@ -364,6 +385,7 @@ impl Field {

Some(quote! {
#[doc=#get_doc]
#deprecated
pub fn #get(&self) -> #ty {
match self.#ident {
#match_some
Expand Down
Loading