heapless
heapless copied to clipboard
Implement `Vec::drain`, `as_(mut_)view` on the `*Inner` types, generic over `Storage`
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)
}
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.
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.
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
Ok, I have a workaround for the compiler ICE, that doesn't appear to have any downsides. Added it and documented it.
Thanks, @sosthene-nitrokey!