diff options
| author | Jorge Aparicio <jorge@japaric.io> | 2018-11-03 17:02:41 +0100 |
|---|---|---|
| committer | Jorge Aparicio <jorge@japaric.io> | 2018-11-03 17:16:55 +0100 |
| commit | c631049efcadca8b07940c794cce2be58fa48444 (patch) | |
| tree | f6bd73e5c396fc06072557ee965cc59e9c8e3e9f /book/src/by-example/types-send-sync.md | |
| parent | 653338e7997a0cdc5deaed98b1bb5f60006717ed (diff) | |
v0.4.0
closes #32
closes #33
Diffstat (limited to 'book/src/by-example/types-send-sync.md')
| -rw-r--r-- | book/src/by-example/types-send-sync.md | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/book/src/by-example/types-send-sync.md b/book/src/by-example/types-send-sync.md new file mode 100644 index 0000000..6433060 --- /dev/null +++ b/book/src/by-example/types-send-sync.md @@ -0,0 +1,60 @@ +# Types, Send and Sync + +The `app` attribute injects a context, a collection of variables, into every +function. All these variables have predictable, non-anonymous types so you can +write plain functions that take them as arguments. + +The API reference specifies how these types are generated from the input. You +can also generate documentation for you binary crate (`cargo doc --bin <name>`); +in the documentation you'll find `Context` structs (e.g. `init::Context` and +`idle::Context`) whose fields represent the variables injected into each +function. + +The example below shows the different types generates by the `app` attribute. + +``` rust +{{#include ../../../examples/types.rs}} +``` + +## `Send` + +[`Send`] is a marker trait for "types that can be transferred across thread +boundaries", according to its definition in `core`. In the context of RTFM the +`Send` trait is only required where it's possible to transfer a value between +tasks that run at *different* priorities. This occurs in a few places: in message +passing, in shared `static mut` resources and in the initialization of late +resources. + +[`Send`]: https://doc.rust-lang.org/core/marker/trait.Send.html + +The `app` attribute will enforce that `Send` is implemented where required so +you don't need to worry much about it. It's more important to know where you do +*not* need the `Send` trait: on types that are transferred between tasks that +run at the *same* priority. This occurs in two places: in message passing and in +shared `static mut` resources. + +The example below shows where a type that doesn't implement `Send` can be used. + +``` rust +{{#include ../../../examples/not-send.rs}} +``` + +## `Sync` + +Similarly, [`Sync`] is a marker trait for "types for which it is safe to share +references between threads", according to its definition in `core`. In the +context of RTFM the `Sync` trait is only required where it's possible for two, +or more, tasks that run at different priority to hold a shared reference to a +resource. This only occurs with shared `static` resources. + +[`Sync`]: https://doc.rust-lang.org/core/marker/trait.Sync.html + +The `app` attribute will enforce that `Sync` is implemented where required but +it's important to know where the `Sync` bound is not required: in `static` +resources shared between tasks that run at the *same* priority. + +The example below shows where a type that doesn't implement `Sync` can be used. + +``` rust +{{#include ../../../examples/not-sync.rs}} +``` |
