Skip to content

How to create a TinyVec of a specific size (not capacity!), initialized to zero (or user-defined value) #210

@dEajL3kA

Description

@dEajL3kA

Hello.

What is the most efficient way to create a new TinyVec of size length, which is initialized zero (or some user-defined value)?

Is there a better way than this?

#[inline(always)]
pub fn calloc_tinyvec<const N: usize>(length: usize) -> TinyVec<[u8; N]> {
    let mut vec = TinyVec::with_capacity(length);
    vec.resize(length, 0u8);
    vec
}

Is this version preferible?

#[inline(always)]
pub fn calloc_tinyvec<const N: usize>(length: usize) -> TinyVec<[u8; N]> {
    if length > N {
        let mut vec = TinyVec::with_capacity(length);
        vec.resize(length, 0u8);
        vec
    } else {
        TinyVec::from_array_len([0u8; N], length)
    }
}

Maybe it could be useful to have a method like:

TinyVec::with_size<A: Array>(length: usize, inital_value: T) -> TinyVec<A>

🤔

Best regards!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions