|
1 | 1 | --- |
2 | 2 | name: "rust-coding-skill" |
3 | 3 | description: "Use whenever editing Rust in PET to write allocation-aware, cross-platform, byte-safe code with behavior-proving tests." |
4 | | -user-invocable: true |
5 | 4 | --- |
6 | 5 |
|
7 | 6 | # PET Rust Coding Skill |
@@ -33,9 +32,13 @@ Never calculate byte offsets from a transformed Unicode string and apply them to |
33 | 32 | For ASCII wire/file markers, use byte-stable ASCII-insensitive matching and checked slicing: |
34 | 33 |
|
35 | 34 | ```rust |
36 | | -let start = find_ascii_case_insensitive(line, "# cmd:")? + "# cmd:".len(); |
37 | | -let end = find_ascii_case_insensitive(line, " create -")?; |
38 | | -let value = line.get(start..end)?.trim(); |
| 35 | +let marker = b"# cmd:"; |
| 36 | +let start = line |
| 37 | + .as_bytes() |
| 38 | + .windows(marker.len()) |
| 39 | + .position(|window| window.eq_ignore_ascii_case(marker))? |
| 40 | + + marker.len(); |
| 41 | +let value = line.get(start..)?.trim(); |
39 | 42 | ``` |
40 | 43 |
|
41 | 44 | Use `to_ascii_lowercase` rather than `to_lowercase` when the format is defined as ASCII. Add a non-ASCII path regression test whenever offsets are derived from textual markers. |
@@ -70,14 +73,14 @@ Tests should demonstrate the behavior or performance invariant, not merely execu |
70 | 73 | For optimizations, instrument the dependency boundary and assert the operation count: |
71 | 74 |
|
72 | 75 | ```rust |
73 | | -let reads = Cell::new(0); |
| 76 | +let reads = AtomicUsize::new(0); |
74 | 77 | parse_with_reader(path, |_| { |
75 | | - reads.set(reads.get() + 1); |
| 78 | + reads.fetch_add(1, Ordering::Relaxed); |
76 | 79 | Some(history.clone()) |
77 | 80 | }); |
78 | | -assert_eq!(reads.get(), 1); |
| 81 | +assert_eq!(reads.load(Ordering::Relaxed), 1); |
79 | 82 | ``` |
80 | 83 |
|
81 | 84 | For parser helpers, include malformed input, non-ASCII surrounding data, and case variations. For diagnostics, test pattern classification and expansion filtering separately. Keep temp paths unique with `tempfile` or process/counter-based names. |
82 | 85 |
|
83 | | -Before every Rust commit, run the targeted tests plus `scripts/rust-precommit.ps1` (or `.sh`). Do not suppress Clippy warnings to land a change. |
| 86 | +Before every Rust commit, run targeted tests and invoke the `rust-precommit` skill. Keep that skill as the single source of truth for required format and Clippy commands. |
0 commit comments