Skip to content

Commit 31b62d3

Browse files
committed
io: add ReadReady, WriteReady.
1 parent 931dd37 commit 31b62d3

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

embedded-io-async/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ extern crate alloc;
99

1010
mod impls;
1111

12-
pub use embedded_io::{Error, ErrorKind, Io, ReadExactError, SeekFrom, WriteAllError};
12+
pub use embedded_io::{
13+
Error, ErrorKind, Io, ReadExactError, ReadReady, SeekFrom, WriteAllError, WriteReady,
14+
};
1315

1416
/// Async reader.
1517
///

embedded-io/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Added `ReadReady`, `WriteReady` traits. They allow peeking whether the i/o handle is ready to read/write, so they allow using the traits in a non-blocking way.
1011
- Moved `embedded_io::blocking` to the crate root.
1112
- Split async traits to the `embedded-io-async` crate.
1213
- Split async trait adapters to separate crates.

embedded-io/src/impls/boxx.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{BufRead, Io, Read, Seek, Write};
1+
use crate::{BufRead, Io, Read, ReadReady, Seek, Write, WriteReady};
22
use alloc::boxed::Box;
33

44
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
@@ -45,3 +45,19 @@ impl<T: ?Sized + Seek> Seek for Box<T> {
4545
T::seek(self, pos)
4646
}
4747
}
48+
49+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
50+
impl<T: ?Sized + ReadReady> ReadReady for Box<T> {
51+
#[inline]
52+
fn read_ready(&mut self) -> Result<bool, Self::Error> {
53+
T::read_ready(self)
54+
}
55+
}
56+
57+
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
58+
impl<T: ?Sized + WriteReady> WriteReady for Box<T> {
59+
#[inline]
60+
fn write_ready(&mut self) -> Result<bool, Self::Error> {
61+
T::write_ready(self)
62+
}
63+
}

embedded-io/src/lib.rs

+41
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,33 @@ pub trait Seek: crate::Io {
262262
}
263263
}
264264

265+
/// Get whether a reader is ready.
266+
///
267+
/// This allows using a [`Read`] or [`BufRead`] in a nonblocking fashion, i.e. trying to read
268+
/// only when it is ready.
269+
pub trait ReadReady: crate::Io {
270+
/// Get whether the reader is ready for immediately reading.
271+
///
272+
/// This usually means that there is either some bytes have been received and are buffered and ready to be read,
273+
/// or that the reader is at EOF.
274+
///
275+
/// If this returns `true`, it's guaranteed that the next call to [`Read::read`] or [`BufRead::fill_buf`] will not block.
276+
fn read_ready(&mut self) -> Result<bool, Self::Error>;
277+
}
278+
279+
/// Get whether a writer is ready.
280+
///
281+
/// This allows using a [`Write`] in a nonblocking fashion, i.e. trying to write
282+
/// only when it is ready.
283+
pub trait WriteReady: crate::Io {
284+
/// Get whether the writer is ready for immediately writeing.
285+
///
286+
/// This usually means that there is free space in the internal transmit buffer.
287+
///
288+
/// If this returns `true`, it's guaranteed that the next call to [`Write::write`] will not block.
289+
fn write_ready(&mut self) -> Result<bool, Self::Error>;
290+
}
291+
265292
impl<T: ?Sized + Read> Read for &mut T {
266293
#[inline]
267294
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
@@ -297,3 +324,17 @@ impl<T: ?Sized + Seek> Seek for &mut T {
297324
T::seek(self, pos)
298325
}
299326
}
327+
328+
impl<T: ?Sized + ReadReady> ReadReady for &mut T {
329+
#[inline]
330+
fn read_ready(&mut self) -> Result<bool, Self::Error> {
331+
T::read_ready(self)
332+
}
333+
}
334+
335+
impl<T: ?Sized + WriteReady> WriteReady for &mut T {
336+
#[inline]
337+
fn write_ready(&mut self) -> Result<bool, Self::Error> {
338+
T::write_ready(self)
339+
}
340+
}

0 commit comments

Comments
 (0)