Trait VecExt

Source
pub trait VecExt: Sized {
    type T;

    // Required methods
    fn try_map<U, R: Try<Ok = U>, F: FnMut(Self::T) -> R>(
        self,
        f: F,
    ) -> Result<Vec<U>, R::Error>;
    fn try_zip_with<U, V, R: Try<Ok = V>, F: FnMut(Self::T, U) -> R>(
        self,
        other: Vec<U>,
        f: F,
    ) -> Result<Vec<V>, R::Error>;
    fn drop_and_reuse<U>(self) -> Vec<U>;

    // Provided methods
    fn map<U, F: FnMut(Self::T) -> U>(self, f: F) -> Vec<U> { ... }
    fn zip_with<U, V, F: FnMut(Self::T, U) -> V>(
        self,
        other: Vec<U>,
        f: F,
    ) -> Vec<V> { ... }
}
Expand description

Extension methods for Vec<T>

Required Associated Types§

Source

type T

The type that the Vec<T> stores

Required Methods§

Source

fn try_map<U, R: Try<Ok = U>, F: FnMut(Self::T) -> R>( self, f: F, ) -> Result<Vec<U>, R::Error>

Map a vector to another vector, will try and reuse the allocation if the allocation layouts of the two types match, i.e. if std::alloc::Layout::<T>::new() == std::alloc::Layout::<U>::new() then the allocation will be reused

The mapping function can be fallible, and on early return, it will drop all previous values, and the rest of the input vector. Thre error will be returned as a Result

Source

fn try_zip_with<U, V, R: Try<Ok = V>, F: FnMut(Self::T, U) -> R>( self, other: Vec<U>, f: F, ) -> Result<Vec<V>, R::Error>

Zip a vector to another vector and combine them, the result will be returned, the allocation will be reused if possible, the larger allocation of the input vectors will be used if all of T, U, and V have the same allocation layouts.

The mapping function can be fallible, and on early return, it will drop all previous values, and the rest of the input vectors. Thre error will be returned as a Result

Source

fn drop_and_reuse<U>(self) -> Vec<U>

Drops all of the values in the vector and create a new vector from it if the layouts are compatible

if layouts are not compatible, then return Vec::new()

Provided Methods§

Source

fn map<U, F: FnMut(Self::T) -> U>(self, f: F) -> Vec<U>

Map a vector to another vector, will try and reuse the allocation if the allocation layouts of the two types match, i.e. if std::alloc::Layout::<T>::new() == std::alloc::Layout::<U>::new() then the allocation will be reused

Source

fn zip_with<U, V, F: FnMut(Self::T, U) -> V>( self, other: Vec<U>, f: F, ) -> Vec<V>

Zip a vector to another vector and combine them, the result will be returned, the allocation will be reused if possible, the larger allocation of the input vectors will be used if all of T, U, and V have the same allocation layouts.

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<T> VecExt for Vec<T>

Source§

type T = T

Source§

fn try_map<U, R: Try<Ok = U>, F: FnMut(Self::T) -> R>( self, f: F, ) -> Result<Vec<U>, R::Error>

Source§

fn try_zip_with<U, V, R: Try<Ok = V>, F: FnMut(Self::T, U) -> R>( self, other: Vec<U>, f: F, ) -> Result<Vec<V>, R::Error>

Source§

fn drop_and_reuse<U>(self) -> Vec<U>

Implementors§