about summary refs log tree commit diff
path: root/docs/dev
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2021-07-06 00:00:39 +0300
committerAleksey Kladov <aleksey.kladov@gmail.com>2021-07-06 00:00:39 +0300
commit0db4f3f6a4bce25f62d25f382ee47db47f5a33f9 (patch)
tree2087a289c2c7b4e5beb98160ed6508d35a485432 /docs/dev
parentb8a6ea5ab58f3b4d9c18f5d15d5115bc44a1d086 (diff)
downloadrust-0db4f3f6a4bce25f62d25f382ee47db47f5a33f9.tar.gz
rust-0db4f3f6a4bce25f62d25f382ee47db47f5a33f9.zip
internal: ensure consistent passing for config params
We pass "context" parametes first, so configs should be on the left.
"Bigger" context wins, so configs goes after db.
Diffstat (limited to 'docs/dev')
-rw-r--r--docs/dev/style.md105
1 files changed, 68 insertions, 37 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md
index f56f36e71c9..f83b99ca6b5 100644
--- a/docs/dev/style.md
+++ b/docs/dev/style.md
@@ -489,42 +489,6 @@ fn foo(bar: Option<Bar>) { ... }
 Splitting the two different control flows into two functions simplifies each path, and remove cross-dependencies between the two paths.
 If there's common code between `foo` and `foo_with_bar`, extract *that* into a common helper.
 
-## Avoid Monomorphization
-
-Avoid making a lot of code type parametric, *especially* on the boundaries between crates.
-
-```rust
-// GOOD
-fn frobnicate(f: impl FnMut()) {
-    frobnicate_impl(&mut f)
-}
-fn frobnicate_impl(f: &mut dyn FnMut()) {
-    // lots of code
-}
-
-// BAD
-fn frobnicate(f: impl FnMut()) {
-    // lots of code
-}
-```
-
-Avoid `AsRef` polymorphism, it pays back only for widely used libraries:
-
-```rust
-// GOOD
-fn frobnicate(f: &Path) {
-}
-
-// BAD
-fn frobnicate(f: impl AsRef<Path>) {
-}
-```
-
-**Rationale:** Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*.
-This allows for exceptionally good performance, but leads to increased compile times.
-Runtime performance obeys 80%/20% rule -- only a small fraction of code is hot.
-Compile time **does not** obey this rule -- all code has to be compiled.
-
 ## Appropriate String Types
 
 When interfacing with OS APIs, use `OsString`, even if the original source of data is utf-8 encoded.
@@ -617,6 +581,42 @@ pub fn reachable_nodes(node: Node) -> FxHashSet<Node> {
 
 **Rationale:** re-use allocations, accumulator style is more concise for complex cases.
 
+## Avoid Monomorphization
+
+Avoid making a lot of code type parametric, *especially* on the boundaries between crates.
+
+```rust
+// GOOD
+fn frobnicate(f: impl FnMut()) {
+    frobnicate_impl(&mut f)
+}
+fn frobnicate_impl(f: &mut dyn FnMut()) {
+    // lots of code
+}
+
+// BAD
+fn frobnicate(f: impl FnMut()) {
+    // lots of code
+}
+```
+
+Avoid `AsRef` polymorphism, it pays back only for widely used libraries:
+
+```rust
+// GOOD
+fn frobnicate(f: &Path) {
+}
+
+// BAD
+fn frobnicate(f: impl AsRef<Path>) {
+}
+```
+
+**Rationale:** Rust uses monomorphization to compile generic code, meaning that for each instantiation of a generic functions with concrete types, the function is compiled afresh, *per crate*.
+This allows for exceptionally good performance, but leads to increased compile times.
+Runtime performance obeys 80%/20% rule -- only a small fraction of code is hot.
+Compile time **does not** obey this rule -- all code has to be compiled.
+
 # Style
 
 ## Order of Imports
@@ -780,6 +780,38 @@ impl Parent {
 **Rationale:** easier to get the sense of the API by visually scanning the file.
 If function bodies are folded in the editor, the source code should read as documentation for the public API.
 
+## Context Parameters
+
+Some parameters are threaded unchanged through many function calls.
+They determine the "context" of the operation.
+Pass such parameters first, not last.
+If there are several context parameters, consider packing them into a `struct Ctx` and passing it as `&self`.
+
+```rust
+// GOOD
+fn dfs(graph: &Graph, v: Vertex) -> usize {
+    let mut visited = FxHashSet::default();
+    return go(graph, &mut visited, v);
+
+    fn go(graph: &Graph, visited: &mut FxHashSet<Vertex>, v: usize) -> usize {
+        ...
+    }
+}
+
+// BAD
+fn dfs(v: Vertex, graph: &Graph) -> usize {
+    fn go(v: usize, graph: &Graph, visited: &mut FxHashSet<Vertex>) -> usize {
+        ...
+    }
+
+    let mut visited = FxHashSet::default();
+    go(v, graph, &mut visited)
+}
+```
+
+**Rationale:** consistency.
+Context-first works better when non-context parameter is a lambda.
+
 ## Variable Naming
 
 Use boring and long names for local variables ([yay code completion](https://github.com/rust-analyzer/rust-analyzer/pull/4162#discussion_r417130973)).
@@ -934,7 +966,6 @@ fn dfs(graph: &Graph, v: Vertex) -> usize {
     let mut visited = FxHashSet::default();
     go(graph, &mut visited, v)
 }
-
 ```
 
 **Rationale:** consistency, improved top-down readability.