Merge pull request #4485 from AnthonyGrondin/feat/lazy-lock-mut

feat(embassy-sync): Add `get_mut` for `LazyLock`
This commit is contained in:
Ulf Lilleengen 2025-08-08 08:03:03 +00:00 committed by GitHub
commit 3220878e13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 0 deletions

View File

@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
- Add `get_mut` to `LazyLock`
## 0.7.0 - 2025-05-28
- Add `remove_if` to `priority_channel::{Receiver, PriorityChannel}`.

View File

@ -57,6 +57,14 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
unsafe { &(*self.data.get()).value }
}
/// Get a mutable reference to the underlying value, initializing it if it
/// has not been done already.
#[inline]
pub fn get_mut(&mut self) -> &mut T {
self.ensure_init_fast();
unsafe { &mut (*self.data.get()).value }
}
/// Consume the `LazyLock`, returning the underlying value. The
/// initialization function will be called if it has not been
/// already.
@ -122,6 +130,13 @@ mod tests {
assert_eq!(reference, &20);
}
#[test]
fn test_lazy_lock_mutation() {
let mut value: LazyLock<u32> = LazyLock::new(|| 20);
*value.get_mut() = 21;
let reference = value.get();
assert_eq!(reference, &21);
}
#[test]
fn test_lazy_lock_into_inner() {
let lazy: LazyLock<u32> = LazyLock::new(|| 20);
let value = lazy.into_inner();