rust-embedded / heapless

Heapless, `static` friendly data structures
Apache License 2.0
1.52k stars 181 forks source link

Implement `Vec::drain`, `as_(mut_)view` on the `*Inner` types, generic over `Storage` #500

Open sosthene-nitrokey opened 3 months ago

sosthene-nitrokey commented 3 months ago

This makes drain usable in the context of an implementation generic over the storage This also makes it possible to get a *View in this same context

This could also be a way to "de-monomorphize" all the implementations of the Inner structs:

We could make all implementation define an inner function that is over a View type, and start the implementation with this = self.as_(mut_)view, maybe for binary size and compilation time benefits. For example the implementation of VecInner::push could be:

    /// Appends an `item` to the back of the collection
    ///
    /// Returns back the `item` if the vector is full.
    pub fn push(&mut self, item: T) -> Result<(), T> {
        fn inner<T>(this: &mut VecView<T>, item: T) -> Result<(), T> {
            if this.len < this.storage_capacity() {
                unsafe { this.push_unchecked(item) }
                Ok(())
            } else {
                Err(item)
            }
        }
        inner(self.as_mut_view(), item)
    }
sosthene-nitrokey commented 3 months ago

I checked with nightly, we can't do that with just a type Buffer<T>: Unsize<[T], because there is no implementation of Unsize<[T]> for [T], that's a shame. So even if Unsize gets stabilized, we'll have to keep all the manual conversion methods.