diff options
| author | Anthony Grondin <[email protected]> | 2025-07-30 16:01:11 -0400 |
|---|---|---|
| committer | Anthony Grondin <[email protected]> | 2025-07-30 16:15:58 -0400 |
| commit | 03b60dd5619bb65dff697cf9dd96f57ccc23f35e (patch) | |
| tree | 012372e1fc068d3ae1c4e15f6b9b132fb4a202e5 /embassy-sync | |
| parent | e145a653cf9d31f101b0735406b8b7c9208bc1da (diff) | |
feat(embassy-sync): Add `get_mut` for `LazyLock`
Diffstat (limited to 'embassy-sync')
| -rw-r--r-- | embassy-sync/CHANGELOG.md | 2 | ||||
| -rw-r--r-- | embassy-sync/src/lazy_lock.rs | 15 |
2 files changed, 17 insertions, 0 deletions
diff --git a/embassy-sync/CHANGELOG.md b/embassy-sync/CHANGELOG.md index 89684de0c..c445e9ba1 100644 --- a/embassy-sync/CHANGELOG.md +++ b/embassy-sync/CHANGELOG.md | |||
| @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |||
| 7 | 7 | ||
| 8 | ## Unreleased | 8 | ## Unreleased |
| 9 | 9 | ||
| 10 | - Add `get_mut` to `LazyLock` | ||
| 11 | |||
| 10 | ## 0.7.0 - 2025-05-28 | 12 | ## 0.7.0 - 2025-05-28 |
| 11 | 13 | ||
| 12 | - Add `remove_if` to `priority_channel::{Receiver, PriorityChannel}`. | 14 | - Add `remove_if` to `priority_channel::{Receiver, PriorityChannel}`. |
diff --git a/embassy-sync/src/lazy_lock.rs b/embassy-sync/src/lazy_lock.rs index f1bd88b61..a919f0037 100644 --- a/embassy-sync/src/lazy_lock.rs +++ b/embassy-sync/src/lazy_lock.rs | |||
| @@ -57,6 +57,14 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> { | |||
| 57 | unsafe { &(*self.data.get()).value } | 57 | unsafe { &(*self.data.get()).value } |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | /// Get a mutable reference to the underlying value, initializing it if it | ||
| 61 | /// has not been done already. | ||
| 62 | #[inline] | ||
| 63 | pub fn get_mut(&mut self) -> &mut T { | ||
| 64 | self.ensure_init_fast(); | ||
| 65 | unsafe { &mut (*self.data.get()).value } | ||
| 66 | } | ||
| 67 | |||
| 60 | /// Consume the `LazyLock`, returning the underlying value. The | 68 | /// Consume the `LazyLock`, returning the underlying value. The |
| 61 | /// initialization function will be called if it has not been | 69 | /// initialization function will be called if it has not been |
| 62 | /// already. | 70 | /// already. |
| @@ -122,6 +130,13 @@ mod tests { | |||
| 122 | assert_eq!(reference, &20); | 130 | assert_eq!(reference, &20); |
| 123 | } | 131 | } |
| 124 | #[test] | 132 | #[test] |
| 133 | fn test_lazy_lock_mutation() { | ||
| 134 | let mut value: LazyLock<u32> = LazyLock::new(|| 20); | ||
| 135 | *value.get_mut() = 21; | ||
| 136 | let reference = value.get(); | ||
| 137 | assert_eq!(reference, &21); | ||
| 138 | } | ||
| 139 | #[test] | ||
| 125 | fn test_lazy_lock_into_inner() { | 140 | fn test_lazy_lock_into_inner() { |
| 126 | let lazy: LazyLock<u32> = LazyLock::new(|| 20); | 141 | let lazy: LazyLock<u32> = LazyLock::new(|| 20); |
| 127 | let value = lazy.into_inner(); | 142 | let value = lazy.into_inner(); |
