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 ## Unreleased +- Add `get_mut` to `LazyLock` + ## 0.7.0 - 2025-05-28 - 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> LazyLock { 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 = 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 = LazyLock::new(|| 20); let value = lazy.into_inner();