Debian Codegraph @

third_party/rust/zerocopy/CONTRIBUTING.md

raw
1 <!-- Copyright 2022 The Fuchsia Authors
2
3 Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4 <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5 license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6 This file may not be copied, modified, or distributed except according to
7 those terms. -->
8
9 # How to Contribute
10
11 We'd love to accept your patches and contributions to zerocopy. There are just a
12 few small guidelines you need to follow.
13
14 Once you've read the rest of this doc, check out our [good-first-issue
15 label][good-first-issue] for some good issues you can use to get your toes wet!
16
17 ## Contributor License Agreement
18
19 Contributions to this project must be accompanied by a Contributor License
20 Agreement. You (or your employer) retain the copyright to your contribution;
21 this simply gives us permission to use and redistribute your contributions as
22 part of the project. Head over to <https://cla.developers.google.com/> to see
23 your current agreements on file or to sign a new one.
24
25 You generally only need to submit a CLA once, so if you've already submitted one
26 (even if it was for a different project), you probably don't need to do it
27 again.
28
29 ## Code Reviews
30
31 All submissions, including submissions by project members, require review. We
32 use GitHub pull requests for this purpose. Consult [GitHub
33 Help][about_pull_requests] for more information on using pull requests.
34
35 ## Code Guidelines
36
37 ### Philosophy
38
39 This section is inspired by [Flutter's style guide][flutter_philosophy], which
40 contains many general principles that you should apply to all your programming
41 work. Read it. The below calls out specific aspects that we feel are
42 particularly important.
43
44 #### Dogfood Your Features
45
46 In non-library code, it's often advised to only implement features you need.
47 After all, it's hard to correctly design code without a concrete use case to
48 guide its design. Since zerocopy is a library, this advice is not as applicable;
49 we want our API surface to be featureful and complete even if not every feature
50 or method has a known use case. However, the observation that unused code is
51 hard to design still holds.
52
53 Thus, when designing external-facing features, try to make use of them somehow.
54 This could be by using them to implement other features, or it could be by
55 writing prototype code which won't actually be checked in anywhere. If you're
56 feeling ambitious, you could even add (and check in) a [Cargo
57 example][cargo_example] that exercises the new feature.
58
59 #### Go Down the Rabbit Hole
60
61 You will occasionally encounter behavior that surprises you or seems wrong. It
62 probably is! Invest the time to find the root cause - you will either learn
63 something, or fix something, and both are worth your time. Do not work around
64 behavior you don't understand.
65
66 ### Avoid Duplication
67
68 Avoid duplicating code whenever possible. In cases where existing code is not
69 exposed in a manner suitable to your needs, prefer to extract the necessary
70 parts into a common dependency.
71
72 ### Comments
73
74 When writing comments, take a moment to consider the future reader of your
75 comment. Ensure that your comments are complete sentences with proper grammar
76 and punctuation. Note that adding more comments or more verbose comments is not
77 always better; for example, avoid comments that repeat the code they're anchored
78 on.
79
80 Documentation comments should be self-contained; in other words, do not assume
81 that the reader is aware of documentation in adjacent files or on adjacent
82 structures. Avoid documentation comments on types which describe _instances_ of
83 the type; for example, `AddressSet is a set of client addresses.` is a comment
84 that describes a field of type `AddressSet`, but the type may be used to hold
85 any kind of `Address`, not just a client's.
86
87 Phrase your comments to avoid references that might become stale; for example:
88 do not mention a variable or type by name when possible (certain doc comments
89 are necessary exceptions). Also avoid references to past or future versions of
90 or past or future work surrounding the item being documented; explain things
91 from first principles rather than making external references (including past
92 revisions).
93
94 When writing TODOs:
95
96 1. Include an issue reference using the format `TODO(#123):`
97 1. Phrase the text as an action that is to be taken; it should be possible for
98 another contributor to pick up the TODO without consulting any external
99 sources, including the referenced issue.
100
101 ### Tests
102
103 Much of the code in zerocopy has the property that, if it is buggy, those bugs
104 may not cause user code to fail. This makes it extra important to write thorough
105 tests, but it also makes it harder to write those tests correctly. Here are some
106 guidelines on how to test code in zerocopy:
107 1. All code added to zerocopy must include tests that exercise it completely.
108 1. Tests must be deterministic. Threaded or time-dependent code, random number
109 generators (RNGs), and communication with external processes are common
110 sources of nondeterminism. See [Write reproducible, deterministic
111 tests][determinism] for tips.
112 1. Avoid [change detector tests][change_detector_tests]; tests that are
113 unnecessarily sensitive to changes, especially ones external to the code
114 under test, can hamper feature development and refactoring.
115 1. Since we run tests in [Miri][miri], make sure that tests exist which exercise
116 any potential [undefined behavior][undefined_behavior] so that Miri can catch
117 it.
118 1. If there's some user code that should be impossible to compile, add a
119 [trybuild test][trybuild] to ensure that it's properly rejected.
120
121 ### Source Control Best Practices
122
123 Commits should be arranged for ease of reading; that is, incidental changes
124 such as code movement or formatting changes should be committed separately from
125 actual code changes.
126
127 Commits should always be focused. For example, a commit could add a feature,
128 fix a bug, or refactor code, but not a mixture.
129
130 Commits should be thoughtfully sized; avoid overly large or complex commits
131 which can be logically separated, but also avoid overly separated commits that
132 require code reviews to load multiple commits into their mental working memory
133 in order to properly understand how the various pieces fit together.
134
135 #### Commit Messages
136
137 Commit messages should be _concise_ but self-contained (avoid relying on issue
138 references as explanations for changes) and written such that they are helpful
139 to people reading in the future (include rationale and any necessary context).
140
141 Avoid superfluous details or narrative.
142
143 Commit messages should consist of a brief subject line and a separate
144 explanatory paragraph in accordance with the following:
145
146 1. [Separate subject from body with a blank line](https://chris.beams.io/posts/git-commit/#separate)
147 1. [Limit the subject line to 50 characters](https://chris.beams.io/posts/git-commit/#limit-50)
148 1. [Capitalize the subject line](https://chris.beams.io/posts/git-commit/#capitalize)
149 1. [Do not end the subject line with a period](https://chris.beams.io/posts/git-commit/#end)
150 1. [Use the imperative mood in the subject line](https://chris.beams.io/posts/git-commit/#imperative)
151 1. [Wrap the body at 72 characters](https://chris.beams.io/posts/git-commit/#wrap-72)
152 1. [Use the body to explain what and why vs. how](https://chris.beams.io/posts/git-commit/#why-not-how)
153
154 If the code affects a particular subsystem, prefix the subject line with the
155 name of that subsystem in square brackets, omitting any "zerocopy" prefix
156 (that's implicit). For example, for a commit adding a feature to the
157 zerocopy-derive crate:
158
159 ```text
160 [derive] Support AsBytes on types with parameters
161 ```
162
163 The body may be omitted if the subject is self-explanatory; e.g. when fixing a
164 typo. The git book contains a [Commit Guidelines][commit_guidelines] section
165 with much of the same advice, and the list above is part of a [blog
166 post][beams_git_commit] by [Chris Beams][chris_beams].
167
168 Commit messages should make use of issue integration. Including an issue
169 reference like `#123` will cause the GitHub UI to link the text of that
170 reference to the referenced issue, and will also make it so that the referenced
171 issue back-links to the commit. Use "Closes", "Fixes", or "Resolves" on its own
172 line to automatically close an issue when your commit is merged:
173
174 ```text
175 Closes #123
176 Fixes #123
177 Resolves #123
178 ```
179
180 When using issue integration, don't omit necessary context that may also be
181 included in the relevant issue (see "Commit messages should be _concise_ but
182 self-contained" above). Git history is more likely to be retained indefinitely
183 than issue history (for example, if this repository is migrated away from GitHub
184 at some point in the future).
185
186 Commit messages should never contain references to any of:
187
188 1. Relative moments in time
189 1. Non-public URLs
190 1. Individuals
191 1. Hosted code reviews (such as on https://github.com/google/zerocopy/pulls)
192 + Refer to commits in this repository by their SHA-1 hash
193 + Refer to commits in other repositories by public web address (such as
194 https://github.com/google/zerocopy/commit/789b3deb)
195 1. Other entities which may not make sense to arbitrary future readers
196
197 ## Community Guidelines
198
199 This project follows [Google's Open Source Community
200 Guidelines][google_open_source_guidelines].
201
202 [about_pull_requests]: https://help.github.com/articles/about-pull-requests/
203 [beams_git_commit]: https://chris.beams.io/posts/git-commit/
204 [cargo_example]: http://xion.io/post/code/rust-examples.html
205 [change_detector_tests]: https://testing.googleblog.com/2015/01/testing-on-toilet-change-detector-tests.html
206 [chris_beams]: https://chris.beams.io/
207 [commit_guidelines]: https://www.git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines
208 [determinism]: https://fuchsia.dev/fuchsia-src/contribute/testing/best-practices#write_reproducible_deterministic_tests
209 [flutter_philosophy]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#philosophy
210 [good-first-issue]: https://github.com/google/zerocopy/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22
211 [google_open_source_guidelines]: https://opensource.google/conduct/
212 [magic_number]: https://en.wikipedia.org/wiki/Magic_number_(programming)
213 [miri]: https://github.com/rust-lang/miri
214 [trybuild]: https://crates.io/crates/trybuild
215 [undefined_behavior]: https://raphlinus.github.io/programming/rust/2018/08/17/undefined-behavior.html