aboutsummaryrefslogtreecommitdiff
path: root/embassy-sync
diff options
context:
space:
mode:
authorDániel Buga <[email protected]>2024-12-30 12:13:13 +0100
committerDániel Buga <[email protected]>2024-12-30 12:13:13 +0100
commit44217aa0924e7590aa0afabdf17babd5c2ea5b82 (patch)
treee42f5d02f9b560610b870d802cf390518180c3c6 /embassy-sync
parenta4f8fddd696ca2e3705827ba4b3806cbadcb3134 (diff)
Desugar some async fns
Diffstat (limited to 'embassy-sync')
-rw-r--r--embassy-sync/src/mutex.rs5
-rw-r--r--embassy-sync/src/once_lock.rs5
-rw-r--r--embassy-sync/src/watch.rs6
-rw-r--r--embassy-sync/src/zerocopy_channel.rs24
4 files changed, 20 insertions, 20 deletions
diff --git a/embassy-sync/src/mutex.rs b/embassy-sync/src/mutex.rs
index 08f66e374..f25f74336 100644
--- a/embassy-sync/src/mutex.rs
+++ b/embassy-sync/src/mutex.rs
@@ -2,7 +2,7 @@
2//! 2//!
3//! This module provides a mutex that can be used to synchronize data between asynchronous tasks. 3//! This module provides a mutex that can be used to synchronize data between asynchronous tasks.
4use core::cell::{RefCell, UnsafeCell}; 4use core::cell::{RefCell, UnsafeCell};
5use core::future::poll_fn; 5use core::future::{poll_fn, Future};
6use core::ops::{Deref, DerefMut}; 6use core::ops::{Deref, DerefMut};
7use core::task::Poll; 7use core::task::Poll;
8use core::{fmt, mem}; 8use core::{fmt, mem};
@@ -73,7 +73,7 @@ where
73 /// Lock the mutex. 73 /// Lock the mutex.
74 /// 74 ///
75 /// This will wait for the mutex to be unlocked if it's already locked. 75 /// This will wait for the mutex to be unlocked if it's already locked.
76 pub async fn lock(&self) -> MutexGuard<'_, M, T> { 76 pub fn lock(&self) -> impl Future<Output = MutexGuard<'_, M, T>> {
77 poll_fn(|cx| { 77 poll_fn(|cx| {
78 let ready = self.state.lock(|s| { 78 let ready = self.state.lock(|s| {
79 let mut s = s.borrow_mut(); 79 let mut s = s.borrow_mut();
@@ -92,7 +92,6 @@ where
92 Poll::Pending 92 Poll::Pending
93 } 93 }
94 }) 94 })
95 .await
96 } 95 }
97 96
98 /// Attempt to immediately lock the mutex. 97 /// Attempt to immediately lock the mutex.
diff --git a/embassy-sync/src/once_lock.rs b/embassy-sync/src/once_lock.rs
index 55608ba32..cd05b986d 100644
--- a/embassy-sync/src/once_lock.rs
+++ b/embassy-sync/src/once_lock.rs
@@ -1,7 +1,7 @@
1//! Synchronization primitive for initializing a value once, allowing others to await a reference to the value. 1//! Synchronization primitive for initializing a value once, allowing others to await a reference to the value.
2 2
3use core::cell::Cell; 3use core::cell::Cell;
4use core::future::poll_fn; 4use core::future::{poll_fn, Future};
5use core::mem::MaybeUninit; 5use core::mem::MaybeUninit;
6use core::sync::atomic::{AtomicBool, Ordering}; 6use core::sync::atomic::{AtomicBool, Ordering};
7use core::task::Poll; 7use core::task::Poll;
@@ -55,7 +55,7 @@ impl<T> OnceLock<T> {
55 55
56 /// Get a reference to the underlying value, waiting for it to be set. 56 /// Get a reference to the underlying value, waiting for it to be set.
57 /// If the value is already set, this will return immediately. 57 /// If the value is already set, this will return immediately.
58 pub async fn get(&self) -> &T { 58 pub fn get(&self) -> impl Future<Output = &T> {
59 poll_fn(|cx| match self.try_get() { 59 poll_fn(|cx| match self.try_get() {
60 Some(data) => Poll::Ready(data), 60 Some(data) => Poll::Ready(data),
61 None => { 61 None => {
@@ -63,7 +63,6 @@ impl<T> OnceLock<T> {
63 Poll::Pending 63 Poll::Pending
64 } 64 }
65 }) 65 })
66 .await
67 } 66 }
68 67
69 /// Try to get a reference to the underlying value if it exists. 68 /// Try to get a reference to the underlying value if it exists.
diff --git a/embassy-sync/src/watch.rs b/embassy-sync/src/watch.rs
index 404e31714..e76646c0b 100644
--- a/embassy-sync/src/watch.rs
+++ b/embassy-sync/src/watch.rs
@@ -1,7 +1,7 @@
1//! A synchronization primitive for passing the latest value to **multiple** receivers. 1//! A synchronization primitive for passing the latest value to **multiple** receivers.
2 2
3use core::cell::RefCell; 3use core::cell::RefCell;
4use core::future::poll_fn; 4use core::future::{poll_fn, Future};
5use core::marker::PhantomData; 5use core::marker::PhantomData;
6use core::ops::{Deref, DerefMut}; 6use core::ops::{Deref, DerefMut};
7use core::task::{Context, Poll}; 7use core::task::{Context, Poll};
@@ -547,8 +547,8 @@ impl<'a, T: Clone, W: WatchBehavior<T> + ?Sized> Rcv<'a, T, W> {
547 /// Returns the current value of the `Watch` once it is initialized, marking it as seen. 547 /// Returns the current value of the `Watch` once it is initialized, marking it as seen.
548 /// 548 ///
549 /// **Note**: Futures do nothing unless you `.await` or poll them. 549 /// **Note**: Futures do nothing unless you `.await` or poll them.
550 pub async fn get(&mut self) -> T { 550 pub fn get(&mut self) -> impl Future<Output = T> + '_ {
551 poll_fn(|cx| self.watch.poll_get(&mut self.at_id, cx)).await 551 poll_fn(|cx| self.watch.poll_get(&mut self.at_id, cx))
552 } 552 }
553 553
554 /// Tries to get the current value of the `Watch` without waiting, marking it as seen. 554 /// Tries to get the current value of the `Watch` without waiting, marking it as seen.
diff --git a/embassy-sync/src/zerocopy_channel.rs b/embassy-sync/src/zerocopy_channel.rs
index fabb69bf6..56433cd8a 100644
--- a/embassy-sync/src/zerocopy_channel.rs
+++ b/embassy-sync/src/zerocopy_channel.rs
@@ -15,7 +15,7 @@
15//! another message will result in an error being returned. 15//! another message will result in an error being returned.
16 16
17use core::cell::RefCell; 17use core::cell::RefCell;
18use core::future::poll_fn; 18use core::future::{poll_fn, Future};
19use core::marker::PhantomData; 19use core::marker::PhantomData;
20use core::task::{Context, Poll}; 20use core::task::{Context, Poll};
21 21
@@ -131,12 +131,15 @@ impl<'a, M: RawMutex, T> Sender<'a, M, T> {
131 } 131 }
132 132
133 /// Asynchronously send a value over the channel. 133 /// Asynchronously send a value over the channel.
134 pub async fn send(&mut self) -> &mut T { 134 pub fn send(&mut self) -> impl Future<Output = &mut T> {
135 let i = poll_fn(|cx| { 135 poll_fn(|cx| {
136 self.channel.state.lock(|s| { 136 self.channel.state.lock(|s| {
137 let s = &mut *s.borrow_mut(); 137 let s = &mut *s.borrow_mut();
138 match s.push_index() { 138 match s.push_index() {
139 Some(i) => Poll::Ready(i), 139 Some(i) => {
140 let r = unsafe { &mut *self.channel.buf.add(i) };
141 Poll::Ready(r)
142 }
140 None => { 143 None => {
141 s.receive_waker.register(cx.waker()); 144 s.receive_waker.register(cx.waker());
142 Poll::Pending 145 Poll::Pending
@@ -144,8 +147,6 @@ impl<'a, M: RawMutex, T> Sender<'a, M, T> {
144 } 147 }
145 }) 148 })
146 }) 149 })
147 .await;
148 unsafe { &mut *self.channel.buf.add(i) }
149 } 150 }
150 151
151 /// Notify the channel that the sending of the value has been finalized. 152 /// Notify the channel that the sending of the value has been finalized.
@@ -213,12 +214,15 @@ impl<'a, M: RawMutex, T> Receiver<'a, M, T> {
213 } 214 }
214 215
215 /// Asynchronously receive a value over the channel. 216 /// Asynchronously receive a value over the channel.
216 pub async fn receive(&mut self) -> &mut T { 217 pub fn receive(&mut self) -> impl Future<Output = &mut T> {
217 let i = poll_fn(|cx| { 218 poll_fn(|cx| {
218 self.channel.state.lock(|s| { 219 self.channel.state.lock(|s| {
219 let s = &mut *s.borrow_mut(); 220 let s = &mut *s.borrow_mut();
220 match s.pop_index() { 221 match s.pop_index() {
221 Some(i) => Poll::Ready(i), 222 Some(i) => {
223 let r = unsafe { &mut *self.channel.buf.add(i) };
224 Poll::Ready(r)
225 }
222 None => { 226 None => {
223 s.send_waker.register(cx.waker()); 227 s.send_waker.register(cx.waker());
224 Poll::Pending 228 Poll::Pending
@@ -226,8 +230,6 @@ impl<'a, M: RawMutex, T> Receiver<'a, M, T> {
226 } 230 }
227 }) 231 })
228 }) 232 })
229 .await;
230 unsafe { &mut *self.channel.buf.add(i) }
231 } 233 }
232 234
233 /// Notify the channel that the receiving of the value has been finalized. 235 /// Notify the channel that the receiving of the value has been finalized.