diff options
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}} +``` |
