VecExtensions

Trait VecExtensions 

Source
pub trait VecExtensions<T: Copy + Sized>: Zeroize {
    // Required methods
    fn with_value(value: &[T]) -> Vec<T>;
    fn set_capacity_to(&mut self, capacity: usize);
    fn set_capacity_to_secure(&mut self, capacity: usize);
    fn set_contents_from_slice(&mut self, other: &[T]);
    fn set_contents_from_slice_secure(&mut self, other: &[T]);
    fn shrink_to_fit_secure(&mut self);
    fn reserve_secure(&mut self, additional: usize);
    fn extend_from_slice_secure(&mut self, other: &[T]);
}
Expand description

This trait adds some extension methods to std::vec::Vec for primitive types like integers, floating points and booleans.

Most of those extensions are designed to be either fast implementations of existing methods or secure versions of them.

§Safety

Some operations performed by this extension relies heavily on pointer operations and bitwise copies (see std::ptr for further details). Because of that, it is not safe to implement this trait for non primitive types because it may lead to memory safety violations and potential double free situations.

§Secure variants

This extension also include secure variants of some of the vector methods. Those variants always zeroes the memory segments before releasing them back to the memory pool.

Logically, they do perform the same operations but they are way more expensive than their regular versions. We recommend the use of those versions if and only if you need to avoid potential confidential data leak to the system.

It is possible that, when the allocator_api #32838 become fully integrated into the standard API, those methods will no longer be necessary as the proper memory cleanup will be done by an std::alloc::Allocator instead of the hacks used by those methods.

Required Methods§

Source

fn with_value(value: &[T]) -> Vec<T>

Creates a new vector already initialized with the specified value.

Since it is the first allocation, there is no need to have a secure version of this constructor.

Arguments:

  • value: The initial value of the new Vec instance, the elements of this slice are copied into the new vector;
Source

fn set_capacity_to(&mut self, capacity: usize)

This method sets the capacity of the given Vec to hold at least the specified amount of entries. It is similar to Vec<u8>::reserve() but it takes the target capacity insted of an additional capacity.

If the current capacity is equal or larger than the required capacity, this method does nothing.

Arguments:

  • capacity: The new capacity;
Source

fn set_capacity_to_secure(&mut self, capacity: usize)

This method is the secure variant of Self::set_capacity_to().

Arguments:

  • capacity: The new capacity;
Source

fn set_contents_from_slice(&mut self, other: &[T])

Replaces the contents of this vector with the contents of a given slice. It will expand the size of this vector as needed but will never shrink it.

Arguments:

  • other: The new capacity;
Source

fn set_contents_from_slice_secure(&mut self, other: &[T])

This method is the secure variant of Self::set_contents_from_slice().

Arguments:

  • other: The new capacity;
Source

fn shrink_to_fit_secure(&mut self)

This method is the secure version of std::vec::Vec::shrink_to_fit().

Source

fn reserve_secure(&mut self, additional: usize)

This method is the secure version of std::vec::Vec::reserve().

Source

fn extend_from_slice_secure(&mut self, other: &[T])

This method is the secure version of std::vec::Vec::extend_from_slice().

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl VecExtensions<bool> for Vec<bool>

Source§

fn with_value(value: &[bool]) -> Vec<bool>

Source§

fn set_capacity_to(&mut self, capacity: usize)

Source§

fn set_capacity_to_secure(&mut self, capacity: usize)

Source§

fn set_contents_from_slice(&mut self, other: &[bool])

Source§

fn set_contents_from_slice_secure(&mut self, other: &[bool])

Source§

fn shrink_to_fit_secure(&mut self)

Source§

fn reserve_secure(&mut self, additional: usize)

Source§

fn extend_from_slice_secure(&mut self, other: &[bool])

Source§

impl VecExtensions<f32> for Vec<f32>

Source§

fn with_value(value: &[f32]) -> Vec<f32>

Source§

fn set_capacity_to(&mut self, capacity: usize)

Source§

fn set_capacity_to_secure(&mut self, capacity: usize)

Source§

fn set_contents_from_slice(&mut self, other: &[f32])

Source§

fn set_contents_from_slice_secure(&mut self, other: &[f32])

Source§

fn shrink_to_fit_secure(&mut self)

Source§

fn reserve_secure(&mut self, additional: usize)

Source§

fn extend_from_slice_secure(&mut self, other: &[f32])

Source§

impl VecExtensions<f64> for Vec<f64>

Source§

fn with_value(value: &[f64]) -> Vec<f64>

Source§

fn set_capacity_to(&mut self, capacity: usize)

Source§

fn set_capacity_to_secure(&mut self, capacity: usize)

Source§

fn set_contents_from_slice(&mut self, other: &[f64])

Source§

fn set_contents_from_slice_secure(&mut self, other: &[f64])

Source§

fn shrink_to_fit_secure(&mut self)

Source§

fn reserve_secure(&mut self, additional: usize)

Source§

fn extend_from_slice_secure(&mut self, other: &[f64])

Source§

impl VecExtensions<i8> for Vec<i8>

Source§

fn with_value(value: &[i8]) -> Vec<i8>

Source§

fn set_capacity_to(&mut self, capacity: usize)

Source§

fn set_capacity_to_secure(&mut self, capacity: usize)

Source§

fn set_contents_from_slice(&mut self, other: &[i8])

Source§

fn set_contents_from_slice_secure(&mut self, other: &[i8])

Source§

fn shrink_to_fit_secure(&mut self)

Source§

fn reserve_secure(&mut self, additional: usize)

Source§

fn extend_from_slice_secure(&mut self, other: &[i8])

Source§

impl VecExtensions<i16> for Vec<i16>

Source§

fn with_value(value: &[i16]) -> Vec<i16>

Source§

fn set_capacity_to(&mut self, capacity: usize)

Source§

fn set_capacity_to_secure(&mut self, capacity: usize)

Source§

fn set_contents_from_slice(&mut self, other: &[i16])

Source§

fn set_contents_from_slice_secure(&mut self, other: &[i16])

Source§

fn shrink_to_fit_secure(&mut self)

Source§

fn reserve_secure(&mut self, additional: usize)

Source§

fn extend_from_slice_secure(&mut self, other: &[i16])

Source§

impl VecExtensions<i32> for Vec<i32>

Source§

fn with_value(value: &[i32]) -> Vec<i32>

Source§

fn set_capacity_to(&mut self, capacity: usize)

Source§

fn set_capacity_to_secure(&mut self, capacity: usize)

Source§

fn set_contents_from_slice(&mut self, other: &[i32])

Source§

fn set_contents_from_slice_secure(&mut self, other: &[i32])

Source§

fn shrink_to_fit_secure(&mut self)

Source§

fn reserve_secure(&mut self, additional: usize)

Source§

fn extend_from_slice_secure(&mut self, other: &[i32])

Source§

impl VecExtensions<i64> for Vec<i64>

Source§

fn with_value(value: &[i64]) -> Vec<i64>

Source§

fn set_capacity_to(&mut self, capacity: usize)

Source§

fn set_capacity_to_secure(&mut self, capacity: usize)

Source§

fn set_contents_from_slice(&mut self, other: &[i64])

Source§

fn set_contents_from_slice_secure(&mut self, other: &[i64])

Source§

fn shrink_to_fit_secure(&mut self)

Source§

fn reserve_secure(&mut self, additional: usize)

Source§

fn extend_from_slice_secure(&mut self, other: &[i64])

Source§

impl VecExtensions<i128> for Vec<i128>

Source§

fn with_value(value: &[i128]) -> Vec<i128>

Source§

fn set_capacity_to(&mut self, capacity: usize)

Source§

fn set_capacity_to_secure(&mut self, capacity: usize)

Source§

fn set_contents_from_slice(&mut self, other: &[i128])

Source§

fn set_contents_from_slice_secure(&mut self, other: &[i128])

Source§

fn shrink_to_fit_secure(&mut self)

Source§

fn reserve_secure(&mut self, additional: usize)

Source§

fn extend_from_slice_secure(&mut self, other: &[i128])

Source§

impl VecExtensions<u8> for Vec<u8>

Source§

fn with_value(value: &[u8]) -> Vec<u8>

Source§

fn set_capacity_to(&mut self, capacity: usize)

Source§

fn set_capacity_to_secure(&mut self, capacity: usize)

Source§

fn set_contents_from_slice(&mut self, other: &[u8])

Source§

fn set_contents_from_slice_secure(&mut self, other: &[u8])

Source§

fn shrink_to_fit_secure(&mut self)

Source§

fn reserve_secure(&mut self, additional: usize)

Source§

fn extend_from_slice_secure(&mut self, other: &[u8])

Source§

impl VecExtensions<u16> for Vec<u16>

Source§

fn with_value(value: &[u16]) -> Vec<u16>

Source§

fn set_capacity_to(&mut self, capacity: usize)

Source§

fn set_capacity_to_secure(&mut self, capacity: usize)

Source§

fn set_contents_from_slice(&mut self, other: &[u16])

Source§

fn set_contents_from_slice_secure(&mut self, other: &[u16])

Source§

fn shrink_to_fit_secure(&mut self)

Source§

fn reserve_secure(&mut self, additional: usize)

Source§

fn extend_from_slice_secure(&mut self, other: &[u16])

Source§

impl VecExtensions<u32> for Vec<u32>

Source§

fn with_value(value: &[u32]) -> Vec<u32>

Source§

fn set_capacity_to(&mut self, capacity: usize)

Source§

fn set_capacity_to_secure(&mut self, capacity: usize)

Source§

fn set_contents_from_slice(&mut self, other: &[u32])

Source§

fn set_contents_from_slice_secure(&mut self, other: &[u32])

Source§

fn shrink_to_fit_secure(&mut self)

Source§

fn reserve_secure(&mut self, additional: usize)

Source§

fn extend_from_slice_secure(&mut self, other: &[u32])

Source§

impl VecExtensions<u64> for Vec<u64>

Source§

fn with_value(value: &[u64]) -> Vec<u64>

Source§

fn set_capacity_to(&mut self, capacity: usize)

Source§

fn set_capacity_to_secure(&mut self, capacity: usize)

Source§

fn set_contents_from_slice(&mut self, other: &[u64])

Source§

fn set_contents_from_slice_secure(&mut self, other: &[u64])

Source§

fn shrink_to_fit_secure(&mut self)

Source§

fn reserve_secure(&mut self, additional: usize)

Source§

fn extend_from_slice_secure(&mut self, other: &[u64])

Source§

impl VecExtensions<u128> for Vec<u128>

Source§

fn with_value(value: &[u128]) -> Vec<u128>

Source§

fn set_capacity_to(&mut self, capacity: usize)

Source§

fn set_capacity_to_secure(&mut self, capacity: usize)

Source§

fn set_contents_from_slice(&mut self, other: &[u128])

Source§

fn set_contents_from_slice_secure(&mut self, other: &[u128])

Source§

fn shrink_to_fit_secure(&mut self)

Source§

fn reserve_secure(&mut self, additional: usize)

Source§

fn extend_from_slice_secure(&mut self, other: &[u128])

Implementors§