Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more attributes #493

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open

Add more attributes #493

wants to merge 18 commits into from

Conversation

r3v2d0g
Copy link

@r3v2d0g r3v2d0g commented Jun 25, 2021

This PR adds four new struct attributes, and seven new field attributes. It also adds support for using #[prost(...)] multiple times on a field.

Struct attributes

  • debug = bool (default: true): if false, prost won't implement Debug for the struct
  • default = bool (default: true): if false, prost won't implement Default for the struct
  • merge = bool (default: true): if false, prost will implement Message::merge_field and Message::merge to return an errror
  • proto = "proto2" | "proto3" (default: "proto2"): changes the default behavior of message fields without required or optional specified to be optional when proto = "proto2" and required when proto = "proto3"

Field attributes

Scalar and message fields

T is the field's type. U is either the specified scalar or a type implementing Message.

  • as_msg = "fn_or_closure": fn (field: &T) -> &U
  • to_msg = "fn_or_closure": fn (field: &T) -> U
  • from_msg = "fn_or_closure": fn (msg: U) -> T
  • merge_msg = "fn_or_closure": fn (field: &mut T, msg: U)
  • clear = bool (default: true): if false, doesn't clear this field when Message::clear is called

repeated fields

T is either the field's type (if as_msgs or to_msgs is set) or the T in Vec<T> (if as_msgs and to_msgs are not set). U is either the specified scalar or a type implementing Message.

  • as_msg = "fn_or_closure": fn (elem: &T) -> &U
  • to_msg = "fn_or_closure": fn (elem: &T) -> U
  • as_msgs = "fn_or_closure": fn (field: &T) -> &Vec<U> or fn (field: &T) -> impl Iterator<Item = &U> + '_
  • to_msgs = "fn_or_closure": fn (field: &T) -> Vec<U> or fn (field: &T) -> impl Iterator<U> + '_
  • from_msg = "fn_or_closure": fn (msg: U) -> T
  • merge_msg = "fn_or_closure": fn (elem_or_field: &mut T, msg: U)
  • clear = bool (default: true): if false, doesn't clear this field when Message::clear is called

If the field is defined as packed or the scalar type is a number, then as_msgs and to_msgs should not return an Iterator.

If as_msgs or to_msgs is set, then as_msg and to_msg should not be set.

map = "key, value" fields

T is the value type of the map (the V in HashMap<K, V> and BTreeMap<K, V>). U is either the specified scalar (as value) or a type implementing Message.

  • as_msg = "fn_or_closure": fn (field: &T) -> &U
  • to_msg = "fn_or_closure": fn (field: &T) -> U
  • from_msg = "fn_or_closure": fn (msg: U) -> T
  • merge_msg = "fn_or_closure": fn (field: &mut T, msg: U)
  • clear = bool (default: true): if false, doesn't clear this field when Message::clear is called

@r3v2d0g r3v2d0g force-pushed the master branch 6 times, most recently from fdf2ce9 to bb3a1c1 Compare July 1, 2021 12:02
@LucioFranco
Copy link
Member

This seems interesting, since its quite large its not extremely high priority on my list but I will get to this soon. Thanks for the patience.

@LucioFranco
Copy link
Member

@r3v2d0g I am still interested in adding this to prost but would you be able to break these up into some smaller PRs. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants