Added SmallVec::push_mut andSmallVec::insert_mut - #406
Conversation
emilio
left a comment
There was a problem hiding this comment.
Would be nice to split the formatting into a separate (preliminary?) PR.
|
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 }
} |
yeah when the I honestly support setting the length inline with the but before hacking it we should change how length behaves, then we implement it properly |
|
@alejandro-vaz Should the same dirty workaround be used for |
|
yeah the |
|
please rebase the commit history onto servo:v2 to remove merge commits and have a linear history |
- applied cargo fmt
Reverted cargo fmt changes
d3d962d to
781aa43
Compare
| { | ||
| // This block is an exact copy of `self.set_len`. | ||
| // We have to do this so that Miri doesn't report a "Stacked Borrows" rule violation. | ||
| // See PR/406 | ||
|
|
||
| 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); | ||
| } |
There was a problem hiding this comment.
yeah this is good for now until we implement #451
Signed-off-by: Alejandro Vaz <alejandro.vaz.myt@gmail.com>
|
@mematthias I had to merge upstream changes, it annoyed rustfmt please run |
old review, changes already implemented
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.