tools: change tools::io::ops::*:

We'll stick to existing naming conventions and provide:
    tools::io::{ReadExt, WriteExt}

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-07-01 09:53:04 +02:00
parent 205a9fc2aa
commit 9e7dfce827
2 changed files with 18 additions and 19 deletions

View File

@ -1,10 +1,12 @@
//! Module providing I/O helpers (sync and async). //! Module providing I/O helpers (sync and async).
//! //!
//! The [`ops`](io::ops) module provides helper traits for types implementing [`Read`](std::io::Read). //! The [`ReadExt`] trait provides additional operations for handling byte buffers for types
//! //! implementing [`Read`](std::io::Read).
//! The top level functions in of this module here are used for standalone implementations of
//! various functionality which is actually intended to be available as methods to types
//! implementing `AsyncRead`, which, however, without async/await cannot be methods due to them
//! having non-static lifetimes in that case.
pub mod ops; // DEPRECATED:
pub mod ops {
pub use super::ReadExt as ReadExtOps;
}
mod read;
pub use read::*;

View File

@ -1,7 +1,4 @@
//! This module provides additional operations for handling byte buffers for types implementing //! Helpers for `Read`.
//! [`Read`](std::io::Read).
//!
//! See the [`ReadExtOps`](ops::ReadExtOps) trait for examples.
use std::io; use std::io;
@ -16,7 +13,7 @@ use crate::vec::{self, ops::*};
/// ///
/// Examples: /// Examples:
/// ```no_run /// ```no_run
/// use proxmox::tools::io::ops::*; /// use proxmox::tools::io::ReadExt;
/// ///
/// # fn code() -> std::io::Result<()> { /// # fn code() -> std::io::Result<()> {
/// let mut file = std::fs::File::open("some.data")?; /// let mut file = std::fs::File::open("some.data")?;
@ -35,7 +32,7 @@ use crate::vec::{self, ops::*};
/// ///
/// ```no_run /// ```no_run
/// # use endian_trait::Endian; /// # use endian_trait::Endian;
/// # use proxmox::tools::io::ops::*; /// # use proxmox::tools::io::ReadExt;
/// ///
/// #[derive(Endian)] /// #[derive(Endian)]
/// #[repr(C)] /// #[repr(C)]
@ -53,7 +50,7 @@ use crate::vec::{self, ops::*};
/// ``` /// ```
/// ///
/// [`Endian`]: https://docs.rs/endian_trait/0.6/endian_trait/trait.Endian.html /// [`Endian`]: https://docs.rs/endian_trait/0.6/endian_trait/trait.Endian.html
pub trait ReadExtOps { pub trait ReadExt {
/// Read data into a newly allocated vector. This is a shortcut for: /// Read data into a newly allocated vector. This is a shortcut for:
/// ```ignore /// ```ignore
/// let mut data = Vec::with_capacity(len); /// let mut data = Vec::with_capacity(len);
@ -65,7 +62,7 @@ pub trait ReadExtOps {
/// ///
/// With this trait, we just use: /// With this trait, we just use:
/// ```no_run /// ```no_run
/// use proxmox::tools::io::ops::*; /// use proxmox::tools::io::ReadExt;
/// # fn code(mut reader: std::fs::File, len: usize) -> std::io::Result<()> { /// # fn code(mut reader: std::fs::File, len: usize) -> std::io::Result<()> {
/// let data = reader.read_exact_allocated(len)?; /// let data = reader.read_exact_allocated(len)?;
/// # Ok(()) /// # Ok(())
@ -89,7 +86,7 @@ pub trait ReadExtOps {
/// ///
/// ```no_run /// ```no_run
/// # use endian_trait::Endian; /// # use endian_trait::Endian;
/// use proxmox::tools::io::ops::*; /// use proxmox::tools::io::ReadExt;
/// ///
/// #[derive(Endian)] /// #[derive(Endian)]
/// #[repr(C, packed)] /// #[repr(C, packed)]
@ -121,7 +118,7 @@ pub trait ReadExtOps {
/// ///
/// ```no_run /// ```no_run
/// # use endian_trait::Endian; /// # use endian_trait::Endian;
/// use proxmox::tools::io::ops::*; /// use proxmox::tools::io::ReadExt;
/// ///
/// #[derive(Endian)] /// #[derive(Endian)]
/// #[repr(C, packed)] /// #[repr(C, packed)]
@ -153,7 +150,7 @@ pub trait ReadExtOps {
/// ///
/// ```no_run /// ```no_run
/// # use endian_trait::Endian; /// # use endian_trait::Endian;
/// use proxmox::tools::io::ops::*; /// use proxmox::tools::io::ReadExt;
/// ///
/// #[derive(Endian)] /// #[derive(Endian)]
/// #[repr(C, packed)] /// #[repr(C, packed)]
@ -175,7 +172,7 @@ pub trait ReadExtOps {
unsafe fn read_be_value<T: Endian>(&mut self) -> io::Result<T>; unsafe fn read_be_value<T: Endian>(&mut self) -> io::Result<T>;
} }
impl<R: io::Read> ReadExtOps for R { impl<R: io::Read> ReadExt for R {
fn read_exact_allocated(&mut self, size: usize) -> io::Result<Vec<u8>> { fn read_exact_allocated(&mut self, size: usize) -> io::Result<Vec<u8>> {
let mut out = unsafe { vec::uninitialized(size) }; let mut out = unsafe { vec::uninitialized(size) };
self.read_exact(&mut out)?; self.read_exact(&mut out)?;