Skip to content

Commit 129282e

Browse files
committed
io-async: fix warnings for latest nightly (1.75.0)
Fixes `cargo check` warnings for the latest nightly version (`1.75.0`). Recommended public API signatures for async trait functions has changed to recommending the desugared version for compatibility with different `async` runtimes.
1 parent 8e66252 commit 129282e

File tree

1 file changed

+65
-43
lines changed

1 file changed

+65
-43
lines changed

embedded-io-async/src/lib.rs

+65-43
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#[cfg(feature = "alloc")]
1313
extern crate alloc;
1414

15+
use core::future::Future;
16+
1517
mod impls;
1618

1719
pub use embedded_io::{
@@ -46,7 +48,7 @@ pub trait Read: ErrorType {
4648
/// the hardware with e.g. DMA.
4749
///
4850
/// Implementations should document whether they're actually side-effect-free on cancel or not.
49-
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>;
51+
fn read(&mut self, buf: &mut [u8]) -> impl Future<Output = Result<usize, Self::Error>>;
5052

5153
/// Read the exact number of bytes required to fill `buf`.
5254
///
@@ -55,18 +57,20 @@ pub trait Read: ErrorType {
5557
///
5658
/// This function is not side-effect-free on cancel (AKA "cancel-safe"), i.e. if you cancel (drop) a returned
5759
/// future that hasn't completed yet, some bytes might have already been read, which will get lost.
58-
async fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), ReadExactError<Self::Error>> {
59-
while !buf.is_empty() {
60-
match self.read(buf).await {
61-
Ok(0) => break,
62-
Ok(n) => buf = &mut buf[n..],
63-
Err(e) => return Err(ReadExactError::Other(e)),
60+
fn read_exact(&mut self, mut buf: &mut [u8]) -> impl Future<Output = Result<(), ReadExactError<Self::Error>>> {
61+
async move {
62+
while !buf.is_empty() {
63+
match self.read(buf).await {
64+
Ok(0) => break,
65+
Ok(n) => buf = &mut buf[n..],
66+
Err(e) => return Err(ReadExactError::Other(e)),
67+
}
68+
}
69+
if buf.is_empty() {
70+
Ok(())
71+
} else {
72+
Err(ReadExactError::UnexpectedEof)
6473
}
65-
}
66-
if buf.is_empty() {
67-
Ok(())
68-
} else {
69-
Err(ReadExactError::UnexpectedEof)
7074
}
7175
}
7276
}
@@ -82,7 +86,7 @@ pub trait BufRead: ErrorType {
8286
/// If the reader is at end-of-file (EOF), an empty slice is returned. There is no guarantee that a reader at EOF
8387
/// will always be so in the future, for example a reader can stop being at EOF if another process appends
8488
/// more bytes to the underlying file.
85-
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error>;
89+
fn fill_buf(&mut self) -> impl Future<Output = Result<&[u8], Self::Error>>;
8690

8791
/// Tell this buffer that `amt` bytes have been consumed from the buffer, so they should no longer be returned in calls to `fill_buf`.
8892
fn consume(&mut self, amt: usize);
@@ -116,11 +120,13 @@ pub trait Write: ErrorType {
116120
/// the hardware with e.g. DMA.
117121
///
118122
/// Implementations should document whether they're actually side-effect-free on cancel or not.
119-
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>;
123+
fn write(&mut self, buf: &[u8]) -> impl Future<Output = Result<usize, Self::Error>>;
120124

121125
/// Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
122-
async fn flush(&mut self) -> Result<(), Self::Error> {
123-
Ok(())
126+
fn flush(&mut self) -> impl Future<Output = Result<(), Self::Error>> {
127+
async {
128+
Ok(())
129+
}
124130
}
125131

126132
/// Write an entire buffer into this writer.
@@ -130,16 +136,18 @@ pub trait Write: ErrorType {
130136
///
131137
/// This function is not side-effect-free on cancel (AKA "cancel-safe"), i.e. if you cancel (drop) a returned
132138
/// future that hasn't completed yet, some bytes might have already been written.
133-
async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
134-
let mut buf = buf;
135-
while !buf.is_empty() {
136-
match self.write(buf).await {
137-
Ok(0) => panic!("write() returned Ok(0)"),
138-
Ok(n) => buf = &buf[n..],
139-
Err(e) => return Err(e),
139+
fn write_all(&mut self, buf: &[u8]) -> impl Future<Output = Result<(), Self::Error>> {
140+
async move {
141+
let mut buf = buf;
142+
while !buf.is_empty() {
143+
match self.write(buf).await {
144+
Ok(0) => panic!("write() returned Ok(0)"),
145+
Ok(n) => buf = &buf[n..],
146+
Err(e) => return Err(e),
147+
}
140148
}
149+
Ok(())
141150
}
142-
Ok(())
143151
}
144152
}
145153

@@ -148,52 +156,66 @@ pub trait Write: ErrorType {
148156
/// This trait is the `embedded-io-async` equivalent of [`std::io::Seek`].
149157
pub trait Seek: ErrorType {
150158
/// Seek to an offset, in bytes, in a stream.
151-
async fn seek(&mut self, pos: SeekFrom) -> Result<u64, Self::Error>;
159+
fn seek(&mut self, pos: SeekFrom) -> impl Future<Output = Result<u64, Self::Error>>;
152160

153161
/// Rewind to the beginning of a stream.
154-
async fn rewind(&mut self) -> Result<(), Self::Error> {
155-
self.seek(SeekFrom::Start(0)).await?;
156-
Ok(())
162+
fn rewind(&mut self) -> impl Future<Output = Result<(), Self::Error>> {
163+
async move {
164+
self.seek(SeekFrom::Start(0)).await?;
165+
Ok(())
166+
}
157167
}
158168

159169
/// Returns the current seek position from the start of the stream.
160-
async fn stream_position(&mut self) -> Result<u64, Self::Error> {
161-
self.seek(SeekFrom::Current(0)).await
170+
fn stream_position(&mut self) -> impl Future<Output = Result<u64, Self::Error>> {
171+
async move {
172+
self.seek(SeekFrom::Current(0)).await
173+
}
162174
}
163175
}
164176

165-
impl<T: ?Sized + Read> Read for &mut T {
177+
impl<T: ?Sized + Read + Send> Read for &mut T {
166178
#[inline]
167-
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
168-
T::read(self, buf).await
179+
fn read(&mut self, buf: &mut [u8]) -> impl Future<Output = Result<usize, Self::Error>> {
180+
async {
181+
T::read(self, buf).await
182+
}
169183
}
170184
}
171185

172-
impl<T: ?Sized + BufRead> BufRead for &mut T {
173-
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
174-
T::fill_buf(self).await
186+
impl<T: ?Sized + BufRead + Send> BufRead for &mut T {
187+
fn fill_buf(&mut self) -> impl Future<Output = Result<&[u8], Self::Error>> {
188+
async {
189+
T::fill_buf(self).await
190+
}
175191
}
176192

177193
fn consume(&mut self, amt: usize) {
178194
T::consume(self, amt);
179195
}
180196
}
181197

182-
impl<T: ?Sized + Write> Write for &mut T {
198+
impl<T: ?Sized + Write + Send> Write for &mut T {
183199
#[inline]
184-
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
185-
T::write(self, buf).await
200+
fn write(&mut self, buf: &[u8]) -> impl Future<Output = Result<usize, Self::Error>> {
201+
async {
202+
T::write(self, buf).await
203+
}
186204
}
187205

188206
#[inline]
189-
async fn flush(&mut self) -> Result<(), Self::Error> {
190-
T::flush(self).await
207+
fn flush(&mut self) -> impl Future<Output = Result<(), Self::Error>> {
208+
async {
209+
T::flush(self).await
210+
}
191211
}
192212
}
193213

194214
impl<T: ?Sized + Seek> Seek for &mut T {
195215
#[inline]
196-
async fn seek(&mut self, pos: SeekFrom) -> Result<u64, Self::Error> {
197-
T::seek(self, pos).await
216+
fn seek(&mut self, pos: SeekFrom) -> impl Future<Output = Result<u64, Self::Error>> {
217+
async move {
218+
T::seek(self, pos).await
219+
}
198220
}
199221
}

0 commit comments

Comments
 (0)