diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-11-21 23:31:31 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-11-25 21:02:06 +0100 |
| commit | 1e2fb0459d8546ba658bb9fe150be5f1f537b48e (patch) | |
| tree | eb40a5027581896c7b78db58f509431ed6b11892 /embassy-net | |
| parent | 758f5d7ea29f1df14d5ef59c82e4b7f22545d775 (diff) | |
Switch to async-fn-in-trait
Diffstat (limited to 'embassy-net')
| -rw-r--r-- | embassy-net/Cargo.toml | 4 | ||||
| -rw-r--r-- | embassy-net/src/lib.rs | 6 | ||||
| -rw-r--r-- | embassy-net/src/tcp.rs | 120 |
3 files changed, 48 insertions, 82 deletions
diff --git a/embassy-net/Cargo.toml b/embassy-net/Cargo.toml index 76217075a..0ac53b50d 100644 --- a/embassy-net/Cargo.toml +++ b/embassy-net/Cargo.toml | |||
| @@ -42,7 +42,7 @@ log = { version = "0.4.14", optional = true } | |||
| 42 | 42 | ||
| 43 | embassy-time = { version = "0.1.0", path = "../embassy-time" } | 43 | embassy-time = { version = "0.1.0", path = "../embassy-time" } |
| 44 | embassy-sync = { version = "0.1.0", path = "../embassy-sync" } | 44 | embassy-sync = { version = "0.1.0", path = "../embassy-sync" } |
| 45 | embedded-io = { version = "0.3.1", optional = true } | 45 | embedded-io = { version = "0.4.0", optional = true } |
| 46 | 46 | ||
| 47 | managed = { version = "0.8.0", default-features = false, features = [ "map" ] } | 47 | managed = { version = "0.8.0", default-features = false, features = [ "map" ] } |
| 48 | heapless = { version = "0.7.5", default-features = false } | 48 | heapless = { version = "0.7.5", default-features = false } |
| @@ -52,7 +52,7 @@ stable_deref_trait = { version = "1.2.0", default-features = false } | |||
| 52 | futures = { version = "0.3.17", default-features = false, features = [ "async-await" ] } | 52 | futures = { version = "0.3.17", default-features = false, features = [ "async-await" ] } |
| 53 | atomic-pool = "1.0" | 53 | atomic-pool = "1.0" |
| 54 | atomic-polyfill = "1.0.1" | 54 | atomic-polyfill = "1.0.1" |
| 55 | embedded-nal-async = { version = "0.2.0", optional = true } | 55 | embedded-nal-async = { git = "https://github.com/embassy-rs/embedded-nal.git", rev = "691601e550449a53ab3a7c5eaa0411aee0a64ed0", optional = true } |
| 56 | 56 | ||
| 57 | [dependencies.smoltcp] | 57 | [dependencies.smoltcp] |
| 58 | version = "0.8.0" | 58 | version = "0.8.0" |
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index 4d30550d3..edb969842 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs | |||
| @@ -1,5 +1,9 @@ | |||
| 1 | #![cfg_attr(not(feature = "std"), no_std)] | 1 | #![cfg_attr(not(feature = "std"), no_std)] |
| 2 | #![cfg_attr(feature = "nightly", feature(type_alias_impl_trait))] | 2 | #![cfg_attr( |
| 3 | feature = "nightly", | ||
| 4 | feature(type_alias_impl_trait, async_fn_in_trait, impl_trait_projections) | ||
| 5 | )] | ||
| 6 | #![cfg_attr(feature = "nightly", allow(incomplete_features))] | ||
| 3 | 7 | ||
| 4 | // This mod MUST go first, so that the others see its macros. | 8 | // This mod MUST go first, so that the others see its macros. |
| 5 | pub(crate) mod fmt; | 9 | pub(crate) mod fmt; |
diff --git a/embassy-net/src/tcp.rs b/embassy-net/src/tcp.rs index f3bd2361c..85d9e5ee1 100644 --- a/embassy-net/src/tcp.rs +++ b/embassy-net/src/tcp.rs | |||
| @@ -271,8 +271,6 @@ impl<'d> TcpIo<'d> { | |||
| 271 | 271 | ||
| 272 | #[cfg(feature = "nightly")] | 272 | #[cfg(feature = "nightly")] |
| 273 | mod embedded_io_impls { | 273 | mod embedded_io_impls { |
| 274 | use core::future::Future; | ||
| 275 | |||
| 276 | use super::*; | 274 | use super::*; |
| 277 | 275 | ||
| 278 | impl embedded_io::Error for ConnectError { | 276 | impl embedded_io::Error for ConnectError { |
| @@ -292,30 +290,18 @@ mod embedded_io_impls { | |||
| 292 | } | 290 | } |
| 293 | 291 | ||
| 294 | impl<'d> embedded_io::asynch::Read for TcpSocket<'d> { | 292 | impl<'d> embedded_io::asynch::Read for TcpSocket<'d> { |
| 295 | type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a | 293 | async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { |
| 296 | where | 294 | self.io.read(buf).await |
| 297 | Self: 'a; | ||
| 298 | |||
| 299 | fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { | ||
| 300 | self.io.read(buf) | ||
| 301 | } | 295 | } |
| 302 | } | 296 | } |
| 303 | 297 | ||
| 304 | impl<'d> embedded_io::asynch::Write for TcpSocket<'d> { | 298 | impl<'d> embedded_io::asynch::Write for TcpSocket<'d> { |
| 305 | type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a | 299 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 306 | where | 300 | self.io.write(buf).await |
| 307 | Self: 'a; | ||
| 308 | |||
| 309 | fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { | ||
| 310 | self.io.write(buf) | ||
| 311 | } | 301 | } |
| 312 | 302 | ||
| 313 | type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a | 303 | async fn flush(&mut self) -> Result<(), Self::Error> { |
| 314 | where | 304 | self.io.flush().await |
| 315 | Self: 'a; | ||
| 316 | |||
| 317 | fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { | ||
| 318 | self.io.flush() | ||
| 319 | } | 305 | } |
| 320 | } | 306 | } |
| 321 | 307 | ||
| @@ -324,12 +310,8 @@ mod embedded_io_impls { | |||
| 324 | } | 310 | } |
| 325 | 311 | ||
| 326 | impl<'d> embedded_io::asynch::Read for TcpReader<'d> { | 312 | impl<'d> embedded_io::asynch::Read for TcpReader<'d> { |
| 327 | type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a | 313 | async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { |
| 328 | where | 314 | self.io.read(buf).await |
| 329 | Self: 'a; | ||
| 330 | |||
| 331 | fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { | ||
| 332 | self.io.read(buf) | ||
| 333 | } | 315 | } |
| 334 | } | 316 | } |
| 335 | 317 | ||
| @@ -338,27 +320,18 @@ mod embedded_io_impls { | |||
| 338 | } | 320 | } |
| 339 | 321 | ||
| 340 | impl<'d> embedded_io::asynch::Write for TcpWriter<'d> { | 322 | impl<'d> embedded_io::asynch::Write for TcpWriter<'d> { |
| 341 | type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a | 323 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 342 | where | 324 | self.io.write(buf).await |
| 343 | Self: 'a; | ||
| 344 | |||
| 345 | fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { | ||
| 346 | self.io.write(buf) | ||
| 347 | } | 325 | } |
| 348 | 326 | ||
| 349 | type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a | 327 | async fn flush(&mut self) -> Result<(), Self::Error> { |
| 350 | where | 328 | self.io.flush().await |
| 351 | Self: 'a; | ||
| 352 | |||
| 353 | fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { | ||
| 354 | self.io.flush() | ||
| 355 | } | 329 | } |
| 356 | } | 330 | } |
| 357 | } | 331 | } |
| 358 | 332 | ||
| 359 | #[cfg(all(feature = "unstable-traits", feature = "nightly"))] | 333 | #[cfg(all(feature = "unstable-traits", feature = "nightly"))] |
| 360 | pub mod client { | 334 | pub mod client { |
| 361 | use core::future::Future; | ||
| 362 | use core::mem::MaybeUninit; | 335 | use core::mem::MaybeUninit; |
| 363 | use core::ptr::NonNull; | 336 | use core::ptr::NonNull; |
| 364 | 337 | ||
| @@ -385,28 +358,29 @@ pub mod client { | |||
| 385 | { | 358 | { |
| 386 | type Error = Error; | 359 | type Error = Error; |
| 387 | type Connection<'m> = TcpConnection<'m, N, TX_SZ, RX_SZ> where Self: 'm; | 360 | type Connection<'m> = TcpConnection<'m, N, TX_SZ, RX_SZ> where Self: 'm; |
| 388 | type ConnectFuture<'m> = impl Future<Output = Result<Self::Connection<'m>, Self::Error>> + 'm | 361 | |
| 389 | where | 362 | async fn connect<'a>( |
| 390 | Self: 'm; | 363 | &'a self, |
| 391 | 364 | remote: embedded_nal_async::SocketAddr, | |
| 392 | fn connect<'m>(&'m self, remote: embedded_nal_async::SocketAddr) -> Self::ConnectFuture<'m> { | 365 | ) -> Result<Self::Connection<'a>, Self::Error> |
| 393 | async move { | 366 | where |
| 394 | let addr: crate::IpAddress = match remote.ip() { | 367 | Self: 'a, |
| 395 | IpAddr::V4(addr) => crate::IpAddress::Ipv4(crate::Ipv4Address::from_bytes(&addr.octets())), | 368 | { |
| 396 | #[cfg(feature = "proto-ipv6")] | 369 | let addr: crate::IpAddress = match remote.ip() { |
| 397 | IpAddr::V6(addr) => crate::IpAddress::Ipv6(crate::Ipv6Address::from_bytes(&addr.octets())), | 370 | IpAddr::V4(addr) => crate::IpAddress::Ipv4(crate::Ipv4Address::from_bytes(&addr.octets())), |
| 398 | #[cfg(not(feature = "proto-ipv6"))] | 371 | #[cfg(feature = "proto-ipv6")] |
| 399 | IpAddr::V6(_) => panic!("ipv6 support not enabled"), | 372 | IpAddr::V6(addr) => crate::IpAddress::Ipv6(crate::Ipv6Address::from_bytes(&addr.octets())), |
| 400 | }; | 373 | #[cfg(not(feature = "proto-ipv6"))] |
| 401 | let remote_endpoint = (addr, remote.port()); | 374 | IpAddr::V6(_) => panic!("ipv6 support not enabled"), |
| 402 | let mut socket = TcpConnection::new(&self.stack, self.state)?; | 375 | }; |
| 403 | socket | 376 | let remote_endpoint = (addr, remote.port()); |
| 404 | .socket | 377 | let mut socket = TcpConnection::new(&self.stack, self.state)?; |
| 405 | .connect(remote_endpoint) | 378 | socket |
| 406 | .await | 379 | .socket |
| 407 | .map_err(|_| Error::ConnectionReset)?; | 380 | .connect(remote_endpoint) |
| 408 | Ok(socket) | 381 | .await |
| 409 | } | 382 | .map_err(|_| Error::ConnectionReset)?; |
| 383 | Ok(socket) | ||
| 410 | } | 384 | } |
| 411 | } | 385 | } |
| 412 | 386 | ||
| @@ -445,32 +419,20 @@ pub mod client { | |||
| 445 | impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> embedded_io::asynch::Read | 419 | impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> embedded_io::asynch::Read |
| 446 | for TcpConnection<'d, N, TX_SZ, RX_SZ> | 420 | for TcpConnection<'d, N, TX_SZ, RX_SZ> |
| 447 | { | 421 | { |
| 448 | type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a | 422 | async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { |
| 449 | where | 423 | self.socket.read(buf).await |
| 450 | Self: 'a; | ||
| 451 | |||
| 452 | fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { | ||
| 453 | self.socket.read(buf) | ||
| 454 | } | 424 | } |
| 455 | } | 425 | } |
| 456 | 426 | ||
| 457 | impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> embedded_io::asynch::Write | 427 | impl<'d, const N: usize, const TX_SZ: usize, const RX_SZ: usize> embedded_io::asynch::Write |
| 458 | for TcpConnection<'d, N, TX_SZ, RX_SZ> | 428 | for TcpConnection<'d, N, TX_SZ, RX_SZ> |
| 459 | { | 429 | { |
| 460 | type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a | 430 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 461 | where | 431 | self.socket.write(buf).await |
| 462 | Self: 'a; | ||
| 463 | |||
| 464 | fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { | ||
| 465 | self.socket.write(buf) | ||
| 466 | } | 432 | } |
| 467 | 433 | ||
| 468 | type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a | 434 | async fn flush(&mut self) -> Result<(), Self::Error> { |
| 469 | where | 435 | self.socket.flush().await |
| 470 | Self: 'a; | ||
| 471 | |||
| 472 | fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { | ||
| 473 | self.socket.flush() | ||
| 474 | } | 436 | } |
| 475 | } | 437 | } |
| 476 | 438 | ||
