aboutsummaryrefslogtreecommitdiff
path: root/embassy-net/src
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-net/src
parenta4f8fddd696ca2e3705827ba4b3806cbadcb3134 (diff)
Desugar some async fns
Diffstat (limited to 'embassy-net/src')
-rw-r--r--embassy-net/src/raw.rs19
-rw-r--r--embassy-net/src/tcp.rs53
-rw-r--r--embassy-net/src/udp.rs22
3 files changed, 46 insertions, 48 deletions
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
3use core::future::poll_fn; 3use core::future::{poll_fn, Future};
4use core::mem; 4use core::mem;
5use core::task::{Context, Poll}; 5use 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
11use core::future::poll_fn; 11use core::future::{poll_fn, Future};
12use core::mem; 12use core::mem;
13use core::task::{Context, Poll}; 13use 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
3use core::future::poll_fn; 3use core::future::{poll_fn, Future};
4use core::mem; 4use core::mem;
5use core::task::{Context, Poll}; 5use 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.