Skip to content

Commit d84cb37

Browse files
authored
Merge pull request #111 from bluss/miri-complaints
Fix just a few stacked borrow issues, and improve extend performance
2 parents b197664 + f40e708 commit d84cb37

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

benches/extend.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate arrayvec;
55
use arrayvec::ArrayVec;
66

77
use bencher::Bencher;
8+
use bencher::black_box;
89

910
fn extend_with_constant(b: &mut Bencher) {
1011
let mut v = ArrayVec::<[u8; 512]>::new();
@@ -33,7 +34,7 @@ fn extend_with_slice(b: &mut Bencher) {
3334
let data = [1; 512];
3435
b.iter(|| {
3536
v.clear();
36-
v.extend(data.iter().cloned());
37+
v.extend(black_box(data.iter()).cloned());
3738
v[0]
3839
});
3940
b.bytes = v.capacity() as u64;

src/lib.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,11 @@ impl<A: Array> ArrayVec<A> {
461461
/// array.truncate(4);
462462
/// assert_eq!(&array[..], &[1, 2, 3]);
463463
/// ```
464-
pub fn truncate(&mut self, len: usize) {
464+
pub fn truncate(&mut self, new_len: usize) {
465465
unsafe {
466-
if len < self.len() {
467-
let tail: *mut [_] = &mut self[len..];
468-
self.set_len(len);
466+
if new_len < self.len() {
467+
let tail: *mut [_] = &mut self[new_len..];
468+
self.len = Index::from(new_len);
469469
ptr::drop_in_place(tail);
470470
}
471471
}
@@ -890,10 +890,10 @@ impl<A: Array> Extend<A::Item> for ArrayVec<A> {
890890
// We update the length to handle panic in the iteration of the
891891
// user's iterator, without dropping any elements on the floor.
892892
let mut guard = ScopeExitGuard {
893-
value: self,
893+
value: &mut self.len,
894894
data: len,
895-
f: |&len, self_| {
896-
self_.set_len(len)
895+
f: move |&len, self_len| {
896+
**self_len = Index::from(len);
897897
}
898898
};
899899
for elt in iter.into_iter().take(take) {

0 commit comments

Comments
 (0)