2020-10-30 11:26:15 -07:00
|
|
|
#![doc(html_root_url = "https://docs.rs/tokio-util/0.5.0")]
|
2019-12-21 00:54:43 +03:00
|
|
|
#![allow(clippy::needless_doctest_main)]
|
2019-10-22 10:13:49 -07:00
|
|
|
#![warn(
|
|
|
|
|
missing_debug_implementations,
|
|
|
|
|
missing_docs,
|
|
|
|
|
rust_2018_idioms,
|
|
|
|
|
unreachable_pub
|
|
|
|
|
)]
|
2020-09-13 15:50:40 +02:00
|
|
|
#![cfg_attr(docsrs, deny(broken_intra_doc_links))]
|
2019-10-22 10:13:49 -07:00
|
|
|
#![doc(test(
|
|
|
|
|
no_crate_inject,
|
|
|
|
|
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
|
|
|
|
|
))]
|
2019-11-22 15:55:10 -08:00
|
|
|
#![cfg_attr(docsrs, feature(doc_cfg))]
|
2019-10-22 10:13:49 -07:00
|
|
|
|
|
|
|
|
//! Utilities for working with Tokio.
|
2020-03-04 17:27:18 -05:00
|
|
|
//!
|
|
|
|
|
//! This crate is not versioned in lockstep with the core
|
|
|
|
|
//! [`tokio`] crate. However, `tokio-util` _will_ respect Rust's
|
|
|
|
|
//! semantic versioning policy, especially with regard to breaking changes.
|
|
|
|
|
//!
|
|
|
|
|
//! [`tokio`]: https://docs.rs/tokio
|
2019-10-22 10:13:49 -07:00
|
|
|
|
2019-11-22 15:55:10 -08:00
|
|
|
#[macro_use]
|
|
|
|
|
mod cfg;
|
|
|
|
|
|
2020-08-23 08:45:52 -07:00
|
|
|
mod loom;
|
|
|
|
|
|
2019-11-22 15:55:10 -08:00
|
|
|
cfg_codec! {
|
|
|
|
|
pub mod codec;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-23 13:02:15 -07:00
|
|
|
/*
|
|
|
|
|
Disabled due to removal of poll_ functions on UdpSocket.
|
|
|
|
|
|
|
|
|
|
See https://github.com/tokio-rs/tokio/issues/2830
|
2019-11-22 15:55:10 -08:00
|
|
|
cfg_udp! {
|
|
|
|
|
pub mod udp;
|
|
|
|
|
}
|
2020-09-23 13:02:15 -07:00
|
|
|
*/
|
2020-01-29 11:14:24 -08:00
|
|
|
|
|
|
|
|
cfg_compat! {
|
|
|
|
|
pub mod compat;
|
|
|
|
|
}
|
2020-08-23 08:45:52 -07:00
|
|
|
|
2020-09-08 09:12:32 +02:00
|
|
|
cfg_io! {
|
|
|
|
|
pub mod io;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 06:13:23 +09:00
|
|
|
cfg_rt! {
|
2020-10-12 13:44:54 -04:00
|
|
|
pub mod context;
|
|
|
|
|
}
|
2020-08-27 17:33:43 +02:00
|
|
|
|
2020-08-23 08:45:52 -07:00
|
|
|
pub mod sync;
|
2020-09-08 10:14:08 +03:00
|
|
|
|
|
|
|
|
pub mod either;
|
2020-09-24 17:26:03 -07:00
|
|
|
|
2020-10-05 14:25:04 -07:00
|
|
|
#[cfg(feature = "time")]
|
|
|
|
|
pub mod time;
|
2020-10-27 13:42:00 -07:00
|
|
|
|
|
|
|
|
#[cfg(any(feature = "io", feature = "codec"))]
|
|
|
|
|
mod util {
|
|
|
|
|
use tokio::io::{AsyncRead, ReadBuf};
|
|
|
|
|
|
|
|
|
|
use bytes::BufMut;
|
|
|
|
|
use futures_core::ready;
|
|
|
|
|
use std::io;
|
2020-10-29 18:45:19 +01:00
|
|
|
use std::mem::MaybeUninit;
|
2020-10-27 13:42:00 -07:00
|
|
|
use std::pin::Pin;
|
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
|
2020-11-01 10:22:22 +01:00
|
|
|
/// Try to read data from an `AsyncRead` into an implementer of the [`Buf`] trait.
|
|
|
|
|
///
|
|
|
|
|
/// [`Buf`]: bytes::Buf
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use bytes::{Bytes, BytesMut};
|
|
|
|
|
/// use tokio::stream;
|
|
|
|
|
/// use tokio::io::Result;
|
|
|
|
|
/// use tokio_util::io::{StreamReader, poll_read_buf};
|
|
|
|
|
/// use futures::future::poll_fn;
|
|
|
|
|
/// use std::pin::Pin;
|
|
|
|
|
/// # #[tokio::main]
|
|
|
|
|
/// # async fn main() -> std::io::Result<()> {
|
|
|
|
|
///
|
|
|
|
|
/// // Create a reader from an iterator. This particular reader will always be
|
|
|
|
|
/// // ready.
|
|
|
|
|
/// let mut read = StreamReader::new(stream::iter(vec![Result::Ok(Bytes::from_static(&[0, 1, 2, 3]))]));
|
|
|
|
|
///
|
|
|
|
|
/// let mut buf = BytesMut::new();
|
|
|
|
|
/// let mut reads = 0;
|
|
|
|
|
///
|
|
|
|
|
/// loop {
|
|
|
|
|
/// reads += 1;
|
|
|
|
|
/// let n = poll_fn(|cx| poll_read_buf(Pin::new(&mut read), cx, &mut buf)).await?;
|
|
|
|
|
///
|
|
|
|
|
/// if n == 0 {
|
|
|
|
|
/// break;
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// // one or more reads might be necessary.
|
|
|
|
|
/// assert!(reads >= 1);
|
|
|
|
|
/// assert_eq!(&buf[..], &[0, 1, 2, 3]);
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
/// # }
|
|
|
|
|
/// ```
|
|
|
|
|
#[cfg_attr(not(feature = "io"), allow(unreachable_pub))]
|
|
|
|
|
pub fn poll_read_buf<T: AsyncRead, B: BufMut>(
|
2020-10-27 13:42:00 -07:00
|
|
|
io: Pin<&mut T>,
|
2020-11-01 10:22:22 +01:00
|
|
|
cx: &mut Context<'_>,
|
|
|
|
|
buf: &mut B,
|
2020-10-27 13:42:00 -07:00
|
|
|
) -> Poll<io::Result<usize>> {
|
|
|
|
|
if !buf.has_remaining_mut() {
|
|
|
|
|
return Poll::Ready(Ok(0));
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-29 18:45:19 +01:00
|
|
|
let n = {
|
|
|
|
|
let dst = buf.bytes_mut();
|
|
|
|
|
let dst = unsafe { &mut *(dst as *mut _ as *mut [MaybeUninit<u8>]) };
|
|
|
|
|
let mut buf = ReadBuf::uninit(dst);
|
|
|
|
|
let ptr = buf.filled().as_ptr();
|
|
|
|
|
ready!(io.poll_read(cx, &mut buf)?);
|
2020-10-27 13:42:00 -07:00
|
|
|
|
2020-10-29 18:45:19 +01:00
|
|
|
// Ensure the pointer does not change from under us
|
|
|
|
|
assert_eq!(ptr, buf.filled().as_ptr());
|
|
|
|
|
buf.filled().len()
|
|
|
|
|
};
|
2020-10-27 13:42:00 -07:00
|
|
|
|
2020-10-29 18:45:19 +01:00
|
|
|
// Safety: This is guaranteed to be the number of initialized (and read)
|
|
|
|
|
// bytes due to the invariants provided by `ReadBuf::filled`.
|
2020-10-27 13:42:00 -07:00
|
|
|
unsafe {
|
|
|
|
|
buf.advance_mut(n);
|
|
|
|
|
}
|
2020-10-29 18:45:19 +01:00
|
|
|
|
2020-10-27 13:42:00 -07:00
|
|
|
Poll::Ready(Ok(n))
|
|
|
|
|
}
|
|
|
|
|
}
|