diff options
Diffstat (limited to 'embassy-sync/src')
| -rw-r--r-- | embassy-sync/src/lazy_lock.rs | 15 |
1 files changed, 15 insertions, 0 deletions
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(); |
