heapless icon indicating copy to clipboard operation
heapless copied to clipboard

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

Open sosthene-nitrokey opened this issue 1 year ago • 1 comments

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 avatar Jul 03 '24 08:07 sosthene-nitrokey

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.

sosthene-nitrokey avatar Jul 03 '24 08:07 sosthene-nitrokey

To get it working with LinearMap and String (which have constraints on the the inner type of the Vec), I had to create new traits for them. I wonder if that would not be beneficial for the other types too.

sosthene-nitrokey avatar Mar 21 '25 16:03 sosthene-nitrokey

The cpass tests are blocked by a compiler bug that has been fixed today. I'm not sure if the fix will be included in 1.86 or whether we will have to wait for 1.87: https://github.com/rust-lang/rust/issues/138979

sosthene-nitrokey avatar Mar 28 '25 10:03 sosthene-nitrokey

Ok, I have a workaround for the compiler ICE, that doesn't appear to have any downsides. Added it and documented it.

sosthene-nitrokey avatar Mar 28 '25 10:03 sosthene-nitrokey

Thanks, @sosthene-nitrokey!

reitermarkus avatar Apr 05 '25 21:04 reitermarkus