Skip to content

Commit d2b046e

Browse files
committed
Singleton: safe -> local, change description a bit
1 parent 379b99d commit d2b046e

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- run: cargo run --bin factory-method-render-dialog
3939
- run: cargo run --bin prototype
4040
- run: cargo run --bin simple-factory
41-
- run: cargo run --bin singleton-safe
41+
- run: cargo run --bin singleton-local
4242
- run: cargo run --bin singleton-lazy
4343
# - run: cargo run --bin singleton-mutex # Requires Rust 1.63
4444
- run: cargo run --bin singleton-once

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ cargo run --bin factory-method-maze-game
5858
cargo run --bin factory-method-render-dialog
5959
cargo run --bin prototype
6060
cargo run --bin simple-factory
61-
cargo run --bin singleton-safe
61+
cargo run --bin singleton-local
6262
cargo run --bin singleton-lazy
6363
cargo run --bin singleton-mutex # Requires Rust 1.63
6464
cargo run --bin singleton-once

creational/singleton/how-to-create/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ lazy_static = "1.4.0"
88
once_cell = "1.15"
99

1010
[[bin]]
11-
name = "singleton-safe"
12-
path = "safe.rs"
11+
name = "singleton-local"
12+
path = "local.rs"
1313

1414
[[bin]]
1515
name = "singleton-lazy"

creational/singleton/how-to-create/README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,19 @@ in your code:
2828
2. [lazy_static::lazy_static](https://docs.rs/lazy_static/latest/lazy_static)
2929
3. [std::sync::Mutex](https://doc.rust-lang.org/std/sync/struct.Mutex.html)
3030

31-
💡 Starting with **Rust 1.63**, `Mutex::new` is `const`, you can use global
32-
static `Mutex` locks without needing lazy initialization. See the `mutex.rs`
33-
example below.
31+
In a general case, you can start with `OnceCell` like in the `once.rs` example
32+
(see below).
3433

35-
## `safe.rs`
34+
## `local.rs`
3635

37-
A pure safe way to implement Singleton in Rust is using NO global variables
36+
A safe way to implement Singleton in Rust is using NO global variables
3837
at all and passing everything around through function arguments.
3938
The oldest living variable is an object created at the start of the `main()`.
4039

4140
### How to Run
4241

4342
```bash
44-
cargo run --bin singleton-safe
43+
cargo run --bin singleton-local
4544
```
4645

4746
### Output
@@ -55,8 +54,9 @@ Final state: 1
5554
This is a singleton implementation via `lazy_static!`.
5655

5756
`lazy-static` allows declaring a static variable with lazy initialization
58-
at first access. It is actually implemented via `unsafe` with `static mut`
59-
manipulation, however, it keeps your code free of explicit `unsafe` blocks.
57+
at first access. A drawback of `lazy_static!` is that it doesn't allow
58+
initialization at the arbitrary code place, only in the static block
59+
with predefined instructions.
6060

6161
### How to Run
6262

@@ -73,10 +73,13 @@ Called 3
7373
## `once.rs`
7474

7575
`OnceCell` allows having a custom initialization of a singleton at an
76-
**arbitrary place** unlike `lazy_static!`, where the initialization must be
76+
**arbitrary place**, unlike `lazy_static!`, where the initialization must be
7777
placed in a static block. `Mutex` is still needed there to make an actual object
7878
modifiable without an `unsafe` block.
7979

80+
A [`logger`](../logger/), a practical example of Singleton, is
81+
implemented via `OnceCell`.
82+
8083
### How to Run
8184

8285
```bash

0 commit comments

Comments
 (0)