mirror of
https://github.com/embassy-rs/embassy.git
synced 2025-09-28 04:40:39 +00:00
Merge pull request #4485 from AnthonyGrondin/feat/lazy-lock-mut
feat(embassy-sync): Add `get_mut` for `LazyLock`
This commit is contained in:
commit
3220878e13
@ -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}`.
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user