diff options
| author | Dániel Buga <[email protected]> | 2024-12-30 12:13:13 +0100 |
|---|---|---|
| committer | Dániel Buga <[email protected]> | 2024-12-30 12:13:13 +0100 |
| commit | 44217aa0924e7590aa0afabdf17babd5c2ea5b82 (patch) | |
| tree | e42f5d02f9b560610b870d802cf390518180c3c6 | |
| parent | a4f8fddd696ca2e3705827ba4b3806cbadcb3134 (diff) | |
Desugar some async fns
| -rw-r--r-- | cyw43/src/ioctl.rs | 14 | ||||
| -rw-r--r-- | embassy-executor/src/spawner.rs | 8 | ||||
| -rw-r--r-- | embassy-net-esp-hosted/src/ioctl.rs | 8 | ||||
| -rw-r--r-- | embassy-net-nrf91/src/lib.rs | 5 | ||||
| -rw-r--r-- | embassy-net/src/raw.rs | 19 | ||||
| -rw-r--r-- | embassy-net/src/tcp.rs | 53 | ||||
| -rw-r--r-- | embassy-net/src/udp.rs | 22 | ||||
| -rw-r--r-- | embassy-nrf/src/buffered_uarte.rs | 11 | ||||
| -rwxr-xr-x | embassy-nrf/src/qspi.rs | 5 | ||||
| -rw-r--r-- | embassy-nrf/src/usb/mod.rs | 14 | ||||
| -rw-r--r-- | embassy-nrf/src/usb/vbus_detect.rs | 10 | ||||
| -rw-r--r-- | embassy-rp/src/adc.rs | 8 | ||||
| -rw-r--r-- | embassy-sync/src/mutex.rs | 5 | ||||
| -rw-r--r-- | embassy-sync/src/once_lock.rs | 5 | ||||
| -rw-r--r-- | embassy-sync/src/watch.rs | 6 | ||||
| -rw-r--r-- | embassy-sync/src/zerocopy_channel.rs | 24 | ||||
| -rw-r--r-- | embassy-usb/src/class/cdc_acm.rs | 5 | ||||
| -rw-r--r-- | embassy-usb/src/class/uac1/speaker.rs | 5 |
18 files changed, 103 insertions, 124 deletions
diff --git a/cyw43/src/ioctl.rs b/cyw43/src/ioctl.rs index f8b2d9aba..af8bb695b 100644 --- a/cyw43/src/ioctl.rs +++ b/cyw43/src/ioctl.rs | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | use core::cell::{Cell, RefCell}; | 1 | use core::cell::{Cell, RefCell}; |
| 2 | use core::future::poll_fn; | 2 | use core::future::{poll_fn, Future}; |
| 3 | use core::task::{Poll, Waker}; | 3 | use core::task::{Poll, Waker}; |
| 4 | 4 | ||
| 5 | use embassy_sync::waitqueue::WakerRegistration; | 5 | use embassy_sync::waitqueue::WakerRegistration; |
| @@ -71,7 +71,7 @@ impl IoctlState { | |||
| 71 | self.wakers.borrow_mut().runner.register(waker); | 71 | self.wakers.borrow_mut().runner.register(waker); |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | pub async fn wait_complete(&self) -> usize { | 74 | pub fn wait_complete(&self) -> impl Future<Output = usize> + '_ { |
| 75 | poll_fn(|cx| { | 75 | poll_fn(|cx| { |
| 76 | if let IoctlStateInner::Done { resp_len } = self.state.get() { | 76 | if let IoctlStateInner::Done { resp_len } = self.state.get() { |
| 77 | Poll::Ready(resp_len) | 77 | Poll::Ready(resp_len) |
| @@ -80,22 +80,18 @@ impl IoctlState { | |||
| 80 | Poll::Pending | 80 | Poll::Pending |
| 81 | } | 81 | } |
| 82 | }) | 82 | }) |
| 83 | .await | ||
| 84 | } | 83 | } |
| 85 | 84 | ||
| 86 | pub async fn wait_pending(&self) -> PendingIoctl { | 85 | pub fn wait_pending(&self) -> impl Future<Output = PendingIoctl> + '_ { |
| 87 | let pending = poll_fn(|cx| { | 86 | poll_fn(|cx| { |
| 88 | if let IoctlStateInner::Pending(pending) = self.state.get() { | 87 | if let IoctlStateInner::Pending(pending) = self.state.get() { |
| 88 | self.state.set(IoctlStateInner::Sent { buf: pending.buf }); | ||
| 89 | Poll::Ready(pending) | 89 | Poll::Ready(pending) |
| 90 | } else { | 90 | } else { |
| 91 | self.register_runner(cx.waker()); | 91 | self.register_runner(cx.waker()); |
| 92 | Poll::Pending | 92 | Poll::Pending |
| 93 | } | 93 | } |
| 94 | }) | 94 | }) |
| 95 | .await; | ||
| 96 | |||
| 97 | self.state.set(IoctlStateInner::Sent { buf: pending.buf }); | ||
| 98 | pending | ||
| 99 | } | 95 | } |
| 100 | 96 | ||
| 101 | pub fn cancel_ioctl(&self) { | 97 | pub fn cancel_ioctl(&self) { |
diff --git a/embassy-executor/src/spawner.rs b/embassy-executor/src/spawner.rs index 41320d4c3..ce24589bf 100644 --- a/embassy-executor/src/spawner.rs +++ b/embassy-executor/src/spawner.rs | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | use core::future::poll_fn; | 1 | use core::future::{poll_fn, Future}; |
| 2 | use core::marker::PhantomData; | 2 | use core::marker::PhantomData; |
| 3 | use core::mem; | 3 | use core::mem; |
| 4 | use core::sync::atomic::Ordering; | 4 | use core::sync::atomic::Ordering; |
| @@ -100,7 +100,7 @@ impl Spawner { | |||
| 100 | /// # Panics | 100 | /// # Panics |
| 101 | /// | 101 | /// |
| 102 | /// Panics if the current executor is not an Embassy executor. | 102 | /// Panics if the current executor is not an Embassy executor. |
| 103 | pub async fn for_current_executor() -> Self { | 103 | pub fn for_current_executor() -> impl Future<Output = Self> { |
| 104 | poll_fn(|cx| { | 104 | poll_fn(|cx| { |
| 105 | let task = raw::task_from_waker(cx.waker()); | 105 | let task = raw::task_from_waker(cx.waker()); |
| 106 | let executor = unsafe { | 106 | let executor = unsafe { |
| @@ -113,7 +113,6 @@ impl Spawner { | |||
| 113 | let executor = unsafe { raw::Executor::wrap(executor) }; | 113 | let executor = unsafe { raw::Executor::wrap(executor) }; |
| 114 | Poll::Ready(Self::new(executor)) | 114 | Poll::Ready(Self::new(executor)) |
| 115 | }) | 115 | }) |
| 116 | .await | ||
| 117 | } | 116 | } |
| 118 | 117 | ||
| 119 | /// Spawn a task into an executor. | 118 | /// Spawn a task into an executor. |
| @@ -178,7 +177,7 @@ impl SendSpawner { | |||
| 178 | /// # Panics | 177 | /// # Panics |
| 179 | /// | 178 | /// |
| 180 | /// Panics if the current executor is not an Embassy executor. | 179 | /// Panics if the current executor is not an Embassy executor. |
| 181 | pub async fn for_current_executor() -> Self { | 180 | pub fn for_current_executor() -> impl Future<Output = Self> { |
| 182 | poll_fn(|cx| { | 181 | poll_fn(|cx| { |
| 183 | let task = raw::task_from_waker(cx.waker()); | 182 | let task = raw::task_from_waker(cx.waker()); |
| 184 | let executor = unsafe { | 183 | let executor = unsafe { |
| @@ -190,7 +189,6 @@ impl SendSpawner { | |||
| 190 | }; | 189 | }; |
| 191 | Poll::Ready(Self::new(executor)) | 190 | Poll::Ready(Self::new(executor)) |
| 192 | }) | 191 | }) |
| 193 | .await | ||
| 194 | } | 192 | } |
| 195 | 193 | ||
| 196 | /// Spawn a task into an executor. | 194 | /// Spawn a task into an executor. |
diff --git a/embassy-net-esp-hosted/src/ioctl.rs b/embassy-net-esp-hosted/src/ioctl.rs index e2a6815aa..512023206 100644 --- a/embassy-net-esp-hosted/src/ioctl.rs +++ b/embassy-net-esp-hosted/src/ioctl.rs | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | use core::cell::RefCell; | 1 | use core::cell::RefCell; |
| 2 | use core::future::poll_fn; | 2 | use core::future::{poll_fn, Future}; |
| 3 | use core::task::Poll; | 3 | use core::task::Poll; |
| 4 | 4 | ||
| 5 | use embassy_sync::waitqueue::WakerRegistration; | 5 | use embassy_sync::waitqueue::WakerRegistration; |
| @@ -38,7 +38,7 @@ impl Shared { | |||
| 38 | })) | 38 | })) |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | pub async fn ioctl_wait_complete(&self) -> usize { | 41 | pub fn ioctl_wait_complete(&self) -> impl Future<Output = usize> + '_ { |
| 42 | poll_fn(|cx| { | 42 | poll_fn(|cx| { |
| 43 | let mut this = self.0.borrow_mut(); | 43 | let mut this = self.0.borrow_mut(); |
| 44 | if let IoctlState::Done { resp_len } = this.ioctl { | 44 | if let IoctlState::Done { resp_len } = this.ioctl { |
| @@ -48,7 +48,6 @@ impl Shared { | |||
| 48 | Poll::Pending | 48 | Poll::Pending |
| 49 | } | 49 | } |
| 50 | }) | 50 | }) |
| 51 | .await | ||
| 52 | } | 51 | } |
| 53 | 52 | ||
| 54 | pub async fn ioctl_wait_pending(&self) -> PendingIoctl { | 53 | pub async fn ioctl_wait_pending(&self) -> PendingIoctl { |
| @@ -108,7 +107,7 @@ impl Shared { | |||
| 108 | this.control_waker.wake(); | 107 | this.control_waker.wake(); |
| 109 | } | 108 | } |
| 110 | 109 | ||
| 111 | pub async fn init_wait(&self) { | 110 | pub fn init_wait(&self) -> impl Future<Output = ()> + '_ { |
| 112 | poll_fn(|cx| { | 111 | poll_fn(|cx| { |
| 113 | let mut this = self.0.borrow_mut(); | 112 | let mut this = self.0.borrow_mut(); |
| 114 | if this.is_init { | 113 | if this.is_init { |
| @@ -118,6 +117,5 @@ impl Shared { | |||
| 118 | Poll::Pending | 117 | Poll::Pending |
| 119 | } | 118 | } |
| 120 | }) | 119 | }) |
| 121 | .await | ||
| 122 | } | 120 | } |
| 123 | } | 121 | } |
diff --git a/embassy-net-nrf91/src/lib.rs b/embassy-net-nrf91/src/lib.rs index 3abe2c766..61fcaea1f 100644 --- a/embassy-net-nrf91/src/lib.rs +++ b/embassy-net-nrf91/src/lib.rs | |||
| @@ -9,7 +9,7 @@ mod fmt; | |||
| 9 | pub mod context; | 9 | pub mod context; |
| 10 | 10 | ||
| 11 | use core::cell::RefCell; | 11 | use core::cell::RefCell; |
| 12 | use core::future::poll_fn; | 12 | use core::future::{poll_fn, Future}; |
| 13 | use core::marker::PhantomData; | 13 | use core::marker::PhantomData; |
| 14 | use core::mem::{self, MaybeUninit}; | 14 | use core::mem::{self, MaybeUninit}; |
| 15 | use core::ptr::{self, addr_of, addr_of_mut, copy_nonoverlapping}; | 15 | use core::ptr::{self, addr_of, addr_of_mut, copy_nonoverlapping}; |
| @@ -737,7 +737,7 @@ pub struct Control<'a> { | |||
| 737 | 737 | ||
| 738 | impl<'a> Control<'a> { | 738 | impl<'a> Control<'a> { |
| 739 | /// Wait for modem IPC to be initialized. | 739 | /// Wait for modem IPC to be initialized. |
| 740 | pub async fn wait_init(&self) { | 740 | pub fn wait_init(&self) -> impl Future<Output = ()> + '_ { |
| 741 | poll_fn(|cx| { | 741 | poll_fn(|cx| { |
| 742 | let mut state = self.state.borrow_mut(); | 742 | let mut state = self.state.borrow_mut(); |
| 743 | if state.init { | 743 | if state.init { |
| @@ -746,7 +746,6 @@ impl<'a> Control<'a> { | |||
| 746 | state.init_waker.register(cx.waker()); | 746 | state.init_waker.register(cx.waker()); |
| 747 | Poll::Pending | 747 | Poll::Pending |
| 748 | }) | 748 | }) |
| 749 | .await | ||
| 750 | } | 749 | } |
| 751 | 750 | ||
| 752 | async fn request(&self, msg: &mut Message, req_data: &[u8], resp_data: &mut [u8]) -> usize { | 751 | async fn request(&self, msg: &mut Message, req_data: &[u8], resp_data: &mut [u8]) -> usize { |
diff --git a/embassy-net/src/raw.rs b/embassy-net/src/raw.rs index a88bcc458..c9f753f13 100644 --- a/embassy-net/src/raw.rs +++ b/embassy-net/src/raw.rs | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | //! Raw sockets. | 1 | //! Raw sockets. |
| 2 | 2 | ||
| 3 | use core::future::poll_fn; | 3 | use core::future::{poll_fn, Future}; |
| 4 | use core::mem; | 4 | use core::mem; |
| 5 | use core::task::{Context, Poll}; | 5 | use core::task::{Context, Poll}; |
| 6 | 6 | ||
| @@ -66,8 +66,8 @@ impl<'a> RawSocket<'a> { | |||
| 66 | /// | 66 | /// |
| 67 | /// A socket is readable when a packet has been received, or when there are queued packets in | 67 | /// A socket is readable when a packet has been received, or when there are queued packets in |
| 68 | /// the buffer. | 68 | /// the buffer. |
| 69 | pub async fn wait_recv_ready(&self) { | 69 | pub fn wait_recv_ready(&self) -> impl Future<Output = ()> + '_ { |
| 70 | poll_fn(move |cx| self.poll_recv_ready(cx)).await | 70 | poll_fn(move |cx| self.poll_recv_ready(cx)) |
| 71 | } | 71 | } |
| 72 | 72 | ||
| 73 | /// Receive a datagram. | 73 | /// Receive a datagram. |
| @@ -115,8 +115,8 @@ impl<'a> RawSocket<'a> { | |||
| 115 | /// | 115 | /// |
| 116 | /// A socket becomes writable when there is space in the buffer, from initial memory or after | 116 | /// A socket becomes writable when there is space in the buffer, from initial memory or after |
| 117 | /// dispatching datagrams on a full buffer. | 117 | /// dispatching datagrams on a full buffer. |
| 118 | pub async fn wait_send_ready(&self) { | 118 | pub fn wait_send_ready(&self) -> impl Future<Output = ()> + '_ { |
| 119 | poll_fn(move |cx| self.poll_send_ready(cx)).await | 119 | poll_fn(move |cx| self.poll_send_ready(cx)) |
| 120 | } | 120 | } |
| 121 | 121 | ||
| 122 | /// Wait until a datagram can be sent. | 122 | /// Wait until a datagram can be sent. |
| @@ -141,8 +141,8 @@ impl<'a> RawSocket<'a> { | |||
| 141 | /// Send a datagram. | 141 | /// Send a datagram. |
| 142 | /// | 142 | /// |
| 143 | /// This method will wait until the datagram has been sent.` | 143 | /// This method will wait until the datagram has been sent.` |
| 144 | pub async fn send(&self, buf: &[u8]) { | 144 | pub fn send<'s>(&'s self, buf: &'s [u8]) -> impl Future<Output = ()> + 's { |
| 145 | poll_fn(move |cx| self.poll_send(buf, cx)).await | 145 | poll_fn(|cx| self.poll_send(buf, cx)) |
| 146 | } | 146 | } |
| 147 | 147 | ||
| 148 | /// Send a datagram. | 148 | /// Send a datagram. |
| @@ -165,8 +165,8 @@ impl<'a> RawSocket<'a> { | |||
| 165 | /// Flush the socket. | 165 | /// Flush the socket. |
| 166 | /// | 166 | /// |
| 167 | /// This method will wait until the socket is flushed. | 167 | /// This method will wait until the socket is flushed. |
| 168 | pub async fn flush(&mut self) { | 168 | pub fn flush(&mut self) -> impl Future<Output = ()> + '_ { |
| 169 | poll_fn(move |cx| { | 169 | poll_fn(|cx| { |
| 170 | self.with_mut(|s, _| { | 170 | self.with_mut(|s, _| { |
| 171 | if s.send_queue() == 0 { | 171 | if s.send_queue() == 0 { |
| 172 | Poll::Ready(()) | 172 | Poll::Ready(()) |
| @@ -176,7 +176,6 @@ impl<'a> RawSocket<'a> { | |||
| 176 | } | 176 | } |
| 177 | }) | 177 | }) |
| 178 | }) | 178 | }) |
| 179 | .await | ||
| 180 | } | 179 | } |
| 181 | } | 180 | } |
| 182 | 181 | ||
diff --git a/embassy-net/src/tcp.rs b/embassy-net/src/tcp.rs index 32d374064..d0230b581 100644 --- a/embassy-net/src/tcp.rs +++ b/embassy-net/src/tcp.rs | |||
| @@ -8,7 +8,7 @@ | |||
| 8 | //! Incoming connections when no socket is listening are rejected. To accept many incoming | 8 | //! Incoming connections when no socket is listening are rejected. To accept many incoming |
| 9 | //! connections, create many sockets and put them all into listening mode. | 9 | //! connections, create many sockets and put them all into listening mode. |
| 10 | 10 | ||
| 11 | use core::future::poll_fn; | 11 | use core::future::{poll_fn, Future}; |
| 12 | use core::mem; | 12 | use core::mem; |
| 13 | use core::task::{Context, Poll}; | 13 | use core::task::{Context, Poll}; |
| 14 | 14 | ||
| @@ -79,8 +79,8 @@ impl<'a> TcpReader<'a> { | |||
| 79 | /// (see [`may_recv()`](TcpSocket::may_recv)), and there is some pending data in the receive buffer. | 79 | /// (see [`may_recv()`](TcpSocket::may_recv)), and there is some pending data in the receive buffer. |
| 80 | /// | 80 | /// |
| 81 | /// This is the equivalent of [read](#method.read), without buffering any data. | 81 | /// This is the equivalent of [read](#method.read), without buffering any data. |
| 82 | pub async fn wait_read_ready(&self) { | 82 | pub fn wait_read_ready(&self) -> impl Future<Output = ()> + '_ { |
| 83 | poll_fn(move |cx| self.io.poll_read_ready(cx)).await | 83 | poll_fn(move |cx| self.io.poll_read_ready(cx)) |
| 84 | } | 84 | } |
| 85 | 85 | ||
| 86 | /// Read data from the socket. | 86 | /// Read data from the socket. |
| @@ -131,24 +131,24 @@ impl<'a> TcpWriter<'a> { | |||
| 131 | /// (see [`may_send()`](TcpSocket::may_send)), and the transmit buffer is not full. | 131 | /// (see [`may_send()`](TcpSocket::may_send)), and the transmit buffer is not full. |
| 132 | /// | 132 | /// |
| 133 | /// This is the equivalent of [write](#method.write), without sending any data. | 133 | /// This is the equivalent of [write](#method.write), without sending any data. |
| 134 | pub async fn wait_write_ready(&self) { | 134 | pub fn wait_write_ready(&self) -> impl Future<Output = ()> + '_ { |
| 135 | poll_fn(move |cx| self.io.poll_write_ready(cx)).await | 135 | poll_fn(move |cx| self.io.poll_write_ready(cx)) |
| 136 | } | 136 | } |
| 137 | 137 | ||
| 138 | /// Write data to the socket. | 138 | /// Write data to the socket. |
| 139 | /// | 139 | /// |
| 140 | /// Returns how many bytes were written, or an error. If the socket is not ready to | 140 | /// Returns how many bytes were written, or an error. If the socket is not ready to |
| 141 | /// accept data, it waits until it is. | 141 | /// accept data, it waits until it is. |
| 142 | pub async fn write(&mut self, buf: &[u8]) -> Result<usize, Error> { | 142 | pub fn write<'s>(&'s mut self, buf: &'s [u8]) -> impl Future<Output = Result<usize, Error>> + 's { |
| 143 | self.io.write(buf).await | 143 | self.io.write(buf) |
| 144 | } | 144 | } |
| 145 | 145 | ||
| 146 | /// Flushes the written data to the socket. | 146 | /// Flushes the written data to the socket. |
| 147 | /// | 147 | /// |
| 148 | /// This waits until all data has been sent, and ACKed by the remote host. For a connection | 148 | /// This waits until all data has been sent, and ACKed by the remote host. For a connection |
| 149 | /// closed with [`abort()`](TcpSocket::abort) it will wait for the TCP RST packet to be sent. | 149 | /// closed with [`abort()`](TcpSocket::abort) it will wait for the TCP RST packet to be sent. |
| 150 | pub async fn flush(&mut self) -> Result<(), Error> { | 150 | pub fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + '_ { |
| 151 | self.io.flush().await | 151 | self.io.flush() |
| 152 | } | 152 | } |
| 153 | 153 | ||
| 154 | /// Call `f` with the largest contiguous slice of octets in the transmit buffer, | 154 | /// Call `f` with the largest contiguous slice of octets in the transmit buffer, |
| @@ -300,8 +300,8 @@ impl<'a> TcpSocket<'a> { | |||
| 300 | /// (see [may_recv](#method.may_recv)), and there is some pending data in the receive buffer. | 300 | /// (see [may_recv](#method.may_recv)), and there is some pending data in the receive buffer. |
| 301 | /// | 301 | /// |
| 302 | /// This is the equivalent of [read](#method.read), without buffering any data. | 302 | /// This is the equivalent of [read](#method.read), without buffering any data. |
| 303 | pub async fn wait_read_ready(&self) { | 303 | pub fn wait_read_ready(&self) -> impl Future<Output = ()> + '_ { |
| 304 | poll_fn(move |cx| self.io.poll_read_ready(cx)).await | 304 | poll_fn(move |cx| self.io.poll_read_ready(cx)) |
| 305 | } | 305 | } |
| 306 | 306 | ||
| 307 | /// Read data from the socket. | 307 | /// Read data from the socket. |
| @@ -311,8 +311,8 @@ impl<'a> TcpSocket<'a> { | |||
| 311 | /// | 311 | /// |
| 312 | /// A return value of Ok(0) means that the socket was closed and is longer | 312 | /// A return value of Ok(0) means that the socket was closed and is longer |
| 313 | /// able to receive any data. | 313 | /// able to receive any data. |
| 314 | pub async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { | 314 | pub fn read<'s>(&'s mut self, buf: &'s mut [u8]) -> impl Future<Output = Result<usize, Error>> + 's { |
| 315 | self.io.read(buf).await | 315 | self.io.read(buf) |
| 316 | } | 316 | } |
| 317 | 317 | ||
| 318 | /// Wait until the socket becomes writable. | 318 | /// Wait until the socket becomes writable. |
| @@ -321,24 +321,24 @@ impl<'a> TcpSocket<'a> { | |||
| 321 | /// (see [may_send](#method.may_send)), and the transmit buffer is not full. | 321 | /// (see [may_send](#method.may_send)), and the transmit buffer is not full. |
| 322 | /// | 322 | /// |
| 323 | /// This is the equivalent of [write](#method.write), without sending any data. | 323 | /// This is the equivalent of [write](#method.write), without sending any data. |
| 324 | pub async fn wait_write_ready(&self) { | 324 | pub fn wait_write_ready(&self) -> impl Future<Output = ()> + '_ { |
| 325 | poll_fn(move |cx| self.io.poll_write_ready(cx)).await | 325 | poll_fn(move |cx| self.io.poll_write_ready(cx)) |
| 326 | } | 326 | } |
| 327 | 327 | ||
| 328 | /// Write data to the socket. | 328 | /// Write data to the socket. |
| 329 | /// | 329 | /// |
| 330 | /// Returns how many bytes were written, or an error. If the socket is not ready to | 330 | /// Returns how many bytes were written, or an error. If the socket is not ready to |
| 331 | /// accept data, it waits until it is. | 331 | /// accept data, it waits until it is. |
| 332 | pub async fn write(&mut self, buf: &[u8]) -> Result<usize, Error> { | 332 | pub fn write<'s>(&'s mut self, buf: &'s [u8]) -> impl Future<Output = Result<usize, Error>> + 's { |
| 333 | self.io.write(buf).await | 333 | self.io.write(buf) |
| 334 | } | 334 | } |
| 335 | 335 | ||
| 336 | /// Flushes the written data to the socket. | 336 | /// Flushes the written data to the socket. |
| 337 | /// | 337 | /// |
| 338 | /// This waits until all data has been sent, and ACKed by the remote host. For a connection | 338 | /// This waits until all data has been sent, and ACKed by the remote host. For a connection |
| 339 | /// closed with [`abort()`](TcpSocket::abort) it will wait for the TCP RST packet to be sent. | 339 | /// closed with [`abort()`](TcpSocket::abort) it will wait for the TCP RST packet to be sent. |
| 340 | pub async fn flush(&mut self) -> Result<(), Error> { | 340 | pub fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + '_ { |
| 341 | self.io.flush().await | 341 | self.io.flush() |
| 342 | } | 342 | } |
| 343 | 343 | ||
| 344 | /// Set the timeout for the socket. | 344 | /// Set the timeout for the socket. |
| @@ -501,8 +501,8 @@ impl<'d> TcpIo<'d> { | |||
| 501 | }) | 501 | }) |
| 502 | } | 502 | } |
| 503 | 503 | ||
| 504 | async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { | 504 | fn read<'s>(&'s mut self, buf: &'s mut [u8]) -> impl Future<Output = Result<usize, Error>> + 's { |
| 505 | poll_fn(move |cx| { | 505 | poll_fn(|cx| { |
| 506 | // CAUTION: smoltcp semantics around EOF are different to what you'd expect | 506 | // CAUTION: smoltcp semantics around EOF are different to what you'd expect |
| 507 | // from posix-like IO, so we have to tweak things here. | 507 | // from posix-like IO, so we have to tweak things here. |
| 508 | self.with_mut(|s, _| match s.recv_slice(buf) { | 508 | self.with_mut(|s, _| match s.recv_slice(buf) { |
| @@ -526,7 +526,6 @@ impl<'d> TcpIo<'d> { | |||
| 526 | Err(tcp::RecvError::InvalidState) => Poll::Ready(Err(Error::ConnectionReset)), | 526 | Err(tcp::RecvError::InvalidState) => Poll::Ready(Err(Error::ConnectionReset)), |
| 527 | }) | 527 | }) |
| 528 | }) | 528 | }) |
| 529 | .await | ||
| 530 | } | 529 | } |
| 531 | 530 | ||
| 532 | fn poll_write_ready(&self, cx: &mut Context<'_>) -> Poll<()> { | 531 | fn poll_write_ready(&self, cx: &mut Context<'_>) -> Poll<()> { |
| @@ -540,8 +539,8 @@ impl<'d> TcpIo<'d> { | |||
| 540 | }) | 539 | }) |
| 541 | } | 540 | } |
| 542 | 541 | ||
| 543 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Error> { | 542 | fn write<'s>(&'s mut self, buf: &'s [u8]) -> impl Future<Output = Result<usize, Error>> + 's { |
| 544 | poll_fn(move |cx| { | 543 | poll_fn(|cx| { |
| 545 | self.with_mut(|s, _| match s.send_slice(buf) { | 544 | self.with_mut(|s, _| match s.send_slice(buf) { |
| 546 | // Not ready to send (no space in the tx buffer) | 545 | // Not ready to send (no space in the tx buffer) |
| 547 | Ok(0) => { | 546 | Ok(0) => { |
| @@ -554,7 +553,6 @@ impl<'d> TcpIo<'d> { | |||
| 554 | Err(tcp::SendError::InvalidState) => Poll::Ready(Err(Error::ConnectionReset)), | 553 | Err(tcp::SendError::InvalidState) => Poll::Ready(Err(Error::ConnectionReset)), |
| 555 | }) | 554 | }) |
| 556 | }) | 555 | }) |
| 557 | .await | ||
| 558 | } | 556 | } |
| 559 | 557 | ||
| 560 | async fn write_with<F, R>(&mut self, f: F) -> Result<R, Error> | 558 | async fn write_with<F, R>(&mut self, f: F) -> Result<R, Error> |
| @@ -615,8 +613,8 @@ impl<'d> TcpIo<'d> { | |||
| 615 | .await | 613 | .await |
| 616 | } | 614 | } |
| 617 | 615 | ||
| 618 | async fn flush(&mut self) -> Result<(), Error> { | 616 | fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + '_ { |
| 619 | poll_fn(move |cx| { | 617 | poll_fn(|cx| { |
| 620 | self.with_mut(|s, _| { | 618 | self.with_mut(|s, _| { |
| 621 | let data_pending = (s.send_queue() > 0) && s.state() != tcp::State::Closed; | 619 | let data_pending = (s.send_queue() > 0) && s.state() != tcp::State::Closed; |
| 622 | let fin_pending = matches!( | 620 | let fin_pending = matches!( |
| @@ -636,7 +634,6 @@ impl<'d> TcpIo<'d> { | |||
| 636 | } | 634 | } |
| 637 | }) | 635 | }) |
| 638 | }) | 636 | }) |
| 639 | .await | ||
| 640 | } | 637 | } |
| 641 | 638 | ||
| 642 | fn recv_capacity(&self) -> usize { | 639 | fn recv_capacity(&self) -> usize { |
diff --git a/embassy-net/src/udp.rs b/embassy-net/src/udp.rs index 76602edc2..64a22d45b 100644 --- a/embassy-net/src/udp.rs +++ b/embassy-net/src/udp.rs | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | //! UDP sockets. | 1 | //! UDP sockets. |
| 2 | 2 | ||
| 3 | use core::future::poll_fn; | 3 | use core::future::{poll_fn, Future}; |
| 4 | use core::mem; | 4 | use core::mem; |
| 5 | use core::task::{Context, Poll}; | 5 | use core::task::{Context, Poll}; |
| 6 | 6 | ||
| @@ -107,8 +107,8 @@ impl<'a> UdpSocket<'a> { | |||
| 107 | /// | 107 | /// |
| 108 | /// A socket is readable when a packet has been received, or when there are queued packets in | 108 | /// A socket is readable when a packet has been received, or when there are queued packets in |
| 109 | /// the buffer. | 109 | /// the buffer. |
| 110 | pub async fn wait_recv_ready(&self) { | 110 | pub fn wait_recv_ready(&self) -> impl Future<Output = ()> + '_ { |
| 111 | poll_fn(move |cx| self.poll_recv_ready(cx)).await | 111 | poll_fn(move |cx| self.poll_recv_ready(cx)) |
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | /// Wait until a datagram can be read. | 114 | /// Wait until a datagram can be read. |
| @@ -134,8 +134,11 @@ impl<'a> UdpSocket<'a> { | |||
| 134 | /// This method will wait until a datagram is received. | 134 | /// This method will wait until a datagram is received. |
| 135 | /// | 135 | /// |
| 136 | /// Returns the number of bytes received and the remote endpoint. | 136 | /// Returns the number of bytes received and the remote endpoint. |
| 137 | pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, UdpMetadata), RecvError> { | 137 | pub fn recv_from<'s>( |
| 138 | poll_fn(move |cx| self.poll_recv_from(buf, cx)).await | 138 | &'s self, |
| 139 | buf: &'s mut [u8], | ||
| 140 | ) -> impl Future<Output = Result<(usize, UdpMetadata), RecvError>> + 's { | ||
| 141 | poll_fn(|cx| self.poll_recv_from(buf, cx)) | ||
| 139 | } | 142 | } |
| 140 | 143 | ||
| 141 | /// Receive a datagram. | 144 | /// Receive a datagram. |
| @@ -194,8 +197,8 @@ impl<'a> UdpSocket<'a> { | |||
| 194 | /// | 197 | /// |
| 195 | /// A socket becomes writable when there is space in the buffer, from initial memory or after | 198 | /// A socket becomes writable when there is space in the buffer, from initial memory or after |
| 196 | /// dispatching datagrams on a full buffer. | 199 | /// dispatching datagrams on a full buffer. |
| 197 | pub async fn wait_send_ready(&self) { | 200 | pub fn wait_send_ready(&self) -> impl Future<Output = ()> + '_ { |
| 198 | poll_fn(move |cx| self.poll_send_ready(cx)).await | 201 | poll_fn(|cx| self.poll_send_ready(cx)) |
| 199 | } | 202 | } |
| 200 | 203 | ||
| 201 | /// Wait until a datagram can be sent. | 204 | /// Wait until a datagram can be sent. |
| @@ -297,8 +300,8 @@ impl<'a> UdpSocket<'a> { | |||
| 297 | /// Flush the socket. | 300 | /// Flush the socket. |
| 298 | /// | 301 | /// |
| 299 | /// This method will wait until the socket is flushed. | 302 | /// This method will wait until the socket is flushed. |
| 300 | pub async fn flush(&mut self) { | 303 | pub fn flush(&mut self) -> impl Future<Output = ()> + '_ { |
| 301 | poll_fn(move |cx| { | 304 | poll_fn(|cx| { |
| 302 | self.with_mut(|s, _| { | 305 | self.with_mut(|s, _| { |
| 303 | if s.send_queue() == 0 { | 306 | if s.send_queue() == 0 { |
| 304 | Poll::Ready(()) | 307 | Poll::Ready(()) |
| @@ -308,7 +311,6 @@ impl<'a> UdpSocket<'a> { | |||
| 308 | } | 311 | } |
| 309 | }) | 312 | }) |
| 310 | }) | 313 | }) |
| 311 | .await | ||
| 312 | } | 314 | } |
| 313 | 315 | ||
| 314 | /// Returns the local endpoint of the socket. | 316 | /// Returns the local endpoint of the socket. |
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index b55e70a36..c3fcfd06e 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs | |||
| @@ -9,7 +9,7 @@ | |||
| 9 | //! Please also see [crate::uarte] to understand when [BufferedUarte] should be used. | 9 | //! Please also see [crate::uarte] to understand when [BufferedUarte] should be used. |
| 10 | 10 | ||
| 11 | use core::cmp::min; | 11 | use core::cmp::min; |
| 12 | use core::future::poll_fn; | 12 | use core::future::{poll_fn, Future}; |
| 13 | use core::marker::PhantomData; | 13 | use core::marker::PhantomData; |
| 14 | use core::slice; | 14 | use core::slice; |
| 15 | use core::sync::atomic::{compiler_fence, AtomicBool, AtomicU8, AtomicUsize, Ordering}; | 15 | use core::sync::atomic::{compiler_fence, AtomicBool, AtomicU8, AtomicUsize, Ordering}; |
| @@ -452,7 +452,7 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> { | |||
| 452 | } | 452 | } |
| 453 | 453 | ||
| 454 | /// Write a buffer into this writer, returning how many bytes were written. | 454 | /// Write a buffer into this writer, returning how many bytes were written. |
| 455 | pub async fn write(&mut self, buf: &[u8]) -> Result<usize, Error> { | 455 | pub fn write<'a>(&'a mut self, buf: &'a [u8]) -> impl Future<Output = Result<usize, Error>> + 'a { |
| 456 | poll_fn(move |cx| { | 456 | poll_fn(move |cx| { |
| 457 | //trace!("poll_write: {:?}", buf.len()); | 457 | //trace!("poll_write: {:?}", buf.len()); |
| 458 | let ss = U::state(); | 458 | let ss = U::state(); |
| @@ -477,7 +477,6 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> { | |||
| 477 | 477 | ||
| 478 | Poll::Ready(Ok(n)) | 478 | Poll::Ready(Ok(n)) |
| 479 | }) | 479 | }) |
| 480 | .await | ||
| 481 | } | 480 | } |
| 482 | 481 | ||
| 483 | /// Try writing a buffer without waiting, returning how many bytes were written. | 482 | /// Try writing a buffer without waiting, returning how many bytes were written. |
| @@ -504,7 +503,7 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> { | |||
| 504 | } | 503 | } |
| 505 | 504 | ||
| 506 | /// Flush this output stream, ensuring that all intermediately buffered contents reach their destination. | 505 | /// Flush this output stream, ensuring that all intermediately buffered contents reach their destination. |
| 507 | pub async fn flush(&mut self) -> Result<(), Error> { | 506 | pub fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + '_ { |
| 508 | poll_fn(move |cx| { | 507 | poll_fn(move |cx| { |
| 509 | //trace!("poll_flush"); | 508 | //trace!("poll_flush"); |
| 510 | let ss = U::state(); | 509 | let ss = U::state(); |
| @@ -517,7 +516,6 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> { | |||
| 517 | 516 | ||
| 518 | Poll::Ready(Ok(())) | 517 | Poll::Ready(Ok(())) |
| 519 | }) | 518 | }) |
| 520 | .await | ||
| 521 | } | 519 | } |
| 522 | } | 520 | } |
| 523 | 521 | ||
| @@ -721,7 +719,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarteRx<'d, U, T> { | |||
| 721 | } | 719 | } |
| 722 | 720 | ||
| 723 | /// Return the contents of the internal buffer, filling it with more data from the inner reader if it is empty. | 721 | /// Return the contents of the internal buffer, filling it with more data from the inner reader if it is empty. |
| 724 | pub async fn fill_buf(&mut self) -> Result<&[u8], Error> { | 722 | pub fn fill_buf(&mut self) -> impl Future<Output = Result<&'_ [u8], Error>> { |
| 725 | poll_fn(move |cx| { | 723 | poll_fn(move |cx| { |
| 726 | compiler_fence(Ordering::SeqCst); | 724 | compiler_fence(Ordering::SeqCst); |
| 727 | //trace!("poll_read"); | 725 | //trace!("poll_read"); |
| @@ -771,7 +769,6 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarteRx<'d, U, T> { | |||
| 771 | let buf = s.rx_buf.buf.load(Ordering::Relaxed); | 769 | let buf = s.rx_buf.buf.load(Ordering::Relaxed); |
| 772 | Poll::Ready(Ok(unsafe { slice::from_raw_parts(buf.add(start), n) })) | 770 | Poll::Ready(Ok(unsafe { slice::from_raw_parts(buf.add(start), n) })) |
| 773 | }) | 771 | }) |
| 774 | .await | ||
| 775 | } | 772 | } |
| 776 | 773 | ||
| 777 | /// Tell this buffer that `amt` bytes have been consumed from the buffer, so they should no longer be returned in calls to `fill_buf`. | 774 | /// Tell this buffer that `amt` bytes have been consumed from the buffer, so they should no longer be returned in calls to `fill_buf`. |
diff --git a/embassy-nrf/src/qspi.rs b/embassy-nrf/src/qspi.rs index 255b43c33..17e127700 100755 --- a/embassy-nrf/src/qspi.rs +++ b/embassy-nrf/src/qspi.rs | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | #![macro_use] | 3 | #![macro_use] |
| 4 | 4 | ||
| 5 | use core::future::poll_fn; | 5 | use core::future::{poll_fn, Future}; |
| 6 | use core::marker::PhantomData; | 6 | use core::marker::PhantomData; |
| 7 | use core::ptr; | 7 | use core::ptr; |
| 8 | use core::task::Poll; | 8 | use core::task::Poll; |
| @@ -314,7 +314,7 @@ impl<'d, T: Instance> Qspi<'d, T> { | |||
| 314 | Ok(()) | 314 | Ok(()) |
| 315 | } | 315 | } |
| 316 | 316 | ||
| 317 | async fn wait_ready(&mut self) { | 317 | fn wait_ready(&mut self) -> impl Future<Output = ()> { |
| 318 | poll_fn(move |cx| { | 318 | poll_fn(move |cx| { |
| 319 | let r = T::regs(); | 319 | let r = T::regs(); |
| 320 | let s = T::state(); | 320 | let s = T::state(); |
| @@ -324,7 +324,6 @@ impl<'d, T: Instance> Qspi<'d, T> { | |||
| 324 | } | 324 | } |
| 325 | Poll::Pending | 325 | Poll::Pending |
| 326 | }) | 326 | }) |
| 327 | .await | ||
| 328 | } | 327 | } |
| 329 | 328 | ||
| 330 | fn blocking_wait_ready() { | 329 | fn blocking_wait_ready() { |
diff --git a/embassy-nrf/src/usb/mod.rs b/embassy-nrf/src/usb/mod.rs index a9bf16708..06dae694b 100644 --- a/embassy-nrf/src/usb/mod.rs +++ b/embassy-nrf/src/usb/mod.rs | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | pub mod vbus_detect; | 5 | pub mod vbus_detect; |
| 6 | 6 | ||
| 7 | use core::future::poll_fn; | 7 | use core::future::{poll_fn, Future}; |
| 8 | use core::marker::PhantomData; | 8 | use core::marker::PhantomData; |
| 9 | use core::mem::MaybeUninit; | 9 | use core::mem::MaybeUninit; |
| 10 | use core::sync::atomic::{compiler_fence, AtomicU32, Ordering}; | 10 | use core::sync::atomic::{compiler_fence, AtomicU32, Ordering}; |
| @@ -219,8 +219,8 @@ impl<'d, T: Instance, V: VbusDetect> driver::Bus for Bus<'d, T, V> { | |||
| 219 | regs.enable().write(|x| x.set_enable(false)); | 219 | regs.enable().write(|x| x.set_enable(false)); |
| 220 | } | 220 | } |
| 221 | 221 | ||
| 222 | async fn poll(&mut self) -> Event { | 222 | fn poll(&mut self) -> impl Future<Output = Event> { |
| 223 | poll_fn(move |cx| { | 223 | poll_fn(|cx| { |
| 224 | BUS_WAKER.register(cx.waker()); | 224 | BUS_WAKER.register(cx.waker()); |
| 225 | let regs = T::regs(); | 225 | let regs = T::regs(); |
| 226 | 226 | ||
| @@ -277,7 +277,6 @@ impl<'d, T: Instance, V: VbusDetect> driver::Bus for Bus<'d, T, V> { | |||
| 277 | 277 | ||
| 278 | Poll::Pending | 278 | Poll::Pending |
| 279 | }) | 279 | }) |
| 280 | .await | ||
| 281 | } | 280 | } |
| 282 | 281 | ||
| 283 | fn endpoint_set_stalled(&mut self, ep_addr: EndpointAddress, stalled: bool) { | 282 | fn endpoint_set_stalled(&mut self, ep_addr: EndpointAddress, stalled: bool) { |
| @@ -468,7 +467,7 @@ impl<'d, T: Instance, Dir: EndpointDir> driver::Endpoint for Endpoint<'d, T, Dir | |||
| 468 | 467 | ||
| 469 | #[allow(private_bounds)] | 468 | #[allow(private_bounds)] |
| 470 | impl<'d, T: Instance, Dir: EndpointDir> Endpoint<'d, T, Dir> { | 469 | impl<'d, T: Instance, Dir: EndpointDir> Endpoint<'d, T, Dir> { |
| 471 | async fn wait_enabled_state(&mut self, state: bool) { | 470 | fn wait_enabled_state(&mut self, state: bool) -> impl Future<Output = ()> { |
| 472 | let i = self.info.addr.index(); | 471 | let i = self.info.addr.index(); |
| 473 | assert!(i != 0); | 472 | assert!(i != 0); |
| 474 | 473 | ||
| @@ -480,12 +479,11 @@ impl<'d, T: Instance, Dir: EndpointDir> Endpoint<'d, T, Dir> { | |||
| 480 | Poll::Pending | 479 | Poll::Pending |
| 481 | } | 480 | } |
| 482 | }) | 481 | }) |
| 483 | .await | ||
| 484 | } | 482 | } |
| 485 | 483 | ||
| 486 | /// Wait for the endpoint to be disabled | 484 | /// Wait for the endpoint to be disabled |
| 487 | pub async fn wait_disabled(&mut self) { | 485 | pub fn wait_disabled(&mut self) -> impl Future<Output = ()> { |
| 488 | self.wait_enabled_state(false).await | 486 | self.wait_enabled_state(false) |
| 489 | } | 487 | } |
| 490 | } | 488 | } |
| 491 | 489 | ||
diff --git a/embassy-nrf/src/usb/vbus_detect.rs b/embassy-nrf/src/usb/vbus_detect.rs index bdc088dcb..8794beb2d 100644 --- a/embassy-nrf/src/usb/vbus_detect.rs +++ b/embassy-nrf/src/usb/vbus_detect.rs | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | //! Trait and implementations for performing VBUS detection. | 1 | //! Trait and implementations for performing VBUS detection. |
| 2 | 2 | ||
| 3 | use core::future::poll_fn; | 3 | use core::future::{poll_fn, Future}; |
| 4 | use core::sync::atomic::{AtomicBool, Ordering}; | 4 | use core::sync::atomic::{AtomicBool, Ordering}; |
| 5 | use core::task::Poll; | 5 | use core::task::Poll; |
| 6 | 6 | ||
| @@ -99,8 +99,8 @@ impl VbusDetect for HardwareVbusDetect { | |||
| 99 | regs.usbregstatus().read().vbusdetect() | 99 | regs.usbregstatus().read().vbusdetect() |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | async fn wait_power_ready(&mut self) -> Result<(), ()> { | 102 | fn wait_power_ready(&mut self) -> impl Future<Output = Result<(), ()>> { |
| 103 | poll_fn(move |cx| { | 103 | poll_fn(|cx| { |
| 104 | POWER_WAKER.register(cx.waker()); | 104 | POWER_WAKER.register(cx.waker()); |
| 105 | let regs = USB_REG_PERI; | 105 | let regs = USB_REG_PERI; |
| 106 | 106 | ||
| @@ -112,7 +112,6 @@ impl VbusDetect for HardwareVbusDetect { | |||
| 112 | Poll::Pending | 112 | Poll::Pending |
| 113 | } | 113 | } |
| 114 | }) | 114 | }) |
| 115 | .await | ||
| 116 | } | 115 | } |
| 117 | } | 116 | } |
| 118 | 117 | ||
| @@ -163,7 +162,7 @@ impl VbusDetect for &SoftwareVbusDetect { | |||
| 163 | self.usb_detected.load(Ordering::Relaxed) | 162 | self.usb_detected.load(Ordering::Relaxed) |
| 164 | } | 163 | } |
| 165 | 164 | ||
| 166 | async fn wait_power_ready(&mut self) -> Result<(), ()> { | 165 | fn wait_power_ready(&mut self) -> impl Future<Output = Result<(), ()>> { |
| 167 | poll_fn(move |cx| { | 166 | poll_fn(move |cx| { |
| 168 | POWER_WAKER.register(cx.waker()); | 167 | POWER_WAKER.register(cx.waker()); |
| 169 | 168 | ||
| @@ -175,6 +174,5 @@ impl VbusDetect for &SoftwareVbusDetect { | |||
| 175 | Poll::Pending | 174 | Poll::Pending |
| 176 | } | 175 | } |
| 177 | }) | 176 | }) |
| 178 | .await | ||
| 179 | } | 177 | } |
| 180 | } | 178 | } |
diff --git a/embassy-rp/src/adc.rs b/embassy-rp/src/adc.rs index 9582e43c8..19441f194 100644 --- a/embassy-rp/src/adc.rs +++ b/embassy-rp/src/adc.rs | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | //! ADC driver. | 1 | //! ADC driver. |
| 2 | use core::future::poll_fn; | 2 | use core::future::{poll_fn, Future}; |
| 3 | use core::marker::PhantomData; | 3 | use core::marker::PhantomData; |
| 4 | use core::mem; | 4 | use core::mem; |
| 5 | use core::sync::atomic::{compiler_fence, Ordering}; | 5 | use core::sync::atomic::{compiler_fence, Ordering}; |
| @@ -193,18 +193,18 @@ impl<'d> Adc<'d, Async> { | |||
| 193 | Self { phantom: PhantomData } | 193 | Self { phantom: PhantomData } |
| 194 | } | 194 | } |
| 195 | 195 | ||
| 196 | async fn wait_for_ready() { | 196 | fn wait_for_ready() -> impl Future<Output = ()> { |
| 197 | let r = Self::regs(); | 197 | let r = Self::regs(); |
| 198 | r.inte().write(|w| w.set_fifo(true)); | 198 | r.inte().write(|w| w.set_fifo(true)); |
| 199 | compiler_fence(Ordering::SeqCst); | 199 | compiler_fence(Ordering::SeqCst); |
| 200 | poll_fn(|cx| { | 200 | |
| 201 | poll_fn(move |cx| { | ||
| 201 | WAKER.register(cx.waker()); | 202 | WAKER.register(cx.waker()); |
| 202 | if r.cs().read().ready() { | 203 | if r.cs().read().ready() { |
| 203 | return Poll::Ready(()); | 204 | return Poll::Ready(()); |
| 204 | } | 205 | } |
| 205 | Poll::Pending | 206 | Poll::Pending |
| 206 | }) | 207 | }) |
| 207 | .await; | ||
| 208 | } | 208 | } |
| 209 | 209 | ||
| 210 | /// Sample a value from a channel until completed. | 210 | /// Sample a value from a channel until completed. |
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. |
| 4 | use core::cell::{RefCell, UnsafeCell}; | 4 | use core::cell::{RefCell, UnsafeCell}; |
| 5 | use core::future::poll_fn; | 5 | use core::future::{poll_fn, Future}; |
| 6 | use core::ops::{Deref, DerefMut}; | 6 | use core::ops::{Deref, DerefMut}; |
| 7 | use core::task::Poll; | 7 | use core::task::Poll; |
| 8 | use core::{fmt, mem}; | 8 | use 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 | ||
| 3 | use core::cell::Cell; | 3 | use core::cell::Cell; |
| 4 | use core::future::poll_fn; | 4 | use core::future::{poll_fn, Future}; |
| 5 | use core::mem::MaybeUninit; | 5 | use core::mem::MaybeUninit; |
| 6 | use core::sync::atomic::{AtomicBool, Ordering}; | 6 | use core::sync::atomic::{AtomicBool, Ordering}; |
| 7 | use core::task::Poll; | 7 | use 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 | ||
| 3 | use core::cell::RefCell; | 3 | use core::cell::RefCell; |
| 4 | use core::future::poll_fn; | 4 | use core::future::{poll_fn, Future}; |
| 5 | use core::marker::PhantomData; | 5 | use core::marker::PhantomData; |
| 6 | use core::ops::{Deref, DerefMut}; | 6 | use core::ops::{Deref, DerefMut}; |
| 7 | use core::task::{Context, Poll}; | 7 | use 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 | ||
| 17 | use core::cell::RefCell; | 17 | use core::cell::RefCell; |
| 18 | use core::future::poll_fn; | 18 | use core::future::{poll_fn, Future}; |
| 19 | use core::marker::PhantomData; | 19 | use core::marker::PhantomData; |
| 20 | use core::task::{Context, Poll}; | 20 | use 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. |
diff --git a/embassy-usb/src/class/cdc_acm.rs b/embassy-usb/src/class/cdc_acm.rs index 2823e522e..c5b1a56fe 100644 --- a/embassy-usb/src/class/cdc_acm.rs +++ b/embassy-usb/src/class/cdc_acm.rs | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | //! CDC-ACM class implementation, aka Serial over USB. | 1 | //! CDC-ACM class implementation, aka Serial over USB. |
| 2 | 2 | ||
| 3 | use core::cell::{Cell, RefCell}; | 3 | use core::cell::{Cell, RefCell}; |
| 4 | use core::future::poll_fn; | 4 | use core::future::{poll_fn, Future}; |
| 5 | use core::mem::{self, MaybeUninit}; | 5 | use core::mem::{self, MaybeUninit}; |
| 6 | use core::sync::atomic::{AtomicBool, Ordering}; | 6 | use core::sync::atomic::{AtomicBool, Ordering}; |
| 7 | use core::task::Poll; | 7 | use core::task::Poll; |
| @@ -108,7 +108,7 @@ impl Default for ControlShared { | |||
| 108 | } | 108 | } |
| 109 | 109 | ||
| 110 | impl ControlShared { | 110 | impl ControlShared { |
| 111 | async fn changed(&self) { | 111 | fn changed(&self) -> impl Future<Output = ()> + '_ { |
| 112 | poll_fn(|cx| { | 112 | poll_fn(|cx| { |
| 113 | if self.changed.load(Ordering::Relaxed) { | 113 | if self.changed.load(Ordering::Relaxed) { |
| 114 | self.changed.store(false, Ordering::Relaxed); | 114 | self.changed.store(false, Ordering::Relaxed); |
| @@ -118,7 +118,6 @@ impl ControlShared { | |||
| 118 | Poll::Pending | 118 | Poll::Pending |
| 119 | } | 119 | } |
| 120 | }) | 120 | }) |
| 121 | .await; | ||
| 122 | } | 121 | } |
| 123 | } | 122 | } |
| 124 | 123 | ||
diff --git a/embassy-usb/src/class/uac1/speaker.rs b/embassy-usb/src/class/uac1/speaker.rs index 6c3a4e378..25de25d9c 100644 --- a/embassy-usb/src/class/uac1/speaker.rs +++ b/embassy-usb/src/class/uac1/speaker.rs | |||
| @@ -11,7 +11,7 @@ | |||
| 11 | //! The class provides volume and mute controls for each channel. | 11 | //! The class provides volume and mute controls for each channel. |
| 12 | 12 | ||
| 13 | use core::cell::{Cell, RefCell}; | 13 | use core::cell::{Cell, RefCell}; |
| 14 | use core::future::poll_fn; | 14 | use core::future::{poll_fn, Future}; |
| 15 | use core::marker::PhantomData; | 15 | use core::marker::PhantomData; |
| 16 | use core::sync::atomic::{AtomicBool, AtomicU32, Ordering}; | 16 | use core::sync::atomic::{AtomicBool, AtomicU32, Ordering}; |
| 17 | use core::task::Poll; | 17 | use core::task::Poll; |
| @@ -389,7 +389,7 @@ impl<'d> Default for SharedControl<'d> { | |||
| 389 | } | 389 | } |
| 390 | 390 | ||
| 391 | impl<'d> SharedControl<'d> { | 391 | impl<'d> SharedControl<'d> { |
| 392 | async fn changed(&self) { | 392 | fn changed(&self) -> impl Future<Output = ()> + '_ { |
| 393 | poll_fn(|context| { | 393 | poll_fn(|context| { |
| 394 | if self.changed.load(Ordering::Relaxed) { | 394 | if self.changed.load(Ordering::Relaxed) { |
| 395 | self.changed.store(false, Ordering::Relaxed); | 395 | self.changed.store(false, Ordering::Relaxed); |
| @@ -399,7 +399,6 @@ impl<'d> SharedControl<'d> { | |||
| 399 | Poll::Pending | 399 | Poll::Pending |
| 400 | } | 400 | } |
| 401 | }) | 401 | }) |
| 402 | .await; | ||
| 403 | } | 402 | } |
| 404 | } | 403 | } |
| 405 | 404 | ||
