Wow! Yes, that's pretty much it!
I've started pretty much the same thing myself, but the following line from Vec documentation stopped me in my tracks:
Bulk insertion methods may reallocate, even when not necessary.
So according to the documentation, this works:
pub fn push(&mut self, item: T) {
assert!(self.additional_cap() > 0);
self.buffer.push(item)
}
But this may reallocate and invalidate our precious slice:
pub fn extend_from_slice(&mut self, other: &[T]) {
assert!(other.len() <= self.additional_cap());
self.buffer.extend_from_slice(other)
}
This is not the case in practice for slices of Copy types because it specializes into an exact space reservation, but the generic implementation does in fact allocate needlessly.
However, that's nothing a bit of custom unsafe code can't fix.