Added SmallVec::push_mut andSmallVec::insert_mut#406
Added SmallVec::push_mut andSmallVec::insert_mut#406mematthias wants to merge 4 commits intoservo:v2from
Conversation
- applied cargo fmt
emilio
left a comment
There was a problem hiding this comment.
Would be nice to split the formatting into a separate (preliminary?) PR.
| unsafe { ptr.write(value) }; | ||
| unsafe { self.set_len(len + 1) } | ||
|
|
||
| let result = unsafe { ptr.as_mut() }; |
There was a problem hiding this comment.
Maybe just &mut *ptr? Or why would as_mut().unwrap_unchecked() be preferred?
There was a problem hiding this comment.
That is just my writing style; it makes it easier to search.
I simplified it anyway
Reverted cargo fmt changes
|
miri seems to be getting confused on the provenance? |
|
as the guy who did push_mut in std, i feel like the change just made kind of defeats the point? the whole point of push_mut is that you're not rederiving the pointer |
|
@balt-dev While I understand the concerns raised, I am currently unable to resolve the Miri report. That said, this should not block us from shipping the feature. At the moment, users neither benefit from nor are harmed by it. But by making it available now, we ensure that once the optimization is in place, existing users can immediately take advantage of it without any disruption to their experience. One possible (but dirty) solution would be to use #[inline]
pub fn push(&mut self, value: T) {
_ = self.push_mut(value);
}
#[inline]
#[must_use]
pub fn push_mut(&mut self, value: T) -> &mut T {
let len = self.len();
if len == self.capacity() {
self.reserve(1);
}
// SAFETY: both the input and output are within the allocation
let ptr = unsafe { self.as_mut_ptr().add(len) };
// SAFETY: we allocated enough space in case it wasn't enough, so the address is valid for
// writes.
unsafe { ptr.write(value) };
// unsafe { self.set_len(len + 1) };
{
let new_len = len + 1;
debug_assert!(new_len <= self.capacity());
let on_heap = self.len.on_heap();
self.len = TaggedLen::new(new_len, on_heap);
}
unsafe { &mut *ptr }
} |
The
push_mutfeature (rust-lang/rust#135974) has already been merged and is expected to be included in the Rust 1.95 stable release. For this reason, we should also addpush_mutandinsert_muthere.