agy-superpowers 5.2.1 → 5.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -150
- package/package.json +1 -1
- package/template/agent/patches/skills-patches.md +23 -0
- package/template/agent/rules/scratch-scripts.md +37 -0
- package/template/agent/rules/superpowers.md +6 -50
- package/template/agent/skills/brainstorming/SKILL.md +4 -3
- package/template/agent/skills/brainstorming/visual-companion.md +2 -3
- package/template/agent/skills/finishing-a-development-branch/SKILL.md +11 -16
- package/template/agent/skills/subagent-driven-development/SKILL.md +16 -0
- package/template/agent/skills/subagent-driven-development/implementer-prompt.md +4 -3
- package/template/agent/skills/using-git-worktrees/SKILL.md +3 -2
- package/template/agent/skills/using-superpowers/SKILL.md +8 -6
- package/template/agent/skills/using-superpowers/references/copilot-tools.md +52 -0
- package/template/agent/skills/writing-plans/SKILL.md +5 -3
- package/template/agent/skills/writing-skills/SKILL.md +1 -1
- package/template/agent/superpowers-version.json +2 -2
- package/template/agent/tmp/agent-config-backup.yml +9 -0
- package/template/agent/skills/ai-integrated-product/SKILL.md +0 -57
- package/template/agent/skills/analytics-setup/SKILL.md +0 -51
- package/template/agent/skills/api-design/SKILL.md +0 -193
- package/template/agent/skills/app-store-optimizer/SKILL.md +0 -127
- package/template/agent/skills/auth-and-identity/SKILL.md +0 -167
- package/template/agent/skills/backend-developer/SKILL.md +0 -148
- package/template/agent/skills/bootstrapper-finance/SKILL.md +0 -55
- package/template/agent/skills/chrome-extension-developer/SKILL.md +0 -53
- package/template/agent/skills/community-manager/SKILL.md +0 -115
- package/template/agent/skills/content-marketer/SKILL.md +0 -111
- package/template/agent/skills/conversion-optimizer/SKILL.md +0 -142
- package/template/agent/skills/cto-architect/SKILL.md +0 -133
- package/template/agent/skills/customer-success-manager/SKILL.md +0 -126
- package/template/agent/skills/data-analyst/SKILL.md +0 -147
- package/template/agent/skills/devops-engineer/SKILL.md +0 -117
- package/template/agent/skills/email-infrastructure/SKILL.md +0 -164
- package/template/agent/skills/game-design/SKILL.md +0 -194
- package/template/agent/skills/game-developer/SKILL.md +0 -175
- package/template/agent/skills/growth-hacker/SKILL.md +0 -122
- package/template/agent/skills/idea-validator/SKILL.md +0 -55
- package/template/agent/skills/indie-legal/SKILL.md +0 -53
- package/template/agent/skills/influencer-marketer/SKILL.md +0 -141
- package/template/agent/skills/landing-page-builder/SKILL.md +0 -59
- package/template/agent/skills/launch-strategist/SKILL.md +0 -62
- package/template/agent/skills/market-researcher/SKILL.md +0 -53
- package/template/agent/skills/micro-saas-builder/SKILL.md +0 -56
- package/template/agent/skills/monetization-strategist/SKILL.md +0 -119
- package/template/agent/skills/paid-acquisition-specialist/SKILL.md +0 -119
- package/template/agent/skills/pricing-psychologist/SKILL.md +0 -58
- package/template/agent/skills/real-time-features/SKILL.md +0 -194
- package/template/agent/skills/retention-specialist/SKILL.md +0 -123
- package/template/agent/skills/rust-developer/SKILL.md +0 -281
- package/template/agent/skills/rust-developer/references/rust-rules/_sections.md +0 -231
- package/template/agent/skills/rust-developer/references/rust-rules/anti-clone-excessive.md +0 -124
- package/template/agent/skills/rust-developer/references/rust-rules/anti-collect-intermediate.md +0 -131
- package/template/agent/skills/rust-developer/references/rust-rules/anti-empty-catch.md +0 -132
- package/template/agent/skills/rust-developer/references/rust-rules/anti-expect-lazy.md +0 -95
- package/template/agent/skills/rust-developer/references/rust-rules/anti-format-hot-path.md +0 -141
- package/template/agent/skills/rust-developer/references/rust-rules/anti-index-over-iter.md +0 -125
- package/template/agent/skills/rust-developer/references/rust-rules/anti-lock-across-await.md +0 -127
- package/template/agent/skills/rust-developer/references/rust-rules/anti-over-abstraction.md +0 -120
- package/template/agent/skills/rust-developer/references/rust-rules/anti-panic-expected.md +0 -131
- package/template/agent/skills/rust-developer/references/rust-rules/anti-premature-optimize.md +0 -156
- package/template/agent/skills/rust-developer/references/rust-rules/anti-string-for-str.md +0 -122
- package/template/agent/skills/rust-developer/references/rust-rules/anti-stringly-typed.md +0 -167
- package/template/agent/skills/rust-developer/references/rust-rules/anti-type-erasure.md +0 -134
- package/template/agent/skills/rust-developer/references/rust-rules/anti-unwrap-abuse.md +0 -143
- package/template/agent/skills/rust-developer/references/rust-rules/anti-vec-for-slice.md +0 -121
- package/template/agent/skills/rust-developer/references/rust-rules/api-builder-must-use.md +0 -143
- package/template/agent/skills/rust-developer/references/rust-rules/api-builder-pattern.md +0 -187
- package/template/agent/skills/rust-developer/references/rust-rules/api-common-traits.md +0 -165
- package/template/agent/skills/rust-developer/references/rust-rules/api-default-impl.md +0 -177
- package/template/agent/skills/rust-developer/references/rust-rules/api-extension-trait.md +0 -163
- package/template/agent/skills/rust-developer/references/rust-rules/api-from-not-into.md +0 -146
- package/template/agent/skills/rust-developer/references/rust-rules/api-impl-asref.md +0 -142
- package/template/agent/skills/rust-developer/references/rust-rules/api-impl-into.md +0 -160
- package/template/agent/skills/rust-developer/references/rust-rules/api-must-use.md +0 -125
- package/template/agent/skills/rust-developer/references/rust-rules/api-newtype-safety.md +0 -162
- package/template/agent/skills/rust-developer/references/rust-rules/api-non-exhaustive.md +0 -177
- package/template/agent/skills/rust-developer/references/rust-rules/api-parse-dont-validate.md +0 -184
- package/template/agent/skills/rust-developer/references/rust-rules/api-sealed-trait.md +0 -168
- package/template/agent/skills/rust-developer/references/rust-rules/api-serde-optional.md +0 -182
- package/template/agent/skills/rust-developer/references/rust-rules/api-typestate.md +0 -199
- package/template/agent/skills/rust-developer/references/rust-rules/async-bounded-channel.md +0 -175
- package/template/agent/skills/rust-developer/references/rust-rules/async-broadcast-pubsub.md +0 -185
- package/template/agent/skills/rust-developer/references/rust-rules/async-cancellation-token.md +0 -203
- package/template/agent/skills/rust-developer/references/rust-rules/async-clone-before-await.md +0 -171
- package/template/agent/skills/rust-developer/references/rust-rules/async-join-parallel.md +0 -158
- package/template/agent/skills/rust-developer/references/rust-rules/async-joinset-structured.md +0 -195
- package/template/agent/skills/rust-developer/references/rust-rules/async-mpsc-queue.md +0 -171
- package/template/agent/skills/rust-developer/references/rust-rules/async-no-lock-await.md +0 -156
- package/template/agent/skills/rust-developer/references/rust-rules/async-oneshot-response.md +0 -191
- package/template/agent/skills/rust-developer/references/rust-rules/async-select-racing.md +0 -198
- package/template/agent/skills/rust-developer/references/rust-rules/async-spawn-blocking.md +0 -154
- package/template/agent/skills/rust-developer/references/rust-rules/async-tokio-fs.md +0 -167
- package/template/agent/skills/rust-developer/references/rust-rules/async-tokio-runtime.md +0 -169
- package/template/agent/skills/rust-developer/references/rust-rules/async-try-join.md +0 -172
- package/template/agent/skills/rust-developer/references/rust-rules/async-watch-latest.md +0 -189
- package/template/agent/skills/rust-developer/references/rust-rules/doc-all-public.md +0 -113
- package/template/agent/skills/rust-developer/references/rust-rules/doc-cargo-metadata.md +0 -147
- package/template/agent/skills/rust-developer/references/rust-rules/doc-errors-section.md +0 -122
- package/template/agent/skills/rust-developer/references/rust-rules/doc-examples-section.md +0 -161
- package/template/agent/skills/rust-developer/references/rust-rules/doc-hidden-setup.md +0 -149
- package/template/agent/skills/rust-developer/references/rust-rules/doc-intra-links.md +0 -138
- package/template/agent/skills/rust-developer/references/rust-rules/doc-link-types.md +0 -169
- package/template/agent/skills/rust-developer/references/rust-rules/doc-module-inner.md +0 -116
- package/template/agent/skills/rust-developer/references/rust-rules/doc-panics-section.md +0 -128
- package/template/agent/skills/rust-developer/references/rust-rules/doc-question-mark.md +0 -136
- package/template/agent/skills/rust-developer/references/rust-rules/doc-safety-section.md +0 -131
- package/template/agent/skills/rust-developer/references/rust-rules/err-anyhow-app.md +0 -179
- package/template/agent/skills/rust-developer/references/rust-rules/err-context-chain.md +0 -144
- package/template/agent/skills/rust-developer/references/rust-rules/err-custom-type.md +0 -152
- package/template/agent/skills/rust-developer/references/rust-rules/err-doc-errors.md +0 -145
- package/template/agent/skills/rust-developer/references/rust-rules/err-expect-bugs-only.md +0 -133
- package/template/agent/skills/rust-developer/references/rust-rules/err-from-impl.md +0 -152
- package/template/agent/skills/rust-developer/references/rust-rules/err-lowercase-msg.md +0 -124
- package/template/agent/skills/rust-developer/references/rust-rules/err-no-unwrap-prod.md +0 -115
- package/template/agent/skills/rust-developer/references/rust-rules/err-question-mark.md +0 -151
- package/template/agent/skills/rust-developer/references/rust-rules/err-result-over-panic.md +0 -130
- package/template/agent/skills/rust-developer/references/rust-rules/err-source-chain.md +0 -155
- package/template/agent/skills/rust-developer/references/rust-rules/err-thiserror-lib.md +0 -171
- package/template/agent/skills/rust-developer/references/rust-rules/lint-cargo-metadata.md +0 -138
- package/template/agent/skills/rust-developer/references/rust-rules/lint-deny-correctness.md +0 -107
- package/template/agent/skills/rust-developer/references/rust-rules/lint-missing-docs.md +0 -154
- package/template/agent/skills/rust-developer/references/rust-rules/lint-pedantic-selective.md +0 -118
- package/template/agent/skills/rust-developer/references/rust-rules/lint-rustfmt-check.md +0 -157
- package/template/agent/skills/rust-developer/references/rust-rules/lint-unsafe-doc.md +0 -133
- package/template/agent/skills/rust-developer/references/rust-rules/lint-warn-complexity.md +0 -131
- package/template/agent/skills/rust-developer/references/rust-rules/lint-warn-perf.md +0 -136
- package/template/agent/skills/rust-developer/references/rust-rules/lint-warn-style.md +0 -135
- package/template/agent/skills/rust-developer/references/rust-rules/lint-warn-suspicious.md +0 -122
- package/template/agent/skills/rust-developer/references/rust-rules/lint-workspace-lints.md +0 -172
- package/template/agent/skills/rust-developer/references/rust-rules/mem-arena-allocator.md +0 -168
- package/template/agent/skills/rust-developer/references/rust-rules/mem-arrayvec.md +0 -142
- package/template/agent/skills/rust-developer/references/rust-rules/mem-assert-type-size.md +0 -168
- package/template/agent/skills/rust-developer/references/rust-rules/mem-avoid-format.md +0 -147
- package/template/agent/skills/rust-developer/references/rust-rules/mem-box-large-variant.md +0 -158
- package/template/agent/skills/rust-developer/references/rust-rules/mem-boxed-slice.md +0 -139
- package/template/agent/skills/rust-developer/references/rust-rules/mem-clone-from.md +0 -147
- package/template/agent/skills/rust-developer/references/rust-rules/mem-compact-string.md +0 -149
- package/template/agent/skills/rust-developer/references/rust-rules/mem-reuse-collections.md +0 -174
- package/template/agent/skills/rust-developer/references/rust-rules/mem-smaller-integers.md +0 -159
- package/template/agent/skills/rust-developer/references/rust-rules/mem-smallvec.md +0 -138
- package/template/agent/skills/rust-developer/references/rust-rules/mem-thinvec.md +0 -142
- package/template/agent/skills/rust-developer/references/rust-rules/mem-with-capacity.md +0 -156
- package/template/agent/skills/rust-developer/references/rust-rules/mem-write-over-format.md +0 -172
- package/template/agent/skills/rust-developer/references/rust-rules/mem-zero-copy.md +0 -164
- package/template/agent/skills/rust-developer/references/rust-rules/name-acronym-word.md +0 -99
- package/template/agent/skills/rust-developer/references/rust-rules/name-as-free.md +0 -104
- package/template/agent/skills/rust-developer/references/rust-rules/name-consts-screaming.md +0 -94
- package/template/agent/skills/rust-developer/references/rust-rules/name-crate-no-rs.md +0 -78
- package/template/agent/skills/rust-developer/references/rust-rules/name-funcs-snake.md +0 -76
- package/template/agent/skills/rust-developer/references/rust-rules/name-into-ownership.md +0 -123
- package/template/agent/skills/rust-developer/references/rust-rules/name-is-has-bool.md +0 -127
- package/template/agent/skills/rust-developer/references/rust-rules/name-iter-convention.md +0 -129
- package/template/agent/skills/rust-developer/references/rust-rules/name-iter-method.md +0 -131
- package/template/agent/skills/rust-developer/references/rust-rules/name-iter-type-match.md +0 -142
- package/template/agent/skills/rust-developer/references/rust-rules/name-lifetime-short.md +0 -86
- package/template/agent/skills/rust-developer/references/rust-rules/name-no-get-prefix.md +0 -154
- package/template/agent/skills/rust-developer/references/rust-rules/name-to-expensive.md +0 -118
- package/template/agent/skills/rust-developer/references/rust-rules/name-type-param-single.md +0 -92
- package/template/agent/skills/rust-developer/references/rust-rules/name-types-camel.md +0 -65
- package/template/agent/skills/rust-developer/references/rust-rules/name-variants-camel.md +0 -101
- package/template/agent/skills/rust-developer/references/rust-rules/opt-bounds-check.md +0 -161
- package/template/agent/skills/rust-developer/references/rust-rules/opt-cache-friendly.md +0 -187
- package/template/agent/skills/rust-developer/references/rust-rules/opt-codegen-units.md +0 -142
- package/template/agent/skills/rust-developer/references/rust-rules/opt-cold-unlikely.md +0 -152
- package/template/agent/skills/rust-developer/references/rust-rules/opt-inline-always-rare.md +0 -141
- package/template/agent/skills/rust-developer/references/rust-rules/opt-inline-never-cold.md +0 -181
- package/template/agent/skills/rust-developer/references/rust-rules/opt-inline-small.md +0 -160
- package/template/agent/skills/rust-developer/references/rust-rules/opt-likely-hint.md +0 -171
- package/template/agent/skills/rust-developer/references/rust-rules/opt-lto-release.md +0 -130
- package/template/agent/skills/rust-developer/references/rust-rules/opt-pgo-profile.md +0 -167
- package/template/agent/skills/rust-developer/references/rust-rules/opt-simd-portable.md +0 -144
- package/template/agent/skills/rust-developer/references/rust-rules/opt-target-cpu.md +0 -154
- package/template/agent/skills/rust-developer/references/rust-rules/own-arc-shared.md +0 -141
- package/template/agent/skills/rust-developer/references/rust-rules/own-borrow-over-clone.md +0 -95
- package/template/agent/skills/rust-developer/references/rust-rules/own-clone-explicit.md +0 -135
- package/template/agent/skills/rust-developer/references/rust-rules/own-copy-small.md +0 -124
- package/template/agent/skills/rust-developer/references/rust-rules/own-cow-conditional.md +0 -135
- package/template/agent/skills/rust-developer/references/rust-rules/own-lifetime-elision.md +0 -134
- package/template/agent/skills/rust-developer/references/rust-rules/own-move-large.md +0 -134
- package/template/agent/skills/rust-developer/references/rust-rules/own-mutex-interior.md +0 -105
- package/template/agent/skills/rust-developer/references/rust-rules/own-rc-single-thread.md +0 -65
- package/template/agent/skills/rust-developer/references/rust-rules/own-refcell-interior.md +0 -97
- package/template/agent/skills/rust-developer/references/rust-rules/own-rwlock-readers.md +0 -122
- package/template/agent/skills/rust-developer/references/rust-rules/own-slice-over-vec.md +0 -119
- package/template/agent/skills/rust-developer/references/rust-rules/perf-black-box-bench.md +0 -153
- package/template/agent/skills/rust-developer/references/rust-rules/perf-chain-avoid.md +0 -136
- package/template/agent/skills/rust-developer/references/rust-rules/perf-collect-into.md +0 -133
- package/template/agent/skills/rust-developer/references/rust-rules/perf-collect-once.md +0 -120
- package/template/agent/skills/rust-developer/references/rust-rules/perf-drain-reuse.md +0 -137
- package/template/agent/skills/rust-developer/references/rust-rules/perf-entry-api.md +0 -134
- package/template/agent/skills/rust-developer/references/rust-rules/perf-extend-batch.md +0 -150
- package/template/agent/skills/rust-developer/references/rust-rules/perf-iter-lazy.md +0 -123
- package/template/agent/skills/rust-developer/references/rust-rules/perf-iter-over-index.md +0 -113
- package/template/agent/skills/rust-developer/references/rust-rules/perf-profile-first.md +0 -175
- package/template/agent/skills/rust-developer/references/rust-rules/perf-release-profile.md +0 -149
- package/template/agent/skills/rust-developer/references/rust-rules/proj-bin-dir.md +0 -142
- package/template/agent/skills/rust-developer/references/rust-rules/proj-flat-small.md +0 -133
- package/template/agent/skills/rust-developer/references/rust-rules/proj-lib-main-split.md +0 -148
- package/template/agent/skills/rust-developer/references/rust-rules/proj-mod-by-feature.md +0 -130
- package/template/agent/skills/rust-developer/references/rust-rules/proj-mod-rs-dir.md +0 -120
- package/template/agent/skills/rust-developer/references/rust-rules/proj-prelude-module.md +0 -155
- package/template/agent/skills/rust-developer/references/rust-rules/proj-pub-crate-internal.md +0 -139
- package/template/agent/skills/rust-developer/references/rust-rules/proj-pub-super-parent.md +0 -135
- package/template/agent/skills/rust-developer/references/rust-rules/proj-pub-use-reexport.md +0 -162
- package/template/agent/skills/rust-developer/references/rust-rules/proj-workspace-deps.md +0 -186
- package/template/agent/skills/rust-developer/references/rust-rules/proj-workspace-large.md +0 -162
- package/template/agent/skills/rust-developer/references/rust-rules/test-arrange-act-assert.md +0 -160
- package/template/agent/skills/rust-developer/references/rust-rules/test-cfg-test-module.md +0 -151
- package/template/agent/skills/rust-developer/references/rust-rules/test-criterion-bench.md +0 -171
- package/template/agent/skills/rust-developer/references/rust-rules/test-descriptive-names.md +0 -142
- package/template/agent/skills/rust-developer/references/rust-rules/test-doctest-examples.md +0 -168
- package/template/agent/skills/rust-developer/references/rust-rules/test-fixture-raii.md +0 -151
- package/template/agent/skills/rust-developer/references/rust-rules/test-integration-dir.md +0 -144
- package/template/agent/skills/rust-developer/references/rust-rules/test-mock-traits.md +0 -189
- package/template/agent/skills/rust-developer/references/rust-rules/test-mockall-mocking.md +0 -226
- package/template/agent/skills/rust-developer/references/rust-rules/test-proptest-properties.md +0 -161
- package/template/agent/skills/rust-developer/references/rust-rules/test-should-panic.md +0 -130
- package/template/agent/skills/rust-developer/references/rust-rules/test-tokio-async.md +0 -154
- package/template/agent/skills/rust-developer/references/rust-rules/test-use-super.md +0 -127
- package/template/agent/skills/rust-developer/references/rust-rules/type-enum-states.md +0 -154
- package/template/agent/skills/rust-developer/references/rust-rules/type-generic-bounds.md +0 -142
- package/template/agent/skills/rust-developer/references/rust-rules/type-never-diverge.md +0 -146
- package/template/agent/skills/rust-developer/references/rust-rules/type-newtype-ids.md +0 -160
- package/template/agent/skills/rust-developer/references/rust-rules/type-newtype-validated.md +0 -159
- package/template/agent/skills/rust-developer/references/rust-rules/type-no-stringly.md +0 -144
- package/template/agent/skills/rust-developer/references/rust-rules/type-option-nullable.md +0 -137
- package/template/agent/skills/rust-developer/references/rust-rules/type-phantom-marker.md +0 -188
- package/template/agent/skills/rust-developer/references/rust-rules/type-repr-transparent.md +0 -143
- package/template/agent/skills/rust-developer/references/rust-rules/type-result-fallible.md +0 -131
- package/template/agent/skills/saas-architect/SKILL.md +0 -139
- package/template/agent/skills/security-engineer/SKILL.md +0 -133
- package/template/agent/skills/seo-specialist/SKILL.md +0 -130
- package/template/agent/skills/solo-founder-ops/SKILL.md +0 -56
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
# doc-link-types
|
|
2
|
-
|
|
3
|
-
> Use intra-doc links to connect related types and functions
|
|
4
|
-
|
|
5
|
-
## Why It Matters
|
|
6
|
-
|
|
7
|
-
Intra-doc links (`[`TypeName`]`) create clickable references in generated documentation. They enable navigation between related items, verify that referenced items exist at compile time, and update automatically when items are renamed. Plain text references become stale and unclickable.
|
|
8
|
-
|
|
9
|
-
## Bad
|
|
10
|
-
|
|
11
|
-
```rust
|
|
12
|
-
/// Parses input and returns a ParseResult.
|
|
13
|
-
///
|
|
14
|
-
/// See also: ParseError for error types.
|
|
15
|
-
/// Uses the Tokenizer internally.
|
|
16
|
-
pub fn parse(input: &str) -> ParseResult {
|
|
17
|
-
// "ParseResult", "ParseError", "Tokenizer" are not clickable
|
|
18
|
-
// No verification they exist
|
|
19
|
-
}
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Good
|
|
23
|
-
|
|
24
|
-
```rust
|
|
25
|
-
/// Parses input and returns a [`ParseResult`].
|
|
26
|
-
///
|
|
27
|
-
/// # Errors
|
|
28
|
-
///
|
|
29
|
-
/// Returns [`ParseError::InvalidSyntax`] if the input contains invalid tokens.
|
|
30
|
-
/// Returns [`ParseError::UnexpectedEof`] if the input ends prematurely.
|
|
31
|
-
///
|
|
32
|
-
/// # Related
|
|
33
|
-
///
|
|
34
|
-
/// - [`Tokenizer`] - The underlying tokenizer used by this parser
|
|
35
|
-
/// - [`parse_file`] - Convenience function for parsing files
|
|
36
|
-
/// - [`ParseOptions`] - Configuration options for parsing
|
|
37
|
-
pub fn parse(input: &str) -> ParseResult {
|
|
38
|
-
// All links are clickable and verified
|
|
39
|
-
}
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
## Link Syntax
|
|
43
|
-
|
|
44
|
-
```rust
|
|
45
|
-
/// Basic link to type in same module
|
|
46
|
-
/// See [`MyType`] for details.
|
|
47
|
-
|
|
48
|
-
/// Link to method
|
|
49
|
-
/// Use [`MyType::new`] to create instances.
|
|
50
|
-
|
|
51
|
-
/// Link to associated type
|
|
52
|
-
/// Returns [`Iterator::Item`].
|
|
53
|
-
|
|
54
|
-
/// Link to module
|
|
55
|
-
/// See the [`parser`] module.
|
|
56
|
-
|
|
57
|
-
/// Link to external crate type
|
|
58
|
-
/// Works with [`std::collections::HashMap`].
|
|
59
|
-
|
|
60
|
-
/// Link with custom text
|
|
61
|
-
/// See [the parser][`parse`] for details.
|
|
62
|
-
|
|
63
|
-
/// Link to module item
|
|
64
|
-
/// See [`crate::utils::helper`].
|
|
65
|
-
|
|
66
|
-
/// Link to parent module item
|
|
67
|
-
/// See [`super::Parent`].
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
## Common Patterns
|
|
71
|
-
|
|
72
|
-
```rust
|
|
73
|
-
/// A configuration builder.
|
|
74
|
-
///
|
|
75
|
-
/// # Example
|
|
76
|
-
///
|
|
77
|
-
/// ```
|
|
78
|
-
/// use my_crate::Config;
|
|
79
|
-
///
|
|
80
|
-
/// let config = Config::builder()
|
|
81
|
-
/// .timeout(30)
|
|
82
|
-
/// .build()?;
|
|
83
|
-
/// ```
|
|
84
|
-
///
|
|
85
|
-
/// # Methods
|
|
86
|
-
///
|
|
87
|
-
/// - [`Config::builder`] - Create a new builder
|
|
88
|
-
/// - [`Config::default`] - Create with defaults
|
|
89
|
-
///
|
|
90
|
-
/// # Related Types
|
|
91
|
-
///
|
|
92
|
-
/// - [`ConfigBuilder`] - The builder returned by [`Config::builder`]
|
|
93
|
-
/// - [`ConfigError`] - Errors that can occur when building
|
|
94
|
-
pub struct Config { ... }
|
|
95
|
-
|
|
96
|
-
impl Config {
|
|
97
|
-
/// Creates a new [`ConfigBuilder`].
|
|
98
|
-
///
|
|
99
|
-
/// This is equivalent to [`ConfigBuilder::new`].
|
|
100
|
-
pub fn builder() -> ConfigBuilder { ... }
|
|
101
|
-
}
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
## Linking to Trait Items
|
|
105
|
-
|
|
106
|
-
```rust
|
|
107
|
-
/// Implements [`Iterator`] for lazy evaluation.
|
|
108
|
-
///
|
|
109
|
-
/// The [`Iterator::next`] method advances the cursor.
|
|
110
|
-
///
|
|
111
|
-
/// For parallel iteration, see [`rayon::ParallelIterator`].
|
|
112
|
-
pub struct MyIterator { ... }
|
|
113
|
-
|
|
114
|
-
impl Iterator for MyIterator {
|
|
115
|
-
/// Advances and returns the next value.
|
|
116
|
-
///
|
|
117
|
-
/// See also [`Iterator::nth`] for skipping elements.
|
|
118
|
-
fn next(&mut self) -> Option<Self::Item> { ... }
|
|
119
|
-
}
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
## Broken Link Detection
|
|
123
|
-
|
|
124
|
-
```bash
|
|
125
|
-
# Catch broken intra-doc links
|
|
126
|
-
RUSTDOCFLAGS="-D warnings" cargo doc
|
|
127
|
-
|
|
128
|
-
# Or in CI
|
|
129
|
-
cargo doc --no-deps 2>&1 | grep "warning: unresolved link"
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
```toml
|
|
133
|
-
# Cargo.toml - deny broken links
|
|
134
|
-
[lints.rustdoc]
|
|
135
|
-
broken_intra_doc_links = "deny"
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
## Module-Level Documentation
|
|
139
|
-
|
|
140
|
-
```rust
|
|
141
|
-
//! # Parser Module
|
|
142
|
-
//!
|
|
143
|
-
//! This module provides parsing utilities.
|
|
144
|
-
//!
|
|
145
|
-
//! ## Main Types
|
|
146
|
-
//!
|
|
147
|
-
//! - [`Parser`] - The main parser struct
|
|
148
|
-
//! - [`Token`] - Tokens produced by tokenization
|
|
149
|
-
//! - [`Ast`] - The abstract syntax tree
|
|
150
|
-
//!
|
|
151
|
-
//! ## Functions
|
|
152
|
-
//!
|
|
153
|
-
//! - [`parse`] - Parse a string
|
|
154
|
-
//! - [`parse_file`] - Parse a file
|
|
155
|
-
//!
|
|
156
|
-
//! ## Errors
|
|
157
|
-
//!
|
|
158
|
-
//! All functions return [`ParseError`] on failure.
|
|
159
|
-
|
|
160
|
-
pub struct Parser { ... }
|
|
161
|
-
pub enum Token { ... }
|
|
162
|
-
pub struct Ast { ... }
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
## See Also
|
|
166
|
-
|
|
167
|
-
- [doc-examples-section](./doc-examples-section.md) - Code examples in docs
|
|
168
|
-
- [err-doc-errors](./err-doc-errors.md) - Documenting errors
|
|
169
|
-
- [lint-deny-correctness](./lint-deny-correctness.md) - Lint settings
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
# doc-module-inner
|
|
2
|
-
|
|
3
|
-
> Use `//!` for module-level documentation
|
|
4
|
-
|
|
5
|
-
## Why It Matters
|
|
6
|
-
|
|
7
|
-
Inner doc comments (`//!`) document the module itself, not the next item. They appear at the top of module files and describe the module's purpose, contents, and usage patterns. This helps users understand what a module provides before diving into individual items.
|
|
8
|
-
|
|
9
|
-
Module docs are the first thing users see in `cargo doc` when navigating to a module.
|
|
10
|
-
|
|
11
|
-
## Bad
|
|
12
|
-
|
|
13
|
-
```rust
|
|
14
|
-
// This module handles authentication
|
|
15
|
-
// It provides JWT and session-based auth
|
|
16
|
-
|
|
17
|
-
mod auth;
|
|
18
|
-
|
|
19
|
-
pub use auth::*;
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
```rust
|
|
23
|
-
// auth.rs
|
|
24
|
-
/// Authentication utilities // Wrong: this documents nothing useful
|
|
25
|
-
use std::collections::HashMap;
|
|
26
|
-
|
|
27
|
-
pub struct Session { /* ... */ }
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
## Good
|
|
31
|
-
|
|
32
|
-
```rust
|
|
33
|
-
//! Authentication and authorization utilities.
|
|
34
|
-
//!
|
|
35
|
-
//! This module provides multiple authentication strategies:
|
|
36
|
-
//!
|
|
37
|
-
//! - [`JwtAuth`] - JSON Web Token based authentication
|
|
38
|
-
//! - [`SessionAuth`] - Cookie-based session authentication
|
|
39
|
-
//! - [`ApiKeyAuth`] - API key authentication for services
|
|
40
|
-
//!
|
|
41
|
-
//! # Examples
|
|
42
|
-
//!
|
|
43
|
-
//! ```
|
|
44
|
-
//! use my_crate::auth::{JwtAuth, Authenticator};
|
|
45
|
-
//!
|
|
46
|
-
//! let auth = JwtAuth::new("secret-key");
|
|
47
|
-
//! let token = auth.generate_token(&user)?;
|
|
48
|
-
//! ```
|
|
49
|
-
//!
|
|
50
|
-
//! # Feature Flags
|
|
51
|
-
//!
|
|
52
|
-
//! - `jwt` - Enables JWT authentication (enabled by default)
|
|
53
|
-
//! - `sessions` - Enables session-based authentication
|
|
54
|
-
|
|
55
|
-
use std::collections::HashMap;
|
|
56
|
-
|
|
57
|
-
pub struct Session { /* ... */ }
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## Where to Use Inner Docs
|
|
61
|
-
|
|
62
|
-
| Location | Purpose |
|
|
63
|
-
|----------|---------|
|
|
64
|
-
| `lib.rs` | Crate-level documentation (appears on crate root) |
|
|
65
|
-
| `mod.rs` | Module documentation for directory modules |
|
|
66
|
-
| `module.rs` | Module documentation for single-file modules |
|
|
67
|
-
|
|
68
|
-
## Crate Root Example
|
|
69
|
-
|
|
70
|
-
```rust
|
|
71
|
-
//! # My Awesome Crate
|
|
72
|
-
//!
|
|
73
|
-
//! `my_crate` provides utilities for handling complex workflows.
|
|
74
|
-
//!
|
|
75
|
-
//! ## Quick Start
|
|
76
|
-
//!
|
|
77
|
-
//! ```rust
|
|
78
|
-
//! use my_crate::prelude::*;
|
|
79
|
-
//!
|
|
80
|
-
//! let workflow = Workflow::builder()
|
|
81
|
-
//! .add_step(Step::new("fetch"))
|
|
82
|
-
//! .add_step(Step::new("process"))
|
|
83
|
-
//! .build();
|
|
84
|
-
//! ```
|
|
85
|
-
//!
|
|
86
|
-
//! ## Modules
|
|
87
|
-
//!
|
|
88
|
-
//! - [`workflow`] - Core workflow engine
|
|
89
|
-
//! - [`steps`] - Built-in workflow steps
|
|
90
|
-
//! - [`prelude`] - Common imports
|
|
91
|
-
//!
|
|
92
|
-
//! ## Feature Flags
|
|
93
|
-
//!
|
|
94
|
-
//! | Feature | Description |
|
|
95
|
-
//! |---------|-------------|
|
|
96
|
-
//! | `async` | Async workflow execution |
|
|
97
|
-
//! | `serde` | Serialization support |
|
|
98
|
-
|
|
99
|
-
pub mod workflow;
|
|
100
|
-
pub mod steps;
|
|
101
|
-
pub mod prelude;
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
## Key Sections for Module Docs
|
|
105
|
-
|
|
106
|
-
1. **Brief description** - One-line summary
|
|
107
|
-
2. **Overview** - What the module provides
|
|
108
|
-
3. **Examples** - How to use it
|
|
109
|
-
4. **Feature flags** - Optional functionality
|
|
110
|
-
5. **See Also** - Related modules
|
|
111
|
-
|
|
112
|
-
## See Also
|
|
113
|
-
|
|
114
|
-
- [doc-all-public](./doc-all-public.md) - Documenting public items
|
|
115
|
-
- [doc-examples-section](./doc-examples-section.md) - Adding examples
|
|
116
|
-
- [doc-cargo-metadata](./doc-cargo-metadata.md) - Crate metadata
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
# doc-panics-section
|
|
2
|
-
|
|
3
|
-
> Include `# Panics` section for functions that can panic
|
|
4
|
-
|
|
5
|
-
## Why It Matters
|
|
6
|
-
|
|
7
|
-
Panics are exceptional conditions that crash the program (or unwind the stack). Users need to know when a function might panic so they can ensure preconditions are met or avoid the function in contexts where panics are unacceptable (e.g., `no_std`, embedded, FFI).
|
|
8
|
-
|
|
9
|
-
If a function can panic, document exactly when.
|
|
10
|
-
|
|
11
|
-
## Bad
|
|
12
|
-
|
|
13
|
-
```rust
|
|
14
|
-
/// Returns the element at the given index.
|
|
15
|
-
pub fn get(index: usize) -> &T {
|
|
16
|
-
&self.data[index] // Panics if out of bounds - not documented!
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/// Divides two numbers.
|
|
20
|
-
pub fn divide(a: i32, b: i32) -> i32 {
|
|
21
|
-
a / b // Panics on division by zero - not documented!
|
|
22
|
-
}
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
## Good
|
|
26
|
-
|
|
27
|
-
```rust
|
|
28
|
-
/// Returns the element at the given index.
|
|
29
|
-
///
|
|
30
|
-
/// # Panics
|
|
31
|
-
///
|
|
32
|
-
/// Panics if `index` is out of bounds (i.e., `index >= self.len()`).
|
|
33
|
-
///
|
|
34
|
-
/// # Examples
|
|
35
|
-
///
|
|
36
|
-
/// ```
|
|
37
|
-
/// let v = vec![1, 2, 3];
|
|
38
|
-
/// assert_eq!(v.get(1), &2);
|
|
39
|
-
/// ```
|
|
40
|
-
pub fn get(&self, index: usize) -> &T {
|
|
41
|
-
&self.data[index]
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/// Divides two numbers.
|
|
45
|
-
///
|
|
46
|
-
/// # Panics
|
|
47
|
-
///
|
|
48
|
-
/// Panics if `divisor` is zero.
|
|
49
|
-
///
|
|
50
|
-
/// For a non-panicking version, use [`checked_divide`].
|
|
51
|
-
pub fn divide(dividend: i32, divisor: i32) -> i32 {
|
|
52
|
-
dividend / divisor
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/// Divides two numbers, returning `None` if the divisor is zero.
|
|
56
|
-
pub fn checked_divide(dividend: i32, divisor: i32) -> Option<i32> {
|
|
57
|
-
if divisor == 0 {
|
|
58
|
-
None
|
|
59
|
-
} else {
|
|
60
|
-
Some(dividend / divisor)
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## Common Panic Conditions
|
|
66
|
-
|
|
67
|
-
| Operation | Panic Condition |
|
|
68
|
-
|-----------|-----------------|
|
|
69
|
-
| Index access `[i]` | Index out of bounds |
|
|
70
|
-
| Division `/`, `%` | Division by zero |
|
|
71
|
-
| `.unwrap()` | `None` or `Err` value |
|
|
72
|
-
| `.expect()` | `None` or `Err` value |
|
|
73
|
-
| `slice::split_at(mid)` | `mid > len` |
|
|
74
|
-
| `Vec::remove(i)` | `i >= len` |
|
|
75
|
-
| Overflow (debug) | Integer overflow |
|
|
76
|
-
|
|
77
|
-
## Pattern: Panic vs Return Error
|
|
78
|
-
|
|
79
|
-
Document why you chose to panic vs return `Result`:
|
|
80
|
-
|
|
81
|
-
```rust
|
|
82
|
-
/// Creates a new buffer with the given capacity.
|
|
83
|
-
///
|
|
84
|
-
/// # Panics
|
|
85
|
-
///
|
|
86
|
-
/// Panics if `capacity` is zero. A buffer must have at least
|
|
87
|
-
/// one byte of capacity.
|
|
88
|
-
///
|
|
89
|
-
/// This panics rather than returning an error because a zero-capacity
|
|
90
|
-
/// buffer represents a programming error, not a runtime condition.
|
|
91
|
-
pub fn new(capacity: usize) -> Self {
|
|
92
|
-
assert!(capacity > 0, "capacity must be non-zero");
|
|
93
|
-
// ...
|
|
94
|
-
}
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
## Pattern: Debug-Only Panics
|
|
98
|
-
|
|
99
|
-
```rust
|
|
100
|
-
/// Adds an item to the collection.
|
|
101
|
-
///
|
|
102
|
-
/// # Panics
|
|
103
|
-
///
|
|
104
|
-
/// In debug builds, panics if the collection is at capacity.
|
|
105
|
-
/// In release builds, this is a no-op when at capacity.
|
|
106
|
-
pub fn push(&mut self, item: T) {
|
|
107
|
-
debug_assert!(self.len < self.capacity, "collection at capacity");
|
|
108
|
-
// ...
|
|
109
|
-
}
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
## Provide Non-Panicking Alternatives
|
|
113
|
-
|
|
114
|
-
When documenting a panicking function, point to safe alternatives:
|
|
115
|
-
|
|
116
|
-
```rust
|
|
117
|
-
/// # Panics
|
|
118
|
-
///
|
|
119
|
-
/// Panics if the index is out of bounds.
|
|
120
|
-
///
|
|
121
|
-
/// For a non-panicking version, use [`get`] which returns `Option<&T>`.
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
## See Also
|
|
125
|
-
|
|
126
|
-
- [doc-errors-section](./doc-errors-section.md) - Documenting errors
|
|
127
|
-
- [doc-safety-section](./doc-safety-section.md) - Documenting unsafe
|
|
128
|
-
- [err-result-over-panic](./err-result-over-panic.md) - Preferring Result
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
# doc-question-mark
|
|
2
|
-
|
|
3
|
-
> Use `?` in examples, not `.unwrap()`
|
|
4
|
-
|
|
5
|
-
## Why It Matters
|
|
6
|
-
|
|
7
|
-
Doc examples should model best practices. Using `.unwrap()` teaches users to ignore errors, while `?` demonstrates proper error propagation. Examples with `?` also fail the doctest if an error occurs, catching bugs in documentation.
|
|
8
|
-
|
|
9
|
-
Rust doctests wrap examples in a function that returns `Result<(), E>` by default when you use `?`, making this pattern easy to adopt.
|
|
10
|
-
|
|
11
|
-
## Bad
|
|
12
|
-
|
|
13
|
-
```rust
|
|
14
|
-
/// Reads a configuration file.
|
|
15
|
-
///
|
|
16
|
-
/// # Examples
|
|
17
|
-
///
|
|
18
|
-
/// ```
|
|
19
|
-
/// let config = Config::from_file("config.toml").unwrap();
|
|
20
|
-
/// println!("{:?}", config.database_url);
|
|
21
|
-
/// ```
|
|
22
|
-
pub fn from_file(path: &str) -> Result<Config, Error> {
|
|
23
|
-
// ...
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/// Fetches data from the API.
|
|
27
|
-
///
|
|
28
|
-
/// # Examples
|
|
29
|
-
///
|
|
30
|
-
/// ```
|
|
31
|
-
/// let client = Client::new();
|
|
32
|
-
/// let response = client.get("https://api.example.com").unwrap();
|
|
33
|
-
/// let data: Data = response.json().unwrap();
|
|
34
|
-
/// ```
|
|
35
|
-
pub async fn get(&self, url: &str) -> Result<Response, Error> {
|
|
36
|
-
// ...
|
|
37
|
-
}
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
## Good
|
|
41
|
-
|
|
42
|
-
```rust
|
|
43
|
-
/// Reads a configuration file.
|
|
44
|
-
///
|
|
45
|
-
/// # Examples
|
|
46
|
-
///
|
|
47
|
-
/// ```
|
|
48
|
-
/// # use my_crate::{Config, Error};
|
|
49
|
-
/// # fn main() -> Result<(), Error> {
|
|
50
|
-
/// let config = Config::from_file("config.toml")?;
|
|
51
|
-
/// println!("{:?}", config.database_url);
|
|
52
|
-
/// # Ok(())
|
|
53
|
-
/// # }
|
|
54
|
-
/// ```
|
|
55
|
-
pub fn from_file(path: &str) -> Result<Config, Error> {
|
|
56
|
-
// ...
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/// Fetches data from the API.
|
|
60
|
-
///
|
|
61
|
-
/// # Examples
|
|
62
|
-
///
|
|
63
|
-
/// ```no_run
|
|
64
|
-
/// # use my_crate::{Client, Data, Error};
|
|
65
|
-
/// # async fn example() -> Result<(), Error> {
|
|
66
|
-
/// let client = Client::new();
|
|
67
|
-
/// let response = client.get("https://api.example.com").await?;
|
|
68
|
-
/// let data: Data = response.json().await?;
|
|
69
|
-
/// # Ok(())
|
|
70
|
-
/// # }
|
|
71
|
-
/// ```
|
|
72
|
-
pub async fn get(&self, url: &str) -> Result<Response, Error> {
|
|
73
|
-
// ...
|
|
74
|
-
}
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
## Doctest Wrapper Pattern
|
|
78
|
-
|
|
79
|
-
Rust wraps doc examples in a function. You can make this explicit:
|
|
80
|
-
|
|
81
|
-
```rust
|
|
82
|
-
/// # Examples
|
|
83
|
-
///
|
|
84
|
-
/// ```
|
|
85
|
-
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
86
|
-
/// let value = parse_config("key=value")?;
|
|
87
|
-
/// assert_eq!(value.key, "value");
|
|
88
|
-
/// # Ok(())
|
|
89
|
-
/// # }
|
|
90
|
-
/// ```
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
Or use the implicit wrapper (Rust 2021+):
|
|
94
|
-
|
|
95
|
-
```rust
|
|
96
|
-
/// # Examples
|
|
97
|
-
///
|
|
98
|
-
/// ```
|
|
99
|
-
/// # use my_crate::parse_config;
|
|
100
|
-
/// let value = parse_config("key=value")?;
|
|
101
|
-
/// assert_eq!(value.key, "value");
|
|
102
|
-
/// # Ok::<(), my_crate::Error>(())
|
|
103
|
-
/// ```
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
## When to Use `.unwrap()`
|
|
107
|
-
|
|
108
|
-
There are specific cases where `.unwrap()` is acceptable in examples:
|
|
109
|
-
|
|
110
|
-
```rust
|
|
111
|
-
/// # Examples
|
|
112
|
-
///
|
|
113
|
-
/// ```
|
|
114
|
-
/// // Static regex that is known at compile time to be valid
|
|
115
|
-
/// let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
|
|
116
|
-
///
|
|
117
|
-
/// // Parsing a literal that cannot fail
|
|
118
|
-
/// let n: i32 = "42".parse().unwrap();
|
|
119
|
-
/// ```
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
But still prefer `?` when demonstrating error handling patterns.
|
|
123
|
-
|
|
124
|
-
## Comparison
|
|
125
|
-
|
|
126
|
-
| Pattern | Behavior on Error | Teaches |
|
|
127
|
-
|---------|-------------------|---------|
|
|
128
|
-
| `.unwrap()` | Panics with generic message | Bad habits |
|
|
129
|
-
| `.expect()` | Panics with custom message | Slightly better |
|
|
130
|
-
| `?` | Propagates error, test fails | Best practices |
|
|
131
|
-
|
|
132
|
-
## See Also
|
|
133
|
-
|
|
134
|
-
- [doc-examples-section](./doc-examples-section.md) - Writing examples
|
|
135
|
-
- [doc-hidden-setup](./doc-hidden-setup.md) - Hiding setup code
|
|
136
|
-
- [err-question-mark](./err-question-mark.md) - Error propagation
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
# doc-safety-section
|
|
2
|
-
|
|
3
|
-
> Include `# Safety` section for unsafe functions
|
|
4
|
-
|
|
5
|
-
## Why It Matters
|
|
6
|
-
|
|
7
|
-
Unsafe functions require callers to uphold invariants that the compiler cannot verify. The `# Safety` section documents exactly what the caller must guarantee for the function to be sound. Without this, users cannot safely call the function.
|
|
8
|
-
|
|
9
|
-
This is not optional—it's a requirement for sound unsafe code.
|
|
10
|
-
|
|
11
|
-
## Bad
|
|
12
|
-
|
|
13
|
-
```rust
|
|
14
|
-
/// Reads a value from a raw pointer.
|
|
15
|
-
pub unsafe fn read_ptr<T>(ptr: *const T) -> T {
|
|
16
|
-
// What guarantees must the caller provide? Unknown!
|
|
17
|
-
ptr.read()
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/// Creates a string from raw parts.
|
|
21
|
-
pub unsafe fn string_from_raw(ptr: *mut u8, len: usize, cap: usize) -> String {
|
|
22
|
-
String::from_raw_parts(ptr, len, cap)
|
|
23
|
-
}
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
## Good
|
|
27
|
-
|
|
28
|
-
```rust
|
|
29
|
-
/// Reads a value from a raw pointer.
|
|
30
|
-
///
|
|
31
|
-
/// # Safety
|
|
32
|
-
///
|
|
33
|
-
/// The caller must ensure that:
|
|
34
|
-
/// - `ptr` is valid for reads of `size_of::<T>()` bytes
|
|
35
|
-
/// - `ptr` is properly aligned for type `T`
|
|
36
|
-
/// - `ptr` points to a properly initialized value of type `T`
|
|
37
|
-
/// - The memory referenced by `ptr` is not mutated during this call
|
|
38
|
-
pub unsafe fn read_ptr<T>(ptr: *const T) -> T {
|
|
39
|
-
ptr.read()
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/// Creates a `String` from raw parts.
|
|
43
|
-
///
|
|
44
|
-
/// # Safety
|
|
45
|
-
///
|
|
46
|
-
/// The caller must guarantee that:
|
|
47
|
-
/// - `ptr` was allocated by the same allocator that `String` uses
|
|
48
|
-
/// - `len` is less than or equal to `cap`
|
|
49
|
-
/// - The first `len` bytes at `ptr` are valid UTF-8
|
|
50
|
-
/// - `cap` is the capacity that `ptr` was allocated with
|
|
51
|
-
/// - No other code will use `ptr` after this call (ownership is transferred)
|
|
52
|
-
///
|
|
53
|
-
/// Violating these requirements leads to undefined behavior including
|
|
54
|
-
/// memory corruption, use-after-free, or invalid UTF-8 in strings.
|
|
55
|
-
pub unsafe fn string_from_raw(ptr: *mut u8, len: usize, cap: usize) -> String {
|
|
56
|
-
String::from_raw_parts(ptr, len, cap)
|
|
57
|
-
}
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## Key Elements of Safety Documentation
|
|
61
|
-
|
|
62
|
-
| Element | Description |
|
|
63
|
-
|---------|-------------|
|
|
64
|
-
| **Preconditions** | What must be true before calling |
|
|
65
|
-
| **Pointer validity** | Alignment, null-ness, lifetime |
|
|
66
|
-
| **Memory ownership** | Who owns what, transfer semantics |
|
|
67
|
-
| **Invariants** | Type invariants that must hold |
|
|
68
|
-
| **Consequences** | What happens if violated |
|
|
69
|
-
|
|
70
|
-
## Pattern: Unsafe Trait Implementations
|
|
71
|
-
|
|
72
|
-
```rust
|
|
73
|
-
/// A type that can be safely zeroed.
|
|
74
|
-
///
|
|
75
|
-
/// # Safety
|
|
76
|
-
///
|
|
77
|
-
/// Implementing this trait guarantees that:
|
|
78
|
-
/// - All bit patterns of zeros represent a valid value of this type
|
|
79
|
-
/// - The type has no padding bytes that could leak data
|
|
80
|
-
/// - The type contains no references or pointers
|
|
81
|
-
pub unsafe trait Zeroable {
|
|
82
|
-
fn zeroed() -> Self;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// SAFETY: u32 is a primitive integer type where all zero bits
|
|
86
|
-
// represent a valid value (0).
|
|
87
|
-
unsafe impl Zeroable for u32 {
|
|
88
|
-
fn zeroed() -> Self {
|
|
89
|
-
0
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
## Pattern: Unsafe Blocks in Safe Functions
|
|
95
|
-
|
|
96
|
-
When a safe function contains unsafe blocks, document the invariants:
|
|
97
|
-
|
|
98
|
-
```rust
|
|
99
|
-
/// Returns a reference to the element at the given index.
|
|
100
|
-
///
|
|
101
|
-
/// Returns `None` if the index is out of bounds.
|
|
102
|
-
pub fn get(&self, index: usize) -> Option<&T> {
|
|
103
|
-
if index < self.len {
|
|
104
|
-
// SAFETY: We just verified that index < len, so this
|
|
105
|
-
// access is within bounds.
|
|
106
|
-
Some(unsafe { self.data.get_unchecked(index) })
|
|
107
|
-
} else {
|
|
108
|
-
None
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
## Common Safety Requirements
|
|
114
|
-
|
|
115
|
-
```rust
|
|
116
|
-
/// # Safety
|
|
117
|
-
///
|
|
118
|
-
/// - Pointer must be non-null
|
|
119
|
-
/// - Pointer must be aligned to `align_of::<T>()`
|
|
120
|
-
/// - Pointer must be valid for reads/writes of `size_of::<T>()` bytes
|
|
121
|
-
/// - Pointer must point to an initialized value of `T`
|
|
122
|
-
/// - The referenced memory must not be accessed through any other pointer
|
|
123
|
-
/// for the duration of the returned reference
|
|
124
|
-
/// - The total size must not exceed `isize::MAX`
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
## See Also
|
|
128
|
-
|
|
129
|
-
- [doc-panics-section](./doc-panics-section.md) - Documenting panics
|
|
130
|
-
- [lint-unsafe-doc](./lint-unsafe-doc.md) - Enforcing unsafe documentation
|
|
131
|
-
- [doc-errors-section](./doc-errors-section.md) - Documenting errors
|