Skip to content

Add const-erased versions of the various containers #371

Open
@sosthene-nitrokey

Description

@sosthene-nitrokey

Making code flexible around heapless structures can be a bit verbose because it requires being generic over the capacity of each collection. This could be helped by providing versions of the structures with the const erased. This would allow removing the generics in some cases.

For example, heapless::Vec currently is :

pub struct Vec<T, const N: usize> {
    len: usize,
    buffer: [MaybeUninit<T>; N],
}

It should be possible to provide a struct that goes hand in hand with it:

pub struct VecView<'a, T> {
    len: &'a mut len,
    buffer: &mut [MaybeUninit<T>],
}

impl<T, const N: usize> Vec<T, N> {
    fn as_view(&mut self) -> VecView<'_, T> {
        VecView { len: &mut self.len, buffer: &mut self.buffer }
    }
}

With VecView implementing most of the API that Vec currently implements. This would allow for example methods that write data to an outbuffer of type &mut Vec<T, N> to instead take a parameter VecView<'_, T>.

Something like:

fn do_something<const N: usize>(response_buffer: &mut Vec<u8, N>)

could become

fn do_something(response_buffer: VecView<'_, u8>)

In a trait for example this would also have the advantage of making it object-safe.

I believe this pattern:

  1. Improve ergonomics
  2. Might reduce compilation time and binary size because of monomorphisation (unverified)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions