diff options
| author | Emil Fresk <emil.fresk@gmail.com> | 2021-09-22 13:22:45 +0200 |
|---|---|---|
| committer | Emil Fresk <emil.fresk@gmail.com> | 2021-09-23 16:11:04 +0200 |
| commit | b71df58f2fb4ed85d4c8cf806d5837ce63c73f31 (patch) | |
| tree | de4cbe4b43d399d4dcf2021c33225ccd00627434 /book/en/src/by-example/tips_static_lifetimes.md | |
| parent | c8621d78b9b1c0c67dff31404ade873a9d7b426e (diff) | |
The great docs update
Diffstat (limited to 'book/en/src/by-example/tips_static_lifetimes.md')
| -rw-r--r-- | book/en/src/by-example/tips_static_lifetimes.md | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/book/en/src/by-example/tips_static_lifetimes.md b/book/en/src/by-example/tips_static_lifetimes.md new file mode 100644 index 0000000..3ea0816 --- /dev/null +++ b/book/en/src/by-example/tips_static_lifetimes.md @@ -0,0 +1,24 @@ +# 'static super-powers + +As discussed earlier `local` resources are given `'static` lifetime in `#[init]` and `#[idle]`, +this can be used to allocate an object and then split it up or give the pre-allocated object to a +task, driver or some other object. +This is very useful when needing to allocate memory for drivers, such as USB drivers, and using +data structures that can be split such as [`heapless::spsc::Queue`]. + +In the following example an [`heapless::spsc::Queue`] is given to two different tasks for lock-free access +to the shared queue. + +[`heapless::spsc::Queue`]: https://docs.rs/heapless/0.7.5/heapless/spsc/struct.Queue.html + + +``` rust +{{#include ../../../../examples/static.rs}} +``` + +Running this program produces the expected output. + +``` console +$ cargo run --target thumbv7m-none-eabi --example static +{{#include ../../../../ci/expected/static.run}} +``` |
