From 37a0692a0fe5d9b41b65728d496b6856a1152dcc Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 4 Nov 2018 18:50:42 +0100 Subject: impl Mutex on all shared resources document how to write generic code that operates on resources --- book/src/by-example/tips.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'book/src/by-example/tips.md') diff --git a/book/src/by-example/tips.md b/book/src/by-example/tips.md index 0e3d47b..c163328 100644 --- a/book/src/by-example/tips.md +++ b/book/src/by-example/tips.md @@ -1,5 +1,25 @@ # Tips & tricks +## Generics + +Resources shared between two or more tasks implement the `Mutex` trait in *all* +contexts, even on those where a critical section is not required to access the +data. This lets you easily write generic code that operates on resources and can +be called from different tasks. Here's one such example: + +``` rust +{{#include ../../../examples/generics.rs}} +``` + +``` console +$ cargo run --example generics +{{#include ../../../ci/expected/generics.run}}``` + +This also lets you change the static priorities of tasks without having to +rewrite code. If you consistently use `lock`s to access the data behind shared +resources then your code will continue to compile when you change the priority +of tasks. + ## Running tasks from RAM The main goal of moving the specification of RTFM applications to attributes in -- cgit v1.2.3