Skip to content

Commit 1b7c6dc

Browse files
UnbreakableMJAntigravity (Gemini 3.5 Flash)
andcommitted
feat(skills): improve spacecraft-rust-guidelines with Apollo Best Practices
Integrate cargo flamegraph profiling and smart pointer/thread safety references: - Add cargo flamegraph installation and execution guide to SKILL.md under Tooling section. - Add thread-safety guidelines and smart pointer comparison table to references/idioms.md. - Add standard library lazy initialization primitives (OnceCell, OnceLock, LazyCell, LazyLock). - Update references/ATTRIBUTION.md and CREDITS.md to reflect new adapted materials. Co-Authored-By: Antigravity (Gemini 3.5 Flash) <noreply@google.com>
1 parent 6cdf7eb commit 1b7c6dc

6 files changed

Lines changed: 48 additions & 3 deletions

File tree

spacecraft-rust-guidelines.skill

1.15 KB
Binary file not shown.

spacecraft-rust-guidelines.zip

1.15 KB
Binary file not shown.

spacecraft-rust-guidelines/CREDITS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ in accordance with [The Steelbore Standard §15.3](../spacecraft-standard/SKILL.
1313
| Author(s) | Apollo Graph, Inc. |
1414
| License | MIT License |
1515
| Source URL | https://github.com/apollographql/skills |
16-
| Scope | Idiom/readability rules distilled into `references/idioms.md` — borrowing-vs-cloning, `Copy` sizing, idiomatic `Option`/`Result` flow, iterators-vs-`for`, clippy lint names + `expect`-over-`allow`, testing conventions (one assertion per test, `insta` snapshots), static-vs-dynamic dispatch, the type-state pattern, comments-vs-docs, and import ordering. Apollo's error-handling and performance-mindset chapters were **not** adapted (this skill's SKILL.md and `microsoft-rust-guidelines` cover those). |
16+
| Scope | Idiom/readability rules distilled into `references/idioms.md` — borrowing-vs-cloning, `Copy` sizing, idiomatic `Option`/`Result` flow, iterators-vs-`for`, clippy lint names + `expect`-over-`allow`, testing conventions (one assertion per test, `insta` snapshots), static-vs-dynamic dispatch, the type-state pattern, comments-vs-docs, import ordering, flamegraph profiling (adapted from Chapter 3), and smart pointers / thread-safety (adapted from Chapter 9). Apollo's error-handling chapter was **not** adapted (this skill's SKILL.md and `microsoft-rust-guidelines` cover those). |
1717

1818
The verbatim upstream MIT notice is preserved in
1919
[`references/ATTRIBUTION.md`](references/ATTRIBUTION.md) (Standard §4.2).

spacecraft-rust-guidelines/SKILL.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ characteristics, and a clear understanding of the trade‑offs involved.
8686
- `cargo audit` and `cargo deny` to keep dependencies secure and minimal.
8787
- CI must run fmt, clippy, all tests, doc tests, and at least a sample benchmark.
8888

89+
### Performance Profiling with Flamegraph
90+
When profiling to identify performance bottlenecks (Standard §3.2):
91+
- **Installation:** `cargo install flamegraph`
92+
- **Execution (always use `--release`):**
93+
- Run binary: `cargo flamegraph`
94+
- Run specific binary: `cargo flamegraph --bin=<name>`
95+
- Run unit tests: `cargo flamegraph --unit-test -- <test_name>`
96+
- Run benchmarks (e.g., criterion): `cargo flamegraph --bench <name> -- --bench`
97+
- **Reading a Flamegraph:**
98+
- **Y-axis:** Stack depth. The entry point (e.g., `main`) is at the bottom; called functions stack on top.
99+
- **X-axis (Width):** Total CPU time/percentage spent on that function or its callees. Wider box = more CPU time.
100+
- **Color:** Randomized (has no performance/semantic meaning). Focus on wide boxes and thick stacks.
101+
89102
## Error Handling & Resilience
90103
- Libraries: precise error types via `thiserror`, avoid `unwrap()`.
91104
- Applications: `anyhow`/`eyre` for propagation, with `.context()`.

spacecraft-rust-guidelines/references/ATTRIBUTION.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ attribution), the upstream MIT notice is preserved verbatim below.
1818
sizing, idiomatic `Option`/`Result` flow, iterators-vs-`for`, clippy lint names and
1919
`expect`-over-`allow` discipline, testing conventions (one assertion per test,
2020
descriptive names, `insta` snapshots), static-vs-dynamic dispatch, the type-state
21-
pattern, comments-vs-docs, and import ordering. Apollo's error-handling and
22-
performance-mindset chapters were intentionally **not** adapted (covered by this
21+
pattern, comments-vs-docs, import ordering, flamegraph profiling details (adapted
22+
from Chapter 3), and smart pointers / thread-safety guidelines (adapted from Chapter 9).
23+
Apollo's error-handling chapter was intentionally **not** adapted (covered by this
2324
skill's SKILL.md and by `microsoft-rust-guidelines`).
2425

2526
> Note: the *skill* we adapted from (the `apollographql/skills` repository) is MIT. A

spacecraft-rust-guidelines/references/idioms.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,34 @@ group_imports = "StdExternalCrate"
172172
```
173173

174174
(`group_imports` currently needs `cargo +nightly fmt`.)
175+
176+
## 10. Smart pointers & thread safety
177+
178+
Rust tracks thread safety via compiler-enforced auto-traits:
179+
- `Send` indicates that ownership of a type can be transferred across thread boundaries.
180+
- `Sync` indicates that references to a type (`&T`) can be shared safely across threads.
181+
182+
### Smart Pointer Comparison
183+
184+
| Pointer Type | Description | Send / Sync | Use Case |
185+
| :--- | :--- | :--- | :--- |
186+
| `&T` | Shared reference | `Send + Sync` (if `T: Sync`) | Immutable shared access. |
187+
| `&mut T` | Exclusive mutable reference | `Send + Sync` (if `T: Send + Sync`) | Exclusive temporary mutation. |
188+
| `Box<T>` | Unique heap-allocated pointer | `Send + Sync` (if `T: Send + Sync`) | Owned indirection / recursive types. |
189+
| `Rc<T>` | Non-atomic reference-counted | **Neither** `Send` nor `Sync` | Shared ownership within a single thread. |
190+
| `Arc<T>` | Atomic reference-counted | `Send + Sync` (if `T: Send + Sync`) | Shared ownership across multiple threads. |
191+
| `Cell<T>` | Interior mutability for `Copy` types | `Send` (if `T: Send`), **not** `Sync` | Zero-overhead single-thread mutability. |
192+
| `RefCell<T>` | Interior mutability (dynamic borrow) | `Send` (if `T: Send`), **not** `Sync` | Single-thread runtime-checked mutation. |
193+
| `Mutex<T>` | Thread-safe mutual exclusion lock | `Send + Sync` (if `T: Send`) | Shared mutable access across threads. |
194+
| `RwLock<T>` | Thread-safe readers-writer lock | `Send + Sync` (if `T: Send + Sync`) | Read-heavy shared mutable thread access. |
195+
196+
### Lazy Initialization & One-Time Cells
197+
198+
Avoid complex custom setups with `Option` or unsafe blocks for lazy values. Use standard library cells:
199+
200+
- **Single-Threaded (`std::cell`):**
201+
- `OnceCell<T>`: Write-once container for single-threaded deferred initialization.
202+
- `LazyCell<T>` (stabilized in Rust 1.80): Lazy value initialized via a closure on first deref.
203+
- **Thread-Safe / Shared (`std::sync`):**
204+
- `OnceLock<T>` (stabilized in Rust 1.70): Thread-safe `OnceCell` for global/shared resources.
205+
- `LazyLock<T>` (stabilized in Rust 1.80): Thread-safe `LazyCell` for lazy thread-safe globals.

0 commit comments

Comments
 (0)