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

It would be nice to derive Default for message structs #136

Closed
timthelion opened this issue Nov 18, 2018 · 2 comments
Closed

It would be nice to derive Default for message structs #136

timthelion opened this issue Nov 18, 2018 · 2 comments

Comments

@timthelion
Copy link
Contributor

I see that this has already been addressed in https://github.com/danburkert/prost/pull/117 . I'm ending up with a lot of boring code like:

pub fn blank_cell_runtime() -> gradesta::CellRuntime {
 gradesta::CellRuntime {
  cell: None,
  update_count: None,
  click_count: None,
  deleted: None,
  creation_id: None,
  cell_runtime_modes: HashMap::new(),
  cell_modes: HashMap::new(),
  link_modes: HashMap::new(),
  for_link_modes: HashMap::new(),
  back_link_modes: HashMap::new(),
 }
}

pub const blank_actor_metadata: gradesta::ActorMetadata = gradesta::ActorMetadata {
 name: None,
 source_url: None,
 privacy_policy: None,
};

So that I can send messages with only a few fields set.

@danburkert
Copy link
Collaborator

Not sure I understand the question fully. All generated message types do implement Default, so the implementation of that method and const could simply be Default::default(). I don’t see a connection between your question and unknown fields, could you elaborate?

@timthelion
Copy link
Contributor Author

I did not know that. I read the discussion here: https://github.com/danburkert/prost/issues/2#issuecomment-402772743 in which @per-gron writes "This would work if all generated structs would #[derive(Default)] (or equivalent), which seems like a sensible thing to do in general because Protobufs in general assume that no code breaks from just adding a field." And I assumed that default was NOT derived. Further, I saw code in that PR https://github.com/danburkert/prost/pull/117/files#diff-6d0f1e2a66e5c7be23fcd64650dc1044R3 which appeared to be testing the addition of default handling, though I admit that now I am not sure where the new default handling code is in that PR which is to be tested.

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

No branches or pull requests

2 participants