tools: move vec::ops::* to vec::ByteVecExt

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-07-01 11:00:29 +02:00
parent e290d78840
commit 3111d615b8
3 changed files with 16 additions and 14 deletions

View File

@ -4,7 +4,7 @@ use std::io;
use endian_trait::Endian; use endian_trait::Endian;
use crate::vec::{self, ops::*}; use crate::vec::{self, ByteVecExt};
/// Adds some additional related functionality for types implementing [`Read`](std::io::Read). /// Adds some additional related functionality for types implementing [`Read`](std::io::Read).
/// ///

View File

@ -17,7 +17,7 @@
//! //!
//! Examples: //! Examples:
//! ```no_run //! ```no_run
//! use proxmox::tools::vec::{self, ops::*}; //! use proxmox::tools::vec::{self, ByteVecExt};
//! //!
//! # let size = 64usize; //! # let size = 64usize;
//! # let more = 64usize; //! # let more = 64usize;
@ -31,7 +31,9 @@
//! }); //! });
//! ``` //! ```
pub mod ops; mod byte_vec;
pub use byte_vec::*;
/// Create an uninitialized byte vector of a specific size. /// Create an uninitialized byte vector of a specific size.
/// ///

View File

@ -3,7 +3,7 @@
//! Example: //! Example:
//! ``` //! ```
//! # use std::io::Read; //! # use std::io::Read;
//! use proxmox::tools::vec::{self, ops::*}; //! use proxmox::tools::vec::{self, ByteVecExt};
//! //!
//! fn append_1024_to_vec<T: Read>(mut input: T, buffer: &mut Vec<u8>) -> std::io::Result<()> { //! fn append_1024_to_vec<T: Read>(mut input: T, buffer: &mut Vec<u8>) -> std::io::Result<()> {
//! input.read_exact(unsafe { buffer.grow_uninitialized(1024) }) //! input.read_exact(unsafe { buffer.grow_uninitialized(1024) })
@ -14,8 +14,8 @@
/// Example: /// Example:
/// ``` /// ```
/// # use std::io::Read; /// # use std::io::Read;
/// # use proxmox::tools::io::{self, ops::*}; /// # use proxmox::tools::io::{self, ReadExt};
/// use proxmox::tools::vec::{self, ops::*}; /// use proxmox::tools::vec::{self, ByteVecExt};
/// ///
/// # fn code(mut file: std::fs::File, mut data: Vec<u8>) -> std::io::Result<()> { /// # fn code(mut file: std::fs::File, mut data: Vec<u8>) -> std::io::Result<()> {
/// file.read_exact(unsafe { /// file.read_exact(unsafe {
@ -26,14 +26,14 @@
/// ``` /// ```
/// ///
/// Note that this module also provides a safe alternative for the case where /// Note that this module also provides a safe alternative for the case where
/// `grow_uninitialized()` is directly followed by a `read_exact()` call via the [`ReadExtOps`] /// `grow_uninitialized()` is directly followed by a `read_exact()` call via the [`ReadExt`]
/// trait: /// trait:
/// ```ignore /// ```ignore
/// file.append_to_vec(&mut data, 1024)?; /// file.append_to_vec(&mut data, 1024)?;
/// ``` /// ```
/// ///
/// [`ReadExtOps`]: crate::io::ops::ReadExtOps /// [`ReadExt`]: crate::io::ReadExt
pub trait VecU8ExtOps { pub trait ByteVecExt {
/// Grow a vector without initializing its elements. The difference to simply using `reserve` /// Grow a vector without initializing its elements. The difference to simply using `reserve`
/// is that it also updates the actual length, making the newly allocated data part of the /// is that it also updates the actual length, making the newly allocated data part of the
/// slice. /// slice.
@ -50,7 +50,7 @@ pub trait VecU8ExtOps {
/// This returns a mutable slice to the newly allocated space, so it can be used inline: /// This returns a mutable slice to the newly allocated space, so it can be used inline:
/// ``` /// ```
/// # use std::io::Read; /// # use std::io::Read;
/// # use proxmox::tools::vec::ops::*; /// # use proxmox::tools::vec::ByteVecExt;
/// # fn test(mut file: std::fs::File, buffer: &mut Vec<u8>) -> std::io::Result<()> { /// # fn test(mut file: std::fs::File, buffer: &mut Vec<u8>) -> std::io::Result<()> {
/// file.read_exact(unsafe { buffer.grow_uninitialized(1024) })?; /// file.read_exact(unsafe { buffer.grow_uninitialized(1024) })?;
/// # Ok(()) /// # Ok(())
@ -58,13 +58,13 @@ pub trait VecU8ExtOps {
/// ``` /// ```
/// ///
/// Although for the above case it is recommended to use the even shorter version from the /// Although for the above case it is recommended to use the even shorter version from the
/// [`ReadExtOps`] trait: /// [`ReadExt`] trait:
/// ```ignore /// ```ignore
/// // use crate::tools::vec::ops::ReadExtOps; /// // use crate::tools::vec::ByteVecExt;
/// file.append_to_vec(&mut buffer, 1024)?; /// file.append_to_vec(&mut buffer, 1024)?;
/// ``` /// ```
/// ///
/// [`ReadExtOps`]: crate::io::ops::ReadExtOps /// [`ReadExt`]: crate::io::ReadExt
unsafe fn grow_uninitialized(&mut self, more: usize) -> &mut [u8]; unsafe fn grow_uninitialized(&mut self, more: usize) -> &mut [u8];
/// Resize a vector to a specific size without initializing its data. This is a shortcut for: /// Resize a vector to a specific size without initializing its data. This is a shortcut for:
@ -80,7 +80,7 @@ pub trait VecU8ExtOps {
unsafe fn resize_uninitialized(&mut self, total: usize); unsafe fn resize_uninitialized(&mut self, total: usize);
} }
impl VecU8ExtOps for Vec<u8> { impl ByteVecExt for Vec<u8> {
unsafe fn grow_uninitialized(&mut self, more: usize) -> &mut [u8] { unsafe fn grow_uninitialized(&mut self, more: usize) -> &mut [u8] {
let old_len = self.len(); let old_len = self.len();
self.reserve(more); self.reserve(more);