aboutsummaryrefslogtreecommitdiff
path: root/embassy-net/src/tcp.rs
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/tcp.rs
parenta4f8fddd696ca2e3705827ba4b3806cbadcb3134 (diff)
Desugar some async fns
Diffstat (limited to 'embassy-net/src/tcp.rs')
-rw-r--r--embassy-net/src/tcp.rs53
1 files changed, 25 insertions, 28 deletions
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 {