Skip to content

Commit 94d9ea8

Browse files
bors[bot]hyperslv
andauthored
Merge #251
251: Note on transformation of static variables by attribute exception r=therealprof a=hyperslv Hi All The "// `COUNT` has type `&mut u32` and it's safe to use" comment confused me since I did not expect that transformation of the source code so deep comparing with the C language. Finally I found how it (works)[https://docs.rs/cortex-m-rt-macros/0.1.5/src/cortex_m_rt_macros/lib.rs.html#447]. This PR is to provide a note about the transformation. Thanks, Slava Co-authored-by: hyperslv <hyper.slv@gmail.com> Co-authored-by: hyperslv <57301860+hyperslv@users.noreply.github.com>
2 parents 616962a + 5ba9989 commit 94d9ea8

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/start/exceptions.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This behavior is pretty much intended and it's required to provide a feature:
3131
fn SysTick() {
3232
static mut COUNT: u32 = 0;
3333
34-
// `COUNT` has type `&mut u32` and it's safe to use
34+
// `COUNT` has transformed to type `&mut u32` and it's safe to use
3535
*COUNT += 1;
3636
}
3737
```
@@ -47,6 +47,12 @@ use `static mut` variables. How is this possible? This is possible because
4747
`exception` handlers can *not* be called by software thus reentrancy is not
4848
possible.
4949

50+
> Note that the `exception` attribute transforms definitions of static variables
51+
> inside the function by wrapping them into `unsafe` blocks and providing us
52+
> with new appropriate variables of type `&mut` of the same name.
53+
> Thus we can derefence the reference via `*` to access the values of the variables without
54+
> needing to wrap them in an `unsafe` block.
55+
5056
## A complete example
5157

5258
Here's an example that uses the system timer to raise a `SysTick` exception

0 commit comments

Comments
 (0)