diff --git a/prost-derive/src/field/map.rs b/prost-derive/src/field/map.rs index fb5e88c20..554152531 100644 --- a/prost-derive/src/field/map.rs +++ b/prost-derive/src/field/map.rs @@ -42,6 +42,7 @@ fn fake_scalar(ty: scalar::Ty) -> scalar::Field { ty, kind, tag: 0, // Not used here + deprecated: false, } } diff --git a/prost-derive/src/field/mod.rs b/prost-derive/src/field/mod.rs index d3922b1b4..e9c1f3d30 100644 --- a/prost-derive/src/field/mod.rs +++ b/prost-derive/src/field/mod.rs @@ -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, inferred_tag: Option) -> Result, 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) diff --git a/prost-derive/src/field/scalar.rs b/prost-derive/src/field/scalar.rs index 84505ebd2..c55bb0910 100644 --- a/prost-derive/src/field/scalar.rs +++ b/prost-derive/src/field/scalar.rs @@ -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) -> Result, Error> { + pub fn new( + attrs: &[Meta], + inferred_tag: Option, + deprecated: bool, + ) -> Result, Error> { let mut ty = None; let mut label = None; let mut packed = None; @@ -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, 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); @@ -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."); @@ -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; } @@ -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); @@ -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); } @@ -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>, fn(i32) -> ::core::option::Option<#ty>, @@ -343,6 +363,7 @@ impl Field { }) } #[doc=#push_doc] + #deprecated pub fn #push(&mut self, value: #ty) { self.#ident.push(value as i32); } @@ -364,6 +385,7 @@ impl Field { Some(quote! { #[doc=#get_doc] + #deprecated pub fn #get(&self) -> #ty { match self.#ident { #match_some