io: add (read|write)_f(32|64)[_le] methods (#4022)

This commit is contained in:
LinkTed
2021-08-04 13:45:26 +02:00
committed by GitHub
parent 8198ef3881
commit 106bb94896
5 changed files with 321 additions and 0 deletions
+153
View File
@@ -2,6 +2,7 @@ use crate::io::util::chain::{chain, Chain};
use crate::io::util::read::{read, Read};
use crate::io::util::read_buf::{read_buf, ReadBuf};
use crate::io::util::read_exact::{read_exact, ReadExact};
use crate::io::util::read_int::{ReadF32, ReadF32Le, ReadF64, ReadF64Le};
use crate::io::util::read_int::{
ReadI128, ReadI128Le, ReadI16, ReadI16Le, ReadI32, ReadI32Le, ReadI64, ReadI64Le, ReadI8,
};
@@ -691,6 +692,82 @@ cfg_io_util! {
/// ```
fn read_i128(&mut self) -> ReadI128;
/// Reads an 32-bit floating point type in big-endian order from the
/// underlying reader.
///
/// Equivalent to:
///
/// ```ignore
/// async fn read_f32(&mut self) -> io::Result<f32>;
/// ```
///
/// It is recommended to use a buffered reader to avoid excessive
/// syscalls.
///
/// # Errors
///
/// This method returns the same errors as [`AsyncReadExt::read_exact`].
///
/// [`AsyncReadExt::read_exact`]: AsyncReadExt::read_exact
///
/// # Examples
///
/// Read 32-bit floating point type from a `AsyncRead`:
///
/// ```rust
/// use tokio::io::{self, AsyncReadExt};
///
/// use std::io::Cursor;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let mut reader = Cursor::new(vec![0xff, 0x7f, 0xff, 0xff]);
///
/// assert_eq!(f32::MIN, reader.read_f32().await?);
/// Ok(())
/// }
/// ```
fn read_f32(&mut self) -> ReadF32;
/// Reads an 64-bit floating point type in big-endian order from the
/// underlying reader.
///
/// Equivalent to:
///
/// ```ignore
/// async fn read_f64(&mut self) -> io::Result<f64>;
/// ```
///
/// It is recommended to use a buffered reader to avoid excessive
/// syscalls.
///
/// # Errors
///
/// This method returns the same errors as [`AsyncReadExt::read_exact`].
///
/// [`AsyncReadExt::read_exact`]: AsyncReadExt::read_exact
///
/// # Examples
///
/// Read 64-bit floating point type from a `AsyncRead`:
///
/// ```rust
/// use tokio::io::{self, AsyncReadExt};
///
/// use std::io::Cursor;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let mut reader = Cursor::new(vec![
/// 0xff, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
/// ]);
///
/// assert_eq!(f64::MIN, reader.read_f64().await?);
/// Ok(())
/// }
/// ```
fn read_f64(&mut self) -> ReadF64;
/// Reads an unsigned 16-bit integer in little-endian order from the
/// underlying reader.
///
@@ -997,6 +1074,82 @@ cfg_io_util! {
/// }
/// ```
fn read_i128_le(&mut self) -> ReadI128Le;
/// Reads an 32-bit floating point type in little-endian order from the
/// underlying reader.
///
/// Equivalent to:
///
/// ```ignore
/// async fn read_f32_le(&mut self) -> io::Result<f32>;
/// ```
///
/// It is recommended to use a buffered reader to avoid excessive
/// syscalls.
///
/// # Errors
///
/// This method returns the same errors as [`AsyncReadExt::read_exact`].
///
/// [`AsyncReadExt::read_exact`]: AsyncReadExt::read_exact
///
/// # Examples
///
/// Read 32-bit floating point type from a `AsyncRead`:
///
/// ```rust
/// use tokio::io::{self, AsyncReadExt};
///
/// use std::io::Cursor;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let mut reader = Cursor::new(vec![0xff, 0xff, 0x7f, 0xff]);
///
/// assert_eq!(f32::MIN, reader.read_f32_le().await?);
/// Ok(())
/// }
/// ```
fn read_f32_le(&mut self) -> ReadF32Le;
/// Reads an 64-bit floating point type in little-endian order from the
/// underlying reader.
///
/// Equivalent to:
///
/// ```ignore
/// async fn read_f64_le(&mut self) -> io::Result<f64>;
/// ```
///
/// It is recommended to use a buffered reader to avoid excessive
/// syscalls.
///
/// # Errors
///
/// This method returns the same errors as [`AsyncReadExt::read_exact`].
///
/// [`AsyncReadExt::read_exact`]: AsyncReadExt::read_exact
///
/// # Examples
///
/// Read 64-bit floating point type from a `AsyncRead`:
///
/// ```rust
/// use tokio::io::{self, AsyncReadExt};
///
/// use std::io::Cursor;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let mut reader = Cursor::new(vec![
/// 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff
/// ]);
///
/// assert_eq!(f64::MIN, reader.read_f64_le().await?);
/// Ok(())
/// }
/// ```
fn read_f64_le(&mut self) -> ReadF64Le;
}
/// Reads all bytes until EOF in this source, placing them into `buf`.
+152
View File
@@ -4,6 +4,7 @@ use crate::io::util::write::{write, Write};
use crate::io::util::write_all::{write_all, WriteAll};
use crate::io::util::write_all_buf::{write_all_buf, WriteAllBuf};
use crate::io::util::write_buf::{write_buf, WriteBuf};
use crate::io::util::write_int::{WriteF32, WriteF32Le, WriteF64, WriteF64Le};
use crate::io::util::write_int::{
WriteI128, WriteI128Le, WriteI16, WriteI16Le, WriteI32, WriteI32Le, WriteI64, WriteI64Le,
WriteI8,
@@ -750,6 +751,81 @@ cfg_io_util! {
/// ```
fn write_i128(&mut self, n: i128) -> WriteI128;
/// Writes an 32-bit floating point type in big-endian order to the
/// underlying writer.
///
/// Equivalent to:
///
/// ```ignore
/// async fn write_f32(&mut self, n: f32) -> io::Result<()>;
/// ```
///
/// It is recommended to use a buffered writer to avoid excessive
/// syscalls.
///
/// # Errors
///
/// This method returns the same errors as [`AsyncWriteExt::write_all`].
///
/// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
///
/// # Examples
///
/// Write 32-bit floating point type to a `AsyncWrite`:
///
/// ```rust
/// use tokio::io::{self, AsyncWriteExt};
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let mut writer = Vec::new();
///
/// writer.write_f32(f32::MIN).await?;
///
/// assert_eq!(writer, vec![0xff, 0x7f, 0xff, 0xff]);
/// Ok(())
/// }
/// ```
fn write_f32(&mut self, n: f32) -> WriteF32;
/// Writes an 64-bit floating point type in big-endian order to the
/// underlying writer.
///
/// Equivalent to:
///
/// ```ignore
/// async fn write_f64(&mut self, n: f64) -> io::Result<()>;
/// ```
///
/// It is recommended to use a buffered writer to avoid excessive
/// syscalls.
///
/// # Errors
///
/// This method returns the same errors as [`AsyncWriteExt::write_all`].
///
/// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
///
/// # Examples
///
/// Write 64-bit floating point type to a `AsyncWrite`:
///
/// ```rust
/// use tokio::io::{self, AsyncWriteExt};
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let mut writer = Vec::new();
///
/// writer.write_f64(f64::MIN).await?;
///
/// assert_eq!(writer, vec![
/// 0xff, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
/// ]);
/// Ok(())
/// }
/// ```
fn write_f64(&mut self, n: f64) -> WriteF64;
/// Writes an unsigned 16-bit integer in little-endian order to the
/// underlying writer.
@@ -1058,6 +1134,82 @@ cfg_io_util! {
/// }
/// ```
fn write_i128_le(&mut self, n: i128) -> WriteI128Le;
/// Writes an 32-bit floating point type in little-endian order to the
/// underlying writer.
///
/// Equivalent to:
///
/// ```ignore
/// async fn write_f32_le(&mut self, n: f32) -> io::Result<()>;
/// ```
///
/// It is recommended to use a buffered writer to avoid excessive
/// syscalls.
///
/// # Errors
///
/// This method returns the same errors as [`AsyncWriteExt::write_all`].
///
/// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
///
/// # Examples
///
/// Write 32-bit floating point type to a `AsyncWrite`:
///
/// ```rust
/// use tokio::io::{self, AsyncWriteExt};
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let mut writer = Vec::new();
///
/// writer.write_f32_le(f32::MIN).await?;
///
/// assert_eq!(writer, vec![0xff, 0xff, 0x7f, 0xff]);
/// Ok(())
/// }
/// ```
fn write_f32_le(&mut self, n: f32) -> WriteF32Le;
/// Writes an 64-bit floating point type in little-endian order to the
/// underlying writer.
///
/// Equivalent to:
///
/// ```ignore
/// async fn write_f64_le(&mut self, n: f64) -> io::Result<()>;
/// ```
///
/// It is recommended to use a buffered writer to avoid excessive
/// syscalls.
///
/// # Errors
///
/// This method returns the same errors as [`AsyncWriteExt::write_all`].
///
/// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
///
/// # Examples
///
/// Write 64-bit floating point type to a `AsyncWrite`:
///
/// ```rust
/// use tokio::io::{self, AsyncWriteExt};
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let mut writer = Vec::new();
///
/// writer.write_f64_le(f64::MIN).await?;
///
/// assert_eq!(writer, vec![
/// 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff
/// ]);
/// Ok(())
/// }
/// ```
fn write_f64_le(&mut self, n: f64) -> WriteF64Le;
}
/// Flushes this output stream, ensuring that all intermediately buffered
+6
View File
@@ -142,6 +142,9 @@ reader!(ReadI32, i32, get_i32);
reader!(ReadI64, i64, get_i64);
reader!(ReadI128, i128, get_i128);
reader!(ReadF32, f32, get_f32);
reader!(ReadF64, f64, get_f64);
reader!(ReadU16Le, u16, get_u16_le);
reader!(ReadU32Le, u32, get_u32_le);
reader!(ReadU64Le, u64, get_u64_le);
@@ -151,3 +154,6 @@ reader!(ReadI16Le, i16, get_i16_le);
reader!(ReadI32Le, i32, get_i32_le);
reader!(ReadI64Le, i64, get_i64_le);
reader!(ReadI128Le, i128, get_i128_le);
reader!(ReadF32Le, f32, get_f32_le);
reader!(ReadF64Le, f64, get_f64_le);
+6
View File
@@ -135,6 +135,9 @@ writer!(WriteI32, i32, put_i32);
writer!(WriteI64, i64, put_i64);
writer!(WriteI128, i128, put_i128);
writer!(WriteF32, f32, put_f32);
writer!(WriteF64, f64, put_f64);
writer!(WriteU16Le, u16, put_u16_le);
writer!(WriteU32Le, u32, put_u32_le);
writer!(WriteU64Le, u64, put_u64_le);
@@ -144,3 +147,6 @@ writer!(WriteI16Le, i16, put_i16_le);
writer!(WriteI32Le, i32, put_i32_le);
writer!(WriteI64Le, i64, put_i64_le);
writer!(WriteI128Le, i128, put_i128_le);
writer!(WriteF32Le, f32, put_f32_le);
writer!(WriteF64Le, f64, put_f64_le);
+4
View File
@@ -530,6 +530,8 @@ async_assert_fn!(tokio::io::AsyncReadExt::read_u64(&mut BoxAsyncRead): Send & Sy
async_assert_fn!(tokio::io::AsyncReadExt::read_i64(&mut BoxAsyncRead): Send & Sync & !Unpin);
async_assert_fn!(tokio::io::AsyncReadExt::read_u128(&mut BoxAsyncRead): Send & Sync & !Unpin);
async_assert_fn!(tokio::io::AsyncReadExt::read_i128(&mut BoxAsyncRead): Send & Sync & !Unpin);
async_assert_fn!(tokio::io::AsyncReadExt::read_f32(&mut BoxAsyncRead): Send & Sync & !Unpin);
async_assert_fn!(tokio::io::AsyncReadExt::read_f64(&mut BoxAsyncRead): Send & Sync & !Unpin);
async_assert_fn!(tokio::io::AsyncReadExt::read_u16_le(&mut BoxAsyncRead): Send & Sync & !Unpin);
async_assert_fn!(tokio::io::AsyncReadExt::read_i16_le(&mut BoxAsyncRead): Send & Sync & !Unpin);
async_assert_fn!(tokio::io::AsyncReadExt::read_u32_le(&mut BoxAsyncRead): Send & Sync & !Unpin);
@@ -538,6 +540,8 @@ async_assert_fn!(tokio::io::AsyncReadExt::read_u64_le(&mut BoxAsyncRead): Send &
async_assert_fn!(tokio::io::AsyncReadExt::read_i64_le(&mut BoxAsyncRead): Send & Sync & !Unpin);
async_assert_fn!(tokio::io::AsyncReadExt::read_u128_le(&mut BoxAsyncRead): Send & Sync & !Unpin);
async_assert_fn!(tokio::io::AsyncReadExt::read_i128_le(&mut BoxAsyncRead): Send & Sync & !Unpin);
async_assert_fn!(tokio::io::AsyncReadExt::read_f32_le(&mut BoxAsyncRead): Send & Sync & !Unpin);
async_assert_fn!(tokio::io::AsyncReadExt::read_f64_le(&mut BoxAsyncRead): Send & Sync & !Unpin);
async_assert_fn!(tokio::io::AsyncReadExt::read_to_end(&mut BoxAsyncRead, &mut Vec<u8>): Send & Sync & !Unpin);
async_assert_fn!(
tokio::io::AsyncReadExt::read_to_string(&mut BoxAsyncRead, &mut String): Send & Sync & !Unpin