Skip to content

Commit a9e527a

Browse files
[agents] Add safety instructions (#2794)
- Add "Safety" section. - Instruct to avoid `&slice[0] as *const/mut`. - Instruct to avoid `&mut` to `*const` conversion for mutation. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 85f872a commit a9e527a

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

AGENTS.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ When updating UI test files (in `tests/ui*` or `zerocopy-derive/tests/ui*`), run
5757
When a PR resolves an issue, the PR description and commit message should include a line like `Closes #123`.
5858
When a PR makes progress on, but does not close, an issue, the PR description and commit message should include a line like `Makes progress on #123`.
5959

60+
## Safety
61+
62+
### Pointer Casts
63+
64+
- **Avoid `&slice[0] as *const T` or `&slice[0] as *mut T`.**
65+
Instead, use `slice.as_ptr()` or `slice.as_mut_ptr()`. Casting a reference to
66+
a single element creates a raw pointer that is only valid for that element.
67+
Accessing subsequent elements via pointer arithmetic is Undefined Behavior.
68+
See [unsafe-code-guidelines#134](https://github.com/rust-lang/unsafe-code-guidelines/issues/134).
69+
70+
- **Avoid converting `&mut T` to `*const T` (or `*const U`)**.
71+
This advice applies if you intend to later cast the pointer to `*mut T` and
72+
mutate the data. This conversion reborrows `&mut T` as a shared reference
73+
`&T`, which may restrict permissions under Stacked Borrows. Instead, cast
74+
`&mut T` directly to `*mut T` first, then to `*const T` if necessary. See
75+
[rust#56604](https://github.com/rust-lang/rust/issues/56604).
76+
6077
## Code Style
6178

6279
### Comments

0 commit comments

Comments
 (0)