mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00

In an effort to reach API stability, the `tokio` crate is shedding its _public_ dependencies on crates that are either a) do not provide a stable (1.0+) release with longevity guarantees or b) match the `tokio` release cadence. Of course, implementing `std` traits fits the requirements. The on exception, for now, is the `Stream` trait found in `futures_core`. It is expected that this trait will not change much and be moved into `std. Since Tokio is not yet going reaching 1.0, I feel that it is acceptable to maintain a dependency on this trait given how foundational it is. Since the `Stream` implementation is optional, types that are logically streams provide `async fn next_*` functions to obtain the next value. Avoiding the `next()` name prevents fn conflicts with `StreamExt::next()`. Additionally, some misc cleanup is also done: - `tokio::io::io` -> `tokio::io::util`. - `delay` -> `delay_until`. - `Timeout::new` -> `timeout(...)`. - `signal::ctrl_c()` returns a future instead of a stream. - `{tcp,unix}::Incoming` is removed (due to lack of `Stream` trait). - `time::Throttle` is removed (due to lack of `Stream` trait). - Fix: `mpsc::UnboundedSender::send(&self)` (no more conflict with `Sink` fns).
123 lines
4.5 KiB
Rust
123 lines
4.5 KiB
Rust
use crate::io::util::lines::{lines, Lines};
|
|
use crate::io::util::read_line::{read_line, ReadLine};
|
|
use crate::io::util::read_until::{read_until, ReadUntil};
|
|
use crate::io::util::split::{split, Split};
|
|
use crate::io::AsyncBufRead;
|
|
|
|
/// An extension trait which adds utility methods to `AsyncBufRead` types.
|
|
pub trait AsyncBufReadExt: AsyncBufRead {
|
|
/// Creates a future which will read all the bytes associated with this I/O
|
|
/// object into `buf` until the delimiter `byte` or EOF is reached.
|
|
/// This method is the async equivalent to [`BufRead::read_until`](std::io::BufRead::read_until).
|
|
///
|
|
/// This function will read bytes from the underlying stream until the
|
|
/// delimiter or EOF is found. Once found, all bytes up to, and including,
|
|
/// the delimiter (if found) will be appended to `buf`.
|
|
///
|
|
/// The returned future will resolve to the number of bytes read once the read
|
|
/// operation is completed.
|
|
///
|
|
/// In the case of an error the buffer and the object will be discarded, with
|
|
/// the error yielded.
|
|
fn read_until<'a>(&'a mut self, byte: u8, buf: &'a mut Vec<u8>) -> ReadUntil<'a, Self>
|
|
where
|
|
Self: Unpin,
|
|
{
|
|
read_until(self, byte, buf)
|
|
}
|
|
|
|
/// Creates a future which will read all the bytes associated with this I/O
|
|
/// object into `buf` until a newline (the 0xA byte) or EOF is reached,
|
|
/// This method is the async equivalent to [`BufRead::read_line`](std::io::BufRead::read_line).
|
|
///
|
|
/// This function will read bytes from the underlying stream until the
|
|
/// newline delimiter (the 0xA byte) or EOF is found. Once found, all bytes
|
|
/// up to, and including, the delimiter (if found) will be appended to
|
|
/// `buf`.
|
|
///
|
|
/// The returned future will resolve to the number of bytes read once the read
|
|
/// operation is completed.
|
|
///
|
|
/// In the case of an error the buffer and the object will be discarded, with
|
|
/// the error yielded.
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// This function has the same error semantics as [`read_until`] and will
|
|
/// also return an error if the read bytes are not valid UTF-8. If an I/O
|
|
/// error is encountered then `buf` may contain some bytes already read in
|
|
/// the event that all data read so far was valid UTF-8.
|
|
///
|
|
/// [`read_until`]: AsyncBufReadExt::read_until
|
|
fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self>
|
|
where
|
|
Self: Unpin,
|
|
{
|
|
read_line(self, buf)
|
|
}
|
|
|
|
/// Returns a stream of the contents of this reader split on the byte
|
|
/// `byte`.
|
|
///
|
|
/// This method is the asynchronous equivalent to
|
|
/// [`BufRead::split`](std::io::BufRead::split).
|
|
///
|
|
/// The stream returned from this function will yield instances of
|
|
/// [`io::Result`]`<`[`Vec<u8>`]`>`. Each vector returned will *not* have
|
|
/// the delimiter byte at the end.
|
|
///
|
|
/// [`io::Result`]: std::io::Result
|
|
/// [`Vec<u8>`]: std::vec::Vec
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// Each item of the stream has the same error semantics as
|
|
/// [`AsyncBufReadExt::read_until`](AsyncBufReadExt::read_until).
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```
|
|
/// # use tokio::io::AsyncBufRead;
|
|
/// use tokio::io::AsyncBufReadExt;
|
|
///
|
|
/// # async fn dox(my_buf_read: impl AsyncBufRead + Unpin) -> std::io::Result<()> {
|
|
/// let mut segments = my_buf_read.split(b'f');
|
|
///
|
|
/// while let Some(segment) = segments.next_segment().await? {
|
|
/// println!("length = {}", segment.len())
|
|
/// }
|
|
/// # Ok(())
|
|
/// # }
|
|
/// ```
|
|
fn split(self, byte: u8) -> Split<Self>
|
|
where
|
|
Self: Sized + Unpin,
|
|
{
|
|
split(self, byte)
|
|
}
|
|
|
|
/// Returns a stream over the lines of this reader.
|
|
/// This method is the async equivalent to [`BufRead::lines`](std::io::BufRead::lines).
|
|
///
|
|
/// The stream returned from this function will yield instances of
|
|
/// [`io::Result`]`<`[`String`]`>`. Each string returned will *not* have a newline
|
|
/// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end.
|
|
///
|
|
/// [`io::Result`]: std::io::Result
|
|
/// [`String`]: String
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// Each line of the stream has the same error semantics as [`AsyncBufReadExt::read_line`].
|
|
///
|
|
/// [`AsyncBufReadExt::read_line`]: AsyncBufReadExt::read_line
|
|
fn lines(self) -> Lines<Self>
|
|
where
|
|
Self: Sized,
|
|
{
|
|
lines(self)
|
|
}
|
|
}
|
|
|
|
impl<R: AsyncBufRead + ?Sized> AsyncBufReadExt for R {}
|