aboutsummaryrefslogtreecommitdiff
path: root/book/en/src/by-example/singletons.md
blob: 0823f057f5e0fb2a2a0f18c6417d2fc20379e0e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Singletons

The `app` attribute is aware of [`owned-singleton`] crate and its [`Singleton`]
attribute. When this attribute is applied to one of the resources the runtime
will perform the `unsafe` initialization of the singleton for you, ensuring that
only a single instance of the singleton is ever created.

[`owned-singleton`]: ../../api/owned_singleton/index.html
[`Singleton`]: ../../api/owned_singleton_macros/attr.Singleton.html

Note that when using the `Singleton` attribute you'll need to have the
`owned_singleton` in your dependencies.

Below is an example that uses the `Singleton` attribute on a chunk of memory
and then uses the singleton instance as a fixed-size memory pool using one of
the [`alloc-singleton`] abstractions.

[`alloc-singleton`]: https://crates.io/crates/alloc-singleton

``` rust
{{#include ../../../../examples/singleton.rs}}
```

``` console
$ cargo run --example singleton
{{#include ../../../../ci/expected/singleton.run}}```